How can I get this anagram finder to find every single possible anogram (word or not) that is possible. Python

34 Views Asked by At

I am trying to find all anagrams of a string of text (english word or not). For the most part I am very pleased with my code, but it does not do a good job of finding all of the actual english words for the anagrams for elbow, (which are bowel, below, and elbow) no matter what I do it only finds two of them. Upon further notice, it is not generating all of the possibilities, is there anything I am overlooking? Thank you so much.

OH and this is a challenge not to use any imports or anything, just pure python, I know it is possible!


# Finds and creates a list of all possible anograms of a string of letters.

def anogramizer(word):
    """Creates and returns a list of all possible anograms from a provided word or string of letters."""
    list = __letterArray(word)
    if len(list) == 1:
        # If there is just one letter, return the letter.
        return [word]
    
    else:

        answers = []

        size = len(list)

        # Create a copy of the main list, so that we may not change it accidentally:
        workingList = list.copy()

        # Iterates through each letter in the word, but does not need to use "j":
        for j in range(size):

            # Pop the first Item from workingList.
            ltr = workingList.pop(0)

            for i in range(size):
                # Create a third list copied from the workingList which has all items but the one that is popped:
                sWList = workingList.copy()
                sWList.insert(i, ltr)
                
                # Combine the list of letters into a string of a word:
                newWord = __wordForm(sWList)

                # Check if newWord is already a discovered answer:

                if (newWord not in answers) and len(newWord) == len(word):

                    # Append the created anogram to the answers list:
                    answers.append(newWord)

            # Re-Add the popped item back to the list to reset the workingList for the next iteration of J but with the popped at the end instead.
            workingList.append(ltr)

        return answers




def __letterArray(letters):
    list = []
    for i in letters:

        list.append(i)

    return list

def __wordForm(list):
    # Forms a list of letters into a word by adding them all together.
    word = ""
    for i in list:
        word = word + i

    return word


if __name__ == "__main__":

    words = anogramizer("abcd")
    print(words)```
0

There are 0 best solutions below