-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_pigeon.py
152 lines (116 loc) · 5.16 KB
/
game_pigeon.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
from itertools import combinations, permutations
from openpyxl import load_workbook
class GamePigeon(object):
@staticmethod
def gen_words():
wb = load_workbook(filename='all_words.xlsx')
ws = wb.active
all_words = set()
for row in ws.values:
if row[0] is None:
break
all_words.add(row[0])
return all_words
@staticmethod
def check_word(word, input_set):
if word in input_set:
return True
else:
return False
def word_hunt(self, letters):
if len(letters) != 16:
return 'error: invalid input'
letters = letters.upper()
letters_array = []
for letter in letters:
letters_array.append(letter)
moves_dict = {
1: [2, 5, 6],
2: [1, 3, 5, 6, 7],
3: [2, 4, 6, 7, 8],
4: [3, 7, 8],
5: [1, 2, 6, 9, 10],
6: [1, 2, 3, 5, 7, 9, 10, 11],
7: [2, 3, 4, 6, 8, 10, 11, 12],
8: [3, 4, 7, 11, 12],
9: [5, 6, 10, 13, 14],
10: [5, 6, 7, 9, 11, 13, 14, 15],
11: [6, 7, 8, 10, 12, 14, 15, 16],
12: [7, 8, 11, 15, 16],
13: [9, 10, 14],
14: [9, 10, 11, 13, 15],
15: [10, 11, 12, 14, 16],
16: [11, 12, 15]
}
moves_list = []
for start_point in range(16):
start = start_point + 1
temp_combo = [start]
for move2 in moves_dict[start]:
temp_combo.append(move2)
for move3 in moves_dict[move2]:
if move3 not in temp_combo:
temp_combo.append(move3)
for move4 in moves_dict[move3]:
if move4 not in temp_combo:
temp_combo.append(move4)
moves_list.append(temp_combo[:])
for move5 in moves_dict[move4]:
if move5 not in temp_combo:
temp_combo.append(move5)
moves_list.append(temp_combo[:])
for move6 in moves_dict[move5]:
if move6 not in temp_combo:
temp_combo.append(move6)
moves_list.append(temp_combo[:])
for move7 in moves_dict[move6]:
if move7 not in temp_combo:
temp_combo.append(move7)
moves_list.append(temp_combo[:])
for move8 in moves_dict[move7]:
if move8 not in temp_combo:
temp_combo.append(move8)
moves_list.append(temp_combo[:])
temp_combo.remove(move8)
temp_combo.remove(move7)
temp_combo.remove(move6)
temp_combo.remove(move5)
temp_combo.remove(move4)
temp_combo.remove(move3)
temp_combo.remove(move2)
all_words_list = self.gen_words()
letter_combos = []
for move in moves_list:
word = ''
for position in move:
word = word + letters_array[position - 1]
letter_combos.append(word)
letter_combos = list(dict.fromkeys(letter_combos))
final_list = []
for word in letter_combos:
if self.check_word(word, all_words_list):
final_list.append(word)
final_list.sort(key=lambda s: len(s))
final_list = final_list[::-1]
return final_list
def anagrams(self, letters):
letters = letters.upper()
letters_array = []
words_array = []
for letter in letters:
letters_array.append(letter)
for each in range(3, len(letters_array) + 1):
for subset in combinations(letters_array, each):
for perm in permutations(subset):
words_array.append(''.join(perm))
all_words_list = self.gen_words()
final_list = []
for word in words_array:
if self.check_word(word, all_words_list):
final_list.append(word)
final_list = list(dict.fromkeys(final_list))[::-1]
return final_list
def test_class(test_string):
test_game = GamePigeon()
return test_game.word_hunt(test_string)
print(test_class('siprnhnctaeilseo'))