# Binary search## You start in the middle of the list, ...# then you search the second half.import os
words = []
file = os.path.dirname(__file__) + "/words.txt"for line in open(file):
word = line.strip()
words.append(word)
deffull_search(words, keyword):
i = 0for word in words:
i = i + 1if (word == keyword):
return i
return i
defbinary_search(words, keyword, i=0):
i = i + 1
key = len(words) // 2if words[key] == keyword:
return i
if keyword < words[key]: #Look Here
words = words[:key]
else:
words = words[key+1:]
i = binary_search(words, keyword, i)
return i
print(len(words)) # 113783
print(full_search(words, "mother")) # 62889
print(binary_search(words, "mother")) # 16
Double letters
Get the words with three consecutive double letters
# Get the words with three consecutive double lettersimport os
defhas_three_doubles(word):
i = 0
jj = 0while i < len(word) -1:
if word[i] == word[i+1]:
jj = jj + 1if jj == 3:
returnTrue
i = i + 2else:
jj = 0
i = i + 1returnFalseassert has_three_doubles("aabbcc") == Trueassert has_three_doubles("Mississippi") == False
file = os.path.dirname(__file__) + "/words.txt"for line in open(file):
word = line.strip()
if has_three_doubles(word):
print(word)
# bookkeeper# bookkeepers# bookkeeping# bookkeepings