-
Notifications
You must be signed in to change notification settings - Fork 1
/
5055278_linkedwords.py
60 lines (50 loc) · 1.44 KB
/
5055278_linkedwords.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
from depthFirstSearch import DepthFirstSearch
from backtracking import Backtracking
from collections import defaultdict
import time
from random import randint
import matplotlib.pyplot as plt
import sys
"""
Extract words from the dictionary of the given length.
Then find sequences of linked words
Store the process time and the sequence length
Find the longest sequence for each word length
"""
"""
Algorithms:
Backtracking
Depth first search
"""
file = open("dictionary.txt")
allWords = file.read().splitlines()
numberOfWords = len(allWords)
def extractLetters(i):
words = []
frontLetters = defaultdict(list)
endLetters = defaultdict(list)
k = 0
for j in range(numberOfWords - 1):
if len(allWords[j]) == i:
frontLetters[allWords[j][1:3]].append(k)
endLetters[allWords[j][-3:-1]].append(k)
words.append(allWords[j])
k += 1
return frontLetters, endLetters, words
# Order the list of words in terms of the number of
def main():
# grab word length
i = int(sys.argv[1][0])
# start timer
start = time.time()
# Get relevant words
frontLetters, endLetters, words = extractLetters(i)
wordCount = len(words)
#DFS = DepthFirstSearch(words, wordCount)
#solution = DFS.run()
BT = Backtracking(frontLetters, endLetters, words, wordCount)
solution = BT.search()
end = time.time()
print("Time: ", end-start)
main()
file.close()