输入描述:
输入一个字符串,字符串中包含了全量字符集和已占用字符集,两个字符集用@相连。@前的字符集合为全量字符集,@后的字符集为已占用字符集合。已占用字符集中的字符一定是全量字符集中的字符。字符集中的字符跟字符之间使用英文逗号分隔。字符集中的字符表示为字符加数字,字符跟数字使用英文冒号分隔,比如a:1,表示1个a字符。字符只考虑英文字母,区分大小写,数字只考虑正整形,数量不超过100,如果一个字符都没被占用,@标识符仍在,例如a:3,b:5,c:2@
输出描述:
可用字符集。输出带回车换行。
def reststrs(inputstring):
allzips = inputstring.split("@")[0].split(",")
usedzips = inputstring.split("@")[1].split(",")
allalpha, usedalpha = [], []
allnum, usednum = [], []
rest = ""
for all in allzips:
allalpha.append(all.split(":")[0])
allnum.append(int(all.split(":")[1]))
try:
for used in usedzips:
usedalpha.append(used.split(":")[0])
usednum.append(int(used.split(":")[1]))
except:
pass
for i in range(len(allalpha)):
restalpha = allalpha[i]
if allalpha[i] not in usedalpha:
restnum = allnum[i]
else:
restnum = allnum[i] - usednum[usedalpha.index(allalpha[i])]
if restnum > 0:
if rest == "":
rest= restalpha + ":" + str(restnum)
else:
rest = rest + "\n" + restalpha + ":" + str(restnum)
# elif restnum < 0:
# return print(restalpha + "字符已占用超标!")
return rest
if __name__ == "__main__":
strs = "A:3,a:4,b:5,c:2@a:1,b:4"
print(reststrs(strs))