This repository has been archived by the owner on Nov 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathword_finder.py
120 lines (94 loc) · 3.05 KB
/
word_finder.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
from tree_struct import *
import heapq
class MinHeap:
def __init__(self):
self.heap = []
def push(self, item):
heapq.heappush(self.heap, item)
def pop(self):
return heapq.heappop(self.heap)
def peek(self):
return self.heap[0]
def __getitem__(self, item):
return self.heap[item]
def __len__(self):
return len(self.heap)
class MaxHeap(MinHeap):
def push(self, item):
heapq.heappush(self.heap, Comparator(item))
class Comparator:
def __init__(self, val):
self.val = val
def __lt__(self, other):
return self.val[1] > other.val[1]
def __eq__(self, other):
return self.val[1] == other.val[1]
def __repr__(self):
return repr(self.val[0])
#initialize and load lexicon
alphabet = ['Α', 'Β', 'Γ', 'Δ', 'Ε', 'Ζ', 'Η', 'Θ', 'Ι', 'Κ', 'Λ', 'Μ', 'Ν', 'Ξ', 'Ο', 'Π', 'Ρ', 'Σ', 'Τ', 'Υ', 'Φ', 'Χ', 'Ψ', 'Ω']
#board is given
board = [['Γ', 'Λ', 'Ο', 'Σ'], ['Δ', 'Ε', 'Κ', 'Α'], ['Τ', 'Ι', 'Μ', 'Η'], ['Μ', 'Ε', 'Σ', 'Μ']]
values = {'Α' : 1, 'Β' : 8, 'Γ' : 4, 'Δ' : 4, 'Ε' : 1, 'Ζ' : 10, 'Η' : 2, 'Θ' : 10, 'Ι' : 1, 'Κ' : 2, 'Λ' : 3, 'Μ' : 3, 'Ν' : 1, 'Ξ' : 10, 'Ο' : 1, 'Π' : 2, 'Ρ' : 2, 'Σ' : 1, 'Τ' : 1, 'Υ' : 2, 'Φ' : 8, 'Χ' : 8, 'Ψ' : 10, 'Ω' : 3}
#little change
def add_coord(x):
if x + 1 < 4:
return x + 1
return None
def sub_coord(x):
if x - 1 > -1:
return x - 1
return None
def find_neighbors(coords):
neighbors = []
x = coords[0]
y = coords[1]
ax = add_coord(x)
ay = add_coord(y)
sx = sub_coord(x)
sy = sub_coord(y)
if(ax != None):
neighbors.append((ax, y))
if(ay != None):
neighbors.append((ax, ay))
if(sy != None):
neighbors.append((ax, sy))
if(ay != None):
neighbors.append((x, ay))
if(sx != None):
neighbors.append((sx, ay))
if(sx != None):
neighbors.append((sx, y))
if(sy != None):
neighbors.append((sx, sy))
if(sy != None):
neighbors.append((x, sy))
return neighbors
def calc_points(l, board):
sm = 0
acc = 1
for i in l:
acc *= board[i[0]][i[1]][2]
sm += values[board[i[0]][i[1]][0]] * board[i[0]][i[1]][1]
return sm * acc
def DFS(coords, sofar, lex_tree, board, max_heap):
sofar.append(coords)
neighbors = find_neighbors(coords)
if lex_tree.finished_word == True:
max_heap.push((sofar, calc_points(sofar, board)))
for n in neighbors:
if n in sofar or board[n[0]][n[1]][0] not in lex_tree.neighbors.keys():
continue
DFS(n, sofar.copy(), lex_tree.neighbors[board[n[0]][n[1]][0]], board, max_heap)
def run_board(board):
lex = lexicon()
lex.load("lexicon.wb")
max_heap = MaxHeap()
for i in range(4):
for j in range(4):
DFS((i, j), [], lex.get_tree(board[i][j][0]), board, max_heap)
return max_heap
if __name__ == '__main__':
print("start")
heap = run_board(board)
print(heap.heap)