original_id_list = "1:50000"

import re,json
suffix_words = ["试题",r"第[\d]*题"]

def strip_suffix(problem, suf_words):
    origin = problem["origin"]
    for sw in suf_words:
        origin = re.sub(sw+r"[\S]*$","",origin)
    return(origin)

def generate_number_set(string):
    string = re.sub(r"[\n\s]","",string)
    string_list = string.split(",")
    numbers_list = []
    for s in string_list:
        if not ":" in s:
            numbers_list.append(s.zfill(6))
        else:
            start,end = s.split(":")
            for ind in range(int(start),int(end)+1):
                numbers_list.append(str(ind).zfill(6))
    return numbers_list

def generate_exp(id_list):
    if not len(id_list) == 0:
        exp_list = []
        start = id_list[0]
        current = start
        end = start
        for id in id_list[1:]:
            # print(id,current)
            if int(id)-1 == int(current):
                current = id
                end = id
            else:
                if not start == end:
                    exp_list.append('"'+start+":"+end+'"')
                else:
                    exp_list.append('"'+start+'"')
                start = id
                current = id
                end = id
        if not start == end:
            exp_list.append('"'+start+":"+end+'"')
        else:
            exp_list.append('"'+start+'"')
    return exp_list

with open(r"..\题库0.3\Problems.json","r",encoding="u8") as f:
    pro_dict = json.loads(f.read())

idlist =  generate_number_set(original_id_list)
setlist = {}
for id in idlist:
    if id in pro_dict:
        p = pro_dict[id]
        origin = strip_suffix(p,suffix_words)
        if not origin in setlist:
            setlist[origin] = [id]
        else:
            setlist[origin].append(id)
outputlist = {}
for origin in setlist:
    outputlist[origin] = ",".join(generate_exp(setlist[origin])).replace('"',"")
t = json.dumps(outputlist,ensure_ascii=False,indent=4)
with open("临时文件/分类题号列表.txt","w",encoding="u8") as f:
    f.write(t)
print("已输出至 临时文件/分类题号列表.txt")