Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

alphabet challenge by zelrok

By: a guest on May 15th, 2013  |  syntax: Python  |  size: 2.28 KB  |  views: 52  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import re
  2.  
  3. alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  4. best  = "JQBPCOUFVWMHAXZTRLIKNGEDSY" # 3038 words
  5. words = load('words_list')
  6. # rules = generateRules(alpha) # This generates all the rules (650)
  7. # rules = parseRules(words, rules) # This attributes them scores
  8. # rules = purgeRules(rules) #  This removes rules opposite each other
  9. # eg (A,B) and (B,A), it removes the lower scoring rule.
  10. rules = load('purged-rules-backup')
  11. monsterBuild(alpha, rules)
  12.  
  13. def wordAgainstRule(word,rule):
  14.     pattern = rule[1]+".*"+rule[0]
  15.     if (re.search(pattern,word)==None):
  16.         #print("Rule Stands")
  17.         return 1
  18.     else:
  19.         #print("Rule Broken")
  20.         return 0
  21.    
  22. def ruleAgainstList(words,rule):
  23.     sum = 0
  24.     for word in words:
  25.         sum += wordAgainstRule(word,rule)
  26.     return sum
  27.    
  28. def parseRules(words,rules):
  29.     for x in rules:
  30.         x[2] = ruleAgainstList(words,x)
  31.     rules.sort(key=lambda x: x[2])
  32.     return rules
  33.    
  34. def wordAgainstAlpha(word,alpha):
  35.     for i in range(25):
  36.         if (wordAgainstRule(word,[alpha[i],alpha[i+1],0])==0):
  37.             #print("Not Alphabetical")
  38.             return 0
  39.     #print ("Alphabetical")
  40.     return 1
  41.    
  42. def purgeRules(rules):
  43.     for x in rules:
  44.         for y in rules:
  45.             if (x[0] == y[1]) & (x[1] == y[0]):
  46.                 rules.pop(rules.index(y))
  47.    
  48. def generateRules(alpha):
  49.     rules = []
  50.     for x in alpha:
  51.         for y in alpha:
  52.             if(x!=y):
  53.                 rules.append([x,y,0])
  54.     return rules
  55.    
  56. def buildAlpha(alpha,rules):
  57.     for x in rules:
  58.         if wordAgainstRule(alpha,x) == 0:
  59.             return x
  60.  
  61. def letterSwitch(alpha,rule):
  62.     alphaArray = []
  63.     alphaString = ""
  64.     for x in alpha:
  65.         alphaArray.append(x)
  66.     popped = alphaArray.pop(alphaArray.index(rule[0]))
  67.     alphaArray.insert(alphaArray.index(rule[1]), popped)
  68.     for x in alphaArray:
  69.         alphaString += x
  70.     return alphaString
  71.    
  72. def monsterBuild(alpha, rules):
  73.     alphas = []
  74.     rule = buildAlpha(alpha, rules)
  75.     while rule:
  76.         alpha = letterSwitch(alpha, rule)
  77.         if alpha in alphas:
  78.             rules.pop(rules.index(rule))
  79.         else:
  80.             alphas.append(alpha)
  81.         rule = buildAlpha(alpha,rules)
  82.         print(rule, alpha)
clone this paste RAW Paste Data