/
main.py
153 lines (113 loc) · 3.12 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
"""
Takes in a string, which is tab separated and newline separated, and outputs
a graph, in a to-be-determined format.
"""
# import marisa_trie as trie
from pytrie import SortedStringTrie as trie
from time import time
words = open('englishWords.txt', 'r')
wordlines = words.readlines()
wordlines = [l.strip() for l in wordlines]
one = [1 for i in xrange(len(wordlines))]
zipped = zip(wordlines, one)
T = trie(zipped)
def boardToGraph(board):
rows = board.split('\n')
matrix = [row.split('\t') for row in rows]
if len(matrix) != 4 or len(matrix[0]) != 4:
print matrix
print "something is wrong"
raise Exception("Something is wrong")
node_set = set()
node_dict = {}
for i in range(16):
d = {}
r = i%4
c=int(i/4)
entry = matrix[r][c]
d['id'] = i
d['val'] = entry
neighbors = set()
rs = [p for p in [r-1,r,r+1] if p >=0 and p <4]
cs = [p for p in [c-1,c,c+1] if p >=0 and p <4]
for row in rs:
for col in cs:
if row==r and col==c:
continue
new_id = row + col*4
neighbors.add(new_id)
d['neighbors'] = neighbors
node_dict[i]=d
return node_dict
# graph = boardToGraph(input_string)
# print graph
# That works!
def is_prefix(string):
result = (len(T.items(prefix=string)) != 0)
# print "string: " + string + " is " + str(result)
return result
# return True
# if len(string) <=8:
# return True
# return False
def is_word(string):
has = T.has_key(string)
# if has:
# print string + " is a word"
return has
def dfs(node_dict, string_so_far, ids_so_far, master_set):
if len(string_so_far) >=4 and is_word(string_so_far):
master_set.add(string_so_far)
most_recent_id = ids_so_far[-1]
this_node = node_dict[most_recent_id]
neighbors = this_node['neighbors']
new_neigbors = [n for n in neighbors if n not in ids_so_far]
if len(new_neigbors) == 0:
return
if not is_prefix(string_so_far):
return
for bordering in new_neigbors:
new_string_so_far = string_so_far + node_dict[bordering]['val']
new_ids = list(ids_so_far)
new_ids.append(bordering)
dfs(node_dict, new_string_so_far, new_ids, master_set)
return
def words_from_board(board):
graph = boardToGraph(board)
master_set = set()
for i in range(16):
print str(i)
print "\n"
node = graph[i]
id_list = [i]
string_so_far = node['val']
dfs(graph, string_so_far, id_list, master_set)
print master_set
print "\n\n\n\n\n"
return master_set
# input_string = "1a\t2a\t3a\t4a\t5a\n" +\
# "1b\t2b\t3b\t4b\t5b\n" +\
# "1c\t2c\t3c\t4c\t5c\n" +\
# "1d\t2d\t3d\t4d\t5d\n" +\
# "1e\t2e\t3e\t4e\t5e"
start = time()
# input_string = "qu\te\tn\tt\te\n" +\
# "s\tr\te\ts\ta\n" +\
# "p\te\ti\tc\tr\n" +\
# "a\tb\tt\te\tr\n" +\
# "z\ts\ts\ti\tc"
input_string = "l\to\tr\ts\n" +\
"v\te\te\tp\n" +\
"h\tl\ti\tn\n" +\
"y\tn\ts\te"
s = words_from_board(input_string)
l_s = list(s)
l_s.sort(key=len, reverse=True)
print "\n\n\n\n\n"
for i in l_s:
print i
print "words found: " + str(len(s))
points = sum([max(0, len(w)-3) for w in s])
print "total points: " + str(points)
print "time taken is " + str(time() - start)
# print is_word("cereal")