-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLongest Word Finder.py
124 lines (100 loc) · 3.45 KB
/
Longest 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
121
122
123
124
import csv
import itertools
import time
import os
import msvcrt
def get_file_selection(files):
current_row = 0
while True:
os.system("cls")
print("Select your dictionary (use arrow keys):")
for i, file in enumerate(files):
if i == current_row:
print("->", file)
else:
print(" ", file)
key = msvcrt.getch()
if key == b"\xe0": # arrow key prefix
key = msvcrt.getch()
if key == b"H" and current_row > 0: # up arrow
current_row -= 1
elif key == b"P" and current_row < len(files) - 1: # down arrow
current_row += 1
elif key == b"\r": # Enter key
return files[current_row]
def csv_to_dict(filename):
result_dict = {}
with open(filename, "r") as file:
reader = csv.reader(file)
for row in reader:
word, definition = row
result_dict[word] = definition
return result_dict
def txt_to_list(filename):
with open(filename, "r") as f:
words = [line.strip() for line in f]
return set(words)
# Get and read dictionary file
files = [file for file in os.listdir() if file.endswith((".txt", ".csv"))]
filename = get_file_selection(files)
print(f"Reading dictionary... | file: {filename}")
dictionary = None
dictionary_with_definition = False
if filename.split(".")[-1] == "csv":
dictionary = csv_to_dict(filename)
dictionary_with_definition = True
else:
dictionary = txt_to_list(filename)
print("Reading dictionary completed!")
def possible_words(letters):
possible_words = set()
letter_count = len(letters)
letters = sorted(letters)
while letter_count > 0:
for word_combination in itertools.permutations(letters, letter_count):
word_combination = "".join(word_combination)
if word_combination in dictionary:
possible_words.add(word_combination)
letter_count -= 1
return possible_words
def longest_words(letters):
print("Processing...")
words = possible_words(letters.lower())
word_count = len(words)
if word_count > 0:
words = sorted(words, key=len)
longest_word = words[-1]
longest_len = len(longest_word)
words.remove(longest_word)
same_len_words = [item for item in words if len(item) == longest_len]
print("Total number of possible words: ", word_count)
print("Longest word length: ", len(longest_word))
print("- Longest Word: ", longest_word)
if dictionary_with_definition:
print("Definition: ", dictionary[longest_word])
if same_len_words:
print(f"Other {longest_len} letter words")
if dictionary_with_definition:
for word in same_len_words:
print(f"- {word}: {dictionary[word]}")
else:
print("\n".join(map(lambda x: "- " + x, same_len_words)))
else:
print("No possible word found in dictionary.")
def program():
letters = input("\nEnter letters: ")
start = time.time()
longest_words(letters)
end = time.time()
print("Processing completed!")
print(f"Time taken: {end-start} seconds ")
again()
def again():
check_again = input("\nDo you want to run again? [Y/N]")
if check_again.upper() == "Y":
program()
elif check_again.upper() == "N":
print("See you later.")
else:
again()
program()