使用dic和set数据结构解出,详细可看注释
# 字典非手写,同学请注意
def isPrime(num):#判断质数的函数
if num == 1 or num ==0:
return False
for i in range(2,num):
if num%i == 0 :
return False
return True
str1 = input()
lis1 =[]
set1 = {''}#初始化set
dic = {'a':0,'b':0,'c':0,'d':0,'e':0,'f':0,'g':0,'h':0,'i':0,'j':0,'k':0,'l':0,'m':0,'n':0,'o':0,'p':0,'q':0,'r':0,'s':0,'t':0,'u':0,'v':0,'w':0,'x':0,'y':0,'z':0}
for i in str1:
dic[i]+=1#把每个字母的数量加到字典里面
set1.add(i)#用set来数字母个数
if len(set1) == 2:#如果字母个数只有一个
differ = max(dic.values())#那么差值等于字典中values的最大值
else:
differ = max(dic.values())-min(value for value in dic.values() if value != 0)#其他情况差值等于最大values-最小values 同时把没出现的字母过滤掉
if(isPrime(differ)):#判断差值是否是质数
print("Lucky Word")
print(differ)
else:
print("No Answer")
print(0)
# 以下为字典生成代码
# # char_xx = [chr(i) for i in range(97, 123)]
# print('26个小写字母:', char_xx)
# for i in char_xx: print(f"'{i}':0,",end='')