0
I have a program called anagrams.py
and its function is to show the permutations of a word typed by the user if it is in the file words.txt
.
This is the complete code:
""" Anagrams by WhoisBsa """
from itertools import permutations
import sys
def findPermutation(wrd):
""" Find the permutation of the words """
parmutationList = permutations(wrd)
for item in parmutationList:
print(''.join(item))
def checkWord(wrd, wordLine):
""" Checks whether the word exists in the file or not """
while True:
for i in wordLine.readlines():
if wrd in i:
result = True
break
else:
result = False
if result:
findPermutation(wrd)
break
else:
print('This word is not available')
break
def main():
""" Main function """
with open('words.txt', 'r') as f:
word = sys.argv[1].upper()
checkWord(word, f)
f.close()
if __name__ == '__main__':
main()
This code runs perfectly with one exception: in function check checkWord
the condition
if wrd in i:
look for any word that satisfies the condition, ie if I type the word house
and run the code, the intention is that the return (if it is in the file) is all permutations of the word house, however, in the file there is the word blockhouse
, and with that the program checks if there is house in the file and when it finds the word Blockhouse it stops and makes the permutations of the word house.
I know the condition if wrd in i
will validate whichever word that satisfies the condition. The point is that if I use
if wrd == i:
the program doesn’t work, it goes straight to else
.
Another problem is that if I type Hou the program will find Blockhouse and make the anagram of the word Hou and the same does not exist.
I thought I’d use the method regex
to validate this but I do not know how to apply this code.
It is a little confused what you want, see that house is not anagram (and neither permutation) of Blockhouse and nor vice versa. If the user types House and in the file has Blockhouse and no house, then the word is not in the file, unless you want to search for substrings inside the strings in the file (or maybe both: anagrams and substrings), then the statement (and the code) would be other.
– Sidon
@Sidon that’s the problem, the file has no house but has Lockhouse, I want to return that there is no house in the file but this does not happen, and sees that there is the word Blockhouse and then does the permutation of the word house because in the code the permutationList receives the permutations of the variable word:
permutationList = permutations(wrd)
– WhoisMatt
Let me get this straight: If the user types house, you have to search for house and all the house anagrams in the file, it’s?
– Sidon
Tip that has nothing to do with the question: you do not need (nor should, but only for legibility) to call the
.close
of a file used thewith
to open it. Thewith
already closes the file.– jsbueno
@Sidon if I type house I will search for house and if there is house in the file I do the anagrams of the word house :)
– WhoisMatt
@jsbueno thanks for the tip! :)
– WhoisMatt
Okay, see if my answer helps.
– Sidon