-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlistofwords.py
More file actions
25 lines (18 loc) · 1.13 KB
/
listofwords.py
File metadata and controls
25 lines (18 loc) · 1.13 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
wordfile = open("words.txt")
wordset = {word.strip() for word in wordfile.readlines()}
def isListOfWords(s):
#write a O(N^2) algorithm to determine whether the string can be broken into a list of words
#You can start by writing an exponential algorithm and then using dynamic programming to
# improve the runtime complexity
TheDict = [False] * (len(s) + 1) #Creates a dictionary and sets everything to false
TheDict[0] = True
for i in range(len(s)): #one for loop which would take O(n) time
for j in range(i, len(s)): #second for loop taking O(n) time
if TheDict[i] and s[i: j+1] in wordset: #checks if the substring is in the wordset and if the substring is already checked
TheDict[j+1] = True
return TheDict[-1]
assert(isListOfWords("alistofwords"))
assert(isListOfWords("anotherlistofwords"))
assert(isListOfWords("stableapathydropon"))
assert(not isListOfWords("retouchesreissueshockshopbedrollsunspotassailsinstilledintersectionpipitreappointx"))
assert(not isListOfWords("xretouchesreissueshockshopbedrollsunspotassailsinstilledintersectionpipitreappoint"))