import re alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" best = "JQBPCOUFVWMHAXZTRLIKNGEDSY" # 3038 words words = load('words_list') # rules = generateRules(alpha) # This generates all the rules (650) # rules = parseRules(words, rules) # This attributes them scores # rules = purgeRules(rules) # This removes rules opposite each other # eg (A,B) and (B,A), it removes the lower scoring rule. rules = load('purged-rules-backup') monsterBuild(alpha, rules) def wordAgainstRule(word,rule): pattern = rule[1]+".*"+rule[0] if (re.search(pattern,word)==None): #print("Rule Stands") return 1 else: #print("Rule Broken") return 0 def ruleAgainstList(words,rule): sum = 0 for word in words: sum += wordAgainstRule(word,rule) return sum def parseRules(words,rules): for x in rules: x[2] = ruleAgainstList(words,x) rules.sort(key=lambda x: x[2]) return rules def wordAgainstAlpha(word,alpha): for i in range(25): if (wordAgainstRule(word,[alpha[i],alpha[i+1],0])==0): #print("Not Alphabetical") return 0 #print ("Alphabetical") return 1 def purgeRules(rules): for x in rules: for y in rules: if (x[0] == y[1]) & (x[1] == y[0]): rules.pop(rules.index(y)) def generateRules(alpha): rules = [] for x in alpha: for y in alpha: if(x!=y): rules.append([x,y,0]) return rules def buildAlpha(alpha,rules): for x in rules: if wordAgainstRule(alpha,x) == 0: return x def letterSwitch(alpha,rule): alphaArray = [] alphaString = "" for x in alpha: alphaArray.append(x) popped = alphaArray.pop(alphaArray.index(rule[0])) alphaArray.insert(alphaArray.index(rule[1]), popped) for x in alphaArray: alphaString += x return alphaString def monsterBuild(alpha, rules): alphas = [] rule = buildAlpha(alpha, rules) while rule: alpha = letterSwitch(alpha, rule) if alpha in alphas: rules.pop(rules.index(rule)) else: alphas.append(alpha) rule = buildAlpha(alpha,rules) print(rule, alpha)