rlvaugh / Impractical_Python_Projects

Code & supporting files for chapters in book
401 stars 269 forks source link

Chapter 3 - Project 5 - find_anagrams function #2

Closed dwanneruchi closed 5 years ago

dwanneruchi commented 5 years ago

@rlvaugh this book is great! Really enjoying it!

It looks like find_anagrams() is only able to produce anagrams composed of unique letters (i.e. n letters with n distinct letters).

For example, my name is david. When I run this function the word "dad" is not returned as a possible anagram. I believe this is because of line 28-31 (below):

        for letter in word:
            if word_letter_map[letter] <= name_letter_map[letter]:
                test += letter
        if Counter(test) == word_letter_map:
            anagrams.append(word)

The test string only receives a singular letter, rather than the value attached to the letter, making it impossible to have an anagram where a single letter appears more than once in the word. Once we get to the second if statement, we only have a string (test) comprised of letters that all have a frequency of 1, limiting the words we can utilize.

Was this an intentional decision or am I possibly misinterpreting something?

I updated the function with the following logic (seems to work, but curious if you see an error):

        for letter in word:
            if word_letter_map[letter] <= name_letter_map[letter]:
                 test += letter*word_letter_map[letter]
        if Counter(test) == word_letter_map:
            anagrams.append(word)

Now it will add a letter to the string 'test', but will add that letter * the number of times it exists in the word of interest. On some basic testing it seems to work, but I am curious if I am overlooking something here. Do you think this is a good modification?

rlvaugh commented 5 years ago

Hi David: Thank you for the feedback!  I'll have more time to look at this tomorrow but I quickly ran the program and I was able to find "dad" in "David": == RESTART: C:\Python35\Python 3 Stuff\Anagrams\for_book\phrase_anagrams.py == Enter a name: david Length of anagram phrase = 0 a ad add aid avid dad did diva i id viaRemaining letters = david Number of remaining letters = 5 Number of remaining (real word)anagrams = 11 Current anagram phrase = 

If you've downloaded the book's code from https://nostarch.com/impracticalpythonprojects, could you run the phrase_anagrams.py program found there?  When the publisher moved my manuscript from Word to InDesign, it messed up some of the code printed in the book, especially indents.   For other projects, please check the "Update" section on the website above for the current errata. I may be misreading your note; I'm short for time right now.  Let me know if this doesn't help. Regards,

Lee 

On ‎Sunday‎, ‎February‎ ‎17‎, ‎2019‎ ‎08‎:‎55‎:‎56‎ ‎PM‎ ‎CST, dwanneruchi <notifications@github.com> wrote:  

@rlvaugh this book is great! Really enjoying it!

It looks like find_anagrams() is only able to produce anagrams composed of unique letters (i.e. n letters with n distinct letters).

For example, my name is david. When I run this function the word "dad" is not returned as a possible anagram. I believe this is because of line 28-31 (below):

python for letter in word: if word_letter_map[letter] <= name_letter_map[letter]: test += letter if Counter(test) == word_letter_map: anagrams.append(word)

The test string only receives a singular letter, rather than the value attached to the letter, making it impossible to have an anagram where a single letter appears more than once in the word. Once we get to the second if statement, we only have a string (test) comprised of letters that all have a frequency of 1, limiting the words we can utilize.

Was this an intentional decision or am I possibly misinterpreting something?

I updated the function with the following logic (seems to work, but curious if you see an error):

python for letter in word: if word_letter_map[letter] <= name_letter_map[letter]: test += letterword_letter_map[letter] if Counter(test) == word_letter_map: anagrams.append(word) Now it will add a letter to the string 'test', but will add that letter the number of times it exists in the word of interest. On some basic testing it seems to work, but I am curious if I am overlooking something here.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

dwanneruchi commented 5 years ago

@rlvaugh you are correct, I must have missed something during my initial testing - thanks for the quick reply! I will close this out.

rlvaugh commented 5 years ago

Great! The next project is Finding Voldemort.  This is probably my favorite.  When I saw the movie of The Chamber of Secrets, and Riddle waved his wand and reshuffled all the letters in his name, I wondered how J.K. Rowling figured that out.  I'm terrible at anagrams, so I wrote the Python code to figure it out.  It was a lot easier than I expected, and very satisfying! Lee 

On ‎Monday‎, ‎February‎ ‎18‎, ‎2019‎ ‎09‎:‎15‎:‎25‎ ‎AM‎ ‎CST, dwanneruchi <notifications@github.com> wrote:  

@rlvaugh you are correct, I must have missed something during my initial testing - thanks for the quick reply! I will close this out.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

dwanneruchi commented 5 years ago

Great inspiration - I was a big HP fan growing up, so I love the theme for this! I too am horrible at anagrams, but my wife is a champ. Looking forward to learning how to write a program to compete with her 👍