I need to call various different functions that i have already created in order to achieve the question below. I am really unsure how to programme this in order to achieve it. The question is..
Find two word anagrams in word list( str, str list ), where the input parameters should be a string and a list of strings. The output should be a list of all strings made up of two words separated by a space, such that both of these words are in str list and the combination of the two words is an anagram of str.
The output i expect is:
wordlist = ('and','band','nor,'born')
find_two_word_anagrams_in_wordlist( "brandon", wordlist )
[’and born’, ’band nor’]
How to achieve this is:
- Initialise a list two word anagrams to the empty list, [].
- Call find partial anagrams in word list to get a list of all the partial anagrams of str that can be found in the word list.
- Then, do a loop that runs over all these partial anagrams. For each partial anagram part anag do the following:
- Remove the letters of part anag from the input string, str, to get a string, rem that is the remaining letters after taking away the partial anagram;
- Call find anagrams in word list on rem (and the input word list) to get a list of anagrams of the remaining letters;
- For each anagram rem anag of the remaining letters, form the string part anag + " " + rem anag and add it to the list two word anagrams.
- At this point the list two word anagrams should have all the two word anagrams in it, so return that list from your function.
Code i have already created is:
def find_partial_anagrams_in_word_list(str1,str_list):
partial_anagrams = []
for word in str_list:
if (partial_anagram(word, str1)):
partial_anagrams.append(word)
print(partial_anagrams)
def remove_letters(str1,str2):
str2_list = list(str2)
for char in str2:
if char in str1:
str2_list.remove(char)
return "".join(str2_list)
def find_anagrams_in_word_list(str1,str_list):
anagrams = []
for word in str_list:
if (anagram(str1,word)):
anagrams.append(word)
print(word)
Any step by step help or input would be appreciated.