Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Question: You Must Implement The Function Get_anagrams(Letter_pool), Which Takes In A String Of Uppercase You must implement the function get_anagrams(letter_pool), which takes in a string

Question:You Must Implement The Function Get_anagrams(Letter_pool), Which Takes In A String Of Uppercase

You must implement the function get_anagrams(letter_pool), which takes in a string of uppercase letters letter_pool, representing the letters you have to rearrange. get_anagrams should return asetof all possible strings, where each string contains two uppercase English words. that when combined use exactly the combination of letters present in letter_pool (including the correct quantity of each letter). The two words should be separated by a space.

Note that this does include both possible orderings of those words, since the first one is the username and the second is the password, so flipping them does represent a different possible combination. See the examples below.

You must be able to run all of the examples below in under 10 seconds, so think about how to do this at least somewhat efficiently.

Hints:

  • Generating every possible permutation of the input string might seem like a good idea at first, but think about how that will scale as the number of letters in the string increases.
  • For a similar reason, avoid any structure that would require a nested loop through every combination of two words, so you definitely want to only loop through them once.
  • Consider just looking at every word in scrabble.txt and generating a list of only the words that are composed of some subset of the letters in the letter pool.
  • Then you can consider possible pairings of this much smaller list of words.
  • You can turn other collections (like lists and strings) into sets using the set() function. Note that sets automatically remove any duplicate elements.
  • The .count method can be used to determine the number of times a given letter occurs within a string.

get_anagrams('NOTHPY')

{'NOY PHT', 'PHOT NY', 'YON PHT', 'ONY PHT', 'TON HYP', 'PHT NOY', 'TOPH NY', 'PHT ONY', 'HYP NOT', 'NY PHOT', 'PHT YON', 'HYP TON', 'NOT HYP', 'NY TOPH'}

get_anagrams('TORCHWOOD')

{'HOOT CROWD', 'DOW COHORT', 'TORCH WOOD', 'WOOD ROTCH', 'HOTROD COW', 'DOCO THROW', 'DOCO WORTH', 'CORD WHOOT', 'COW HOTROD', 'DOCTOR WHO', 'DOCTOR HOW', 'WOOD TORCH', 'WOOT CHORD', 'WROTH DOCO', 'HOW DOCTOR', 'ROTCH WOOD', 'WHOOT CORD', 'THROW DOCO', 'CROWD HOOT', 'CROWD TOHO', 'DOCO WHORT', 'WHORT DOCO', 'WHO DOCTOR', 'TOHO CROWD', 'CHORD WOOT', 'WORTH DOCO', 'COHORT DOW', 'DOCO ROWTH', 'ROWTH DOCO', 'DOCO WROTH'}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

App Inventor

Authors: David Wolber, Hal Abelson

1st Edition

1449397484, 9781449397487

More Books

Students also viewed these Programming questions