I am trying to write a function that checks if I can generate the required word/phrase using the characters provided. The phrase created can contain any characters including special characters, capital letters, numbers, and spaces.
I can generate the phrase if the frequency of unique characters in the characters string is equal or greater than the frequency in the phrase.
What I tried to do was use a counter and then compare the dictionaries that it produced but not sure how to find if the characters in the character string are more in frequency than in the phrase.
This is my code:
from collections import Counter
def generate_phrase(characters, phrase):
new_characters = characters.lower()
new_phrase = phrase.lower()
a = Counter(new_characters)
b = Counter(new_phrase)
if a == b:
return True
else:
return False
This example should produce false, as there are less characters in the character string than the phrase
characters = "cbacba"
phrase = "aabbccc"
generate_phrase(characters, phrase)
Ouput:
False
This example should produce True as although it has other letters in it, that are not in the phrase. It does contain all characters of the phrase and the right frequency of them.
characters = "Magiciansktb!"
phrase = "m!aagsnici"
generate_phrase(characters, phrase)
Ideal Ouput:
True
My current code output:
False