3

I have this code:

mylist = open('sortedwords.txt')
txt = mylist.read()
mylist = txt.split()
stuff = input('Type a word here: ')

def removeletters (word, Analysis):
    for char in range (len(Analysis)):
        if Analysis [char] in word:
             word = word.replace(Analysis[char],"",1)
    return word

def anagramSubset(word, textList):
    newWord = word
    for char in range(len(textList)):
        if textList[char] not in newWord: 
            return False
        else:
           newWord = newWord.replace(textList[char],"",1)
    return True

def anagram(word, textList):
    savedWords =[]
    for checkword in textList:  
        if len(word) == len(checkword) and anagramSubset(word, checkword):
            savedWords.append(checkword)
            print(checkword)
                    
anagram(stuff, mylist)

It is supposed to take an input word, remove letters from the input word, then make a subset of words and save that to an array to print off of.

The problem is that the code will save every word that can be created from the input. E.g. an input of spot results in top, tops, stop, pots, pot, etc. The result should only have tops, pots, and stop.

What is wrong with the code, and how do I fix it?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
user3040579
  • 31
  • 1
  • 3
  • What is the purpose of `sortedwords.txt`, and of `textlist` in the `anagram` method? What do you mean by "remove a letter from the inputted word"? – slider Nov 27 '13 at 07:31
  • "What the code is supposed to do is take an inputted word, remove letters from the inputted word" Remove *which* words, according to *what logic*? "That would be fine if it only printed tops, pots, and stop." I can't understand. The apparent expected output is just the anagrams of `spot`. But then - what's all this about removing letters? – Karl Knechtel Aug 01 '22 at 22:18

1 Answers1

1

I looked at the code and am wondering what the recursion is adding? The first pass does all of the computational work and then the recursion adds some extra stack frames and alters how output is printed. Am I making the wrong assumption that textList is a list of valid words split from a single line in a file?

When I run this locally with a particular word list, this gets the same effect (in the sense that it finds words whose letters are a subset) with less thrashing:

def anagram(word, textList):
    savedWords = []
    for checkword in textList:
        if anagramSubset(word, checkword):
            savedWords.append(checkword)
    print(savedWords)

If the problem eventually becomes that you're getting words that have too few letters, you could fix your problem by checking that a word is the length of the original word before you add it with:

if len(original_word) == len(checkword):
    savedWords.append(checkword)
Tim Wilder
  • 1,607
  • 1
  • 18
  • 26
  • You are right about the textList. Mentors don't really give me answers. Just point me in the right direction. They have many people they have to try to help in a small time. I will look up the documentation for .append() I hope it works. I have tried to fix this for 4 hours. I am getting errors where savedwords is no longer defined. How should I define it? – user3040579 Nov 27 '13 at 08:41
  • I wouldn't look up the append documentation. You are using that correctly! I would run a check before append and make sure you're not appending words shorter than the length of your original word. – Tim Wilder Nov 27 '13 at 08:42
  • I am getting errors where savedwords is no longer defined. How should I define it? – user3040579 Nov 27 '13 at 08:44
  • My example had a typo, fixed! – Tim Wilder Nov 27 '13 at 08:48
  • Yeah I figured it out. I am still getting words less than my input. Even with the if statement. – user3040579 Nov 27 '13 at 08:49
  • If the conditional has the right values, that shouldn't be happening. Perhaps you are appending to the list elsewhere, or the wrong value are being used in the test? – Tim Wilder Nov 27 '13 at 08:52
  • I'm just having a hard time with python and I am learning C++ at the same time. So things get rather mixed up for me. I am sorry. – user3040579 Nov 27 '13 at 08:55
  • No worries, so much new information can definitely be overwhelming sometimes. It comes into focus the more time you spend at it though. Good luck on this problem. – Tim Wilder Nov 27 '13 at 08:58
  • So you saw nothing wrong with how I edited my code. I feel like I should have used an else somewhere. – user3040579 Nov 27 '13 at 09:02
  • Your test should be something like: `if anagramSubset(word, checkword) and length(word) == length(checkword):`. Then append just once. The current code could append twice, which is not what you want. Returning the final list and printing the output of `anagram(stuff, textList)` is probably better as well, but that's splitting hairs. – Tim Wilder Nov 27 '13 at 09:04
  • Ill try it tomorrow. I've spent a total of 8 hours on this and half of it is just on this problem. Sometimes I wish things worked out. I hope I can get it done or I won't make it up to my family for Thanksgiving. This work is really important for me. You have to do what you have to do. You know. – user3040579 Nov 27 '13 at 09:15
  • Oh wow. I got it to work, but how do I omit the inputted word from showing in the output? – user3040579 Nov 27 '13 at 09:22
  • Another problem is that this code doesnt like the input of two words now – user3040579 Nov 27 '13 at 20:45
  • I can't make anagrams out of bigger words like geoffrey. – user3040579 Nov 27 '13 at 21:01