Public » Letterpress
Clone URL:  
Pushed to one repository · View In Graph Contained in tip

add the concept of favored letters

Changeset aee143254881

Parent 0c352cff9833

by Benjamin Pollack

Changes to one file · Browse files at aee143254881 Showing diff from parent 0c352cff9833 Diff from another changeset...

Change 1 of 3 Show Entire File solver.py Stacked
 
 
1
2
3
 
35
36
37
38
 
 
 
 
 
 
 
 
 
39
40
41
 
51
52
53
54
55
56
57
 
 
 
58
59
60
61
62
63
 
 
 
64
65
66
67
68
69
 
 
1
2
3
4
 
36
37
38
 
39
40
41
42
43
44
45
46
47
48
49
50
 
60
61
62
 
 
 
 
63
64
65
66
67
68
 
 
 
69
70
71
72
73
74
75
76
 
77
@@ -1,3 +1,4 @@
+import functools  import sys     @@ -35,7 +36,15 @@
  return s[0] in self._letters and s[1:] in self._letters[s[0]]     -def solve(board, forbidden): +def word_weight(word, favor): + score = 0 + for c in favor: + if c in word: + score += 3 + return score + len(word) + + +def solve(board, favor, forbidden):   def find_words(board, word, trie):   words = set()   if trie.is_terminal(): @@ -51,19 +60,18 @@
  print ' 0 words loaded',   for i, l in enumerate(open('wordlist', 'r')):   word = l.strip() - if word not in forbidden: - if i % 10 == 0: - print '\r %s words loaded' % (i,), - t.add(word) + if i % 100 == 0: + print '\r %s words loaded' % (i,), + t.add(word)   print '\r %s words loaded\n' % (i + 1,)     print 'SEARCHING' - print ' 0 words found', - words = sorted(find_words(board, '', t), key=lambda x: len(x), reverse=True) - print '\r %s words found\n' % len(words) + score = functools.partial(word_weight, favor=favor) + words = sorted((w for w in find_words(board, '', t) if w not in forbidden), key=lambda x: score(x), reverse=True) + print ' %s words found\n' % len(words)   print 'BEST WORDS:'   for word in words[:10]:   print ' %s' % word    if __name__ == '__main__': - solve(sys.argv[1], sys.argv[2:]) + solve(sys.argv[1], sys.argv[2], set(sys.argv[3:]))