-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrambleHelper.py
More file actions
59 lines (48 loc) · 1.38 KB
/
scrambleHelper.py
File metadata and controls
59 lines (48 loc) · 1.38 KB
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
import os
import json
import re
from itertools import permutations
#initialize a word dictionary
words = {}
#function to search for words based on the clue
def search_word(clue):
perms = {''.join(p) for p in permutations(clue)}
results = []
print(sorted(perms))
for perm in perms:
if perm in words.keys():
results.append(perm)
return results
#load the words from the json file
filepath = os.path.join(os.path.dirname(__file__), 'templates\words.json')
#check if the file exists and is a valid json file
try:
with open(filepath) as f:
words = json.load(f)
except FileNotFoundError as e:
print(f'File not found: {filepath}')
except json.JSONDecodeError as e:
print(f'Invalid JSON file: {e}')
#function to get a valid clue from the user
def getvalidclue():
while True:
clue = input('Enter the word clue or Type "exit" to exit: ')
if clue == '':
print('Please enter a clue.')
continue
elif clue.lower()=='exit':
print('Exiting...')
break
return clue
return None
#main code
clue = getvalidclue()
#check if a valid clue was entered
if clue is not None:
results = search_word(clue)
if results:
print(f'Results:\n{results}')
else:
print('No results found.')
else:
print('No search performed.')