-
Notifications
You must be signed in to change notification settings - Fork 0
/
WordCountdown_V2.py
73 lines (60 loc) · 2.58 KB
/
WordCountdown_V2.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
import itertools
from collections import defaultdict
import random
import time
print("====================================")
print("Proccessing...")
#initializing vowels and consonants
vowel = list("aeiou")
consonant = list("bcdfghjklmnpqrstvwxyz")
start = time.time()
# method that generates vowels and consonants
def vowels_consonants(vow, cons):
vowels = [letter for letter in vow if letter in vow]
consonants = [c for c in cons if c in cons]
randConsonant = random.sample(consonants, 4)
randVowel = random.sample(vowels, 5)
newstring = randConsonant + randVowel
vowelsconsonantsword = "".join(newstring)
return vowelsconsonantsword
# read from file and and populate the dictionary
words = defaultdict(list)
file = open('dictionary2.txt','r')
f = file.readlines()
for word in f:
word=word.strip()
words[''.join(sorted(word))].append(word)
# searching for the longest anagram, get the vowels and consonants and check against the words in dictionary
def generate_longest_word():
outputlist = []
inputWord = vowels_consonants(vowel, consonant) #assign generated vowels and consonants to variable inputWord
print("Randomly Generated letters are: ", inputWord)
length_of_word = len(inputWord)
#sort the vowels and consonants
inputWord = sorted(inputWord)
while length_of_word > 0:
# generate possible combinations of anagrams based on the length_of_word
#itertools combinations will maintain the sort order of the letters
for wordcombination in itertools.combinations(inputWord, length_of_word):
result = ''.join(wordcombination)
if result in words:
#returns list that contains all possible generated anagrams that match any of the engilsh words in the dictionary
outputlist= words[result]
return outputlist
length_of_word = length_of_word - 1
found = False
#this function will sort the list of anagrams in ascending order and prints out the longest word from the sorted list
def print_longestWords():
list = generate_longest_word()
#check to see if the matched_word list is not empty
if(list is not None):
outputresult = sorted(list, key=len) #this builds a new sorted list of anagrams from an iterable.
print("Total Number of Anagrams: ",len(list))
print("Longest Word: ",outputresult[-1])
found = True
else:
print("No Longest Word Found...Please try again")
print_longestWords()
end = time.time()
print("Processing Finished...")
print("Time taken... %s seconds " % (end - start))