As a puzzle we were asked to bring out the possibilities in breaking a 4 letter wordlock combination. It was to be a four letter word from the English dictionary. There were 10 possible characters in each place. So \$10^4\$ possibilities of random words. I brute forced my code to return a list of possible words by first reading a file of 300k English words and storing it in a list. Then I applied a filter at each place to narrow down the list of possible words that could work on the combo.
I know I am brute forcing the code down to 1089 possibilities, did I overlook other possibilities? I am a beginner and if I wanted to learn how to improve the efficiency of my code and make it simpler/functioning, what steps can I take?
filterlist2 = ['a','r','t','h','i','v','o','y','l','e',]
filterlist3 = ['a','l','t','o','i','n','s','r','m','f',]
filterlist4 = ['a','m','d','k','e','s','x','p','l','y',]
#filterlist1 basically before I realized I can store parameters in a list
lines = tuple(open("words.txt", 'r'))
char1filter=[]
for element in lines:
if len(element)==5:
if element.startswith('b') is True:
char1filter.append(element)
elif element.startswith('p') is True:
char1filter.append(element)
elif element.startswith('t') is True:
char1filter.append(element)
elif element.startswith('s') is True:
char1filter.append(element)
elif element.startswith('m') is True:
char1filter.append(element)
elif element.startswith('d') is True:
char1filter.append(element)
elif element.startswith('c') is True:
char1filter.append(element)
elif element.startswith('g') is True:
char1filter.append(element)
elif element.startswith('f') is True:
char1filter.append(element)
elif element.startswith('l') is True:
char1filter.append(element)
#print(char1filter)
print(len(char1filter))
#returned 3935 results
#filter 2 for second character
char2filter =[]
for element1 in char1filter:
if element1[1] in filterlist2:
char2filter.append(element1)
print('char2 filter applied')
print(char2filter)
print(len(char2filter))
#filter 2 returned 3260 words
#filter 3
char3filter =[]
for element2 in char2filter:
if element2[2] in filterlist3:
char3filter.append(element2)
print(char3filter)
print(len(char3filter))
#filter 3 returned 1991 words
#filter 4
char4filter =[]
for element3 in char3filter:
if element3[3] in filterlist4:
char4filter.append(element3)
print(char4filter)
print(len(char4filter))
#filter 4 returned 1089 words
if/else
chain. I wrote this function to handle all of those cases at once. \$\endgroup\$element[0] in "bptsmdcgfl"
\$\endgroup\$is True
to a boolean condition is useless. In fact sometimes the condition doesn't return abool
value, and in such case addingis True
breaks the condition. \$\endgroup\$is
does a strict equality check looking at both the value and type. It's kind of (but not exactly) like JavaScript's===
operator \$\endgroup\$