# 合并相同题目的使用记录

import re,json,os

with open(r"../题库0.3/Problems.json","r",encoding = "utf8") as f:
    database = f.read()
pro_dict = json.loads(database)
same_groups = []

# 生成有使用记录的相同题目组
for id in pro_dict:
    same = pro_dict[id]["same"]
    if not len(pro_dict[id]["usages"]) == 0 and len(same) > 0:
        same_groups.append([id]+same)

# 合并使用记录并排序
for same_group in same_groups:
    current_usages = []
    for id in same_group:
        for usage in pro_dict[id]["usages"]:
            if not usage in current_usages:
                current_usages.append(usage)
    current_usages.sort()
    for id in same_group:
        pro_dict[id]["usages"] = current_usages.copy()

# 对所有使用记录排序
for id in pro_dict:
    usages = pro_dict[id]["usages"].copy()
    usages.sort()
    pro_dict[id]["usages"] = usages.copy()

with open(r"../题库0.3/Problems.json","w",encoding = "u8") as f:
    f.write(json.dumps(pro_dict,indent = 4, ensure_ascii= False))