Using python 3.5 or later, write the following program.
A kidnapper kidnaps Baron Barton and writes a ransom note. It is not wrriten by hand to avoid having his hand writing being recognized, so the kid napper uses a magazine to create a ransom note. We need to find out, given the ransom note string and magazine string, is it possible to given ransom note. The kidnapper can use individual characters of words.
Here is how your program should work to simulate the ransom note problem:
your program should prompt the user to enter a long string to represent the magazine article and another short string to represent the ransom note.
your program needs to check if the magazine string contains all required characters in equal or greater number present in the ransom note.
your program should print true if it is possible to create the given ransom note from the given magazine article, and print false if it does not.
Break up your code into a set of well-defined functions. Each function should include a comment block that briefly describes it, its parameters and any return values.
Example: If the magazine string is "welcome to earth, help is needed everywhere you look"
If the ransom note is "I need money" your program should print false as not all the characters in “I need money” exist in the magazine string.
If the ransom note is "real help is not here" you program should print true since all characters in "real help is not here" exists in the magazine string.
If ransom note is "help help help" program should print false, all characters are in the magazine string but not enough to make multiple words of the same word.
We need to check if magazine string contains all characters and in equal or greater number, present in ransom note.
How can we keep track which characters are present and how many instances of characters are present in magazine string?
1. Create a dictionary with keys and values with individual alphabet frequency of ransom_note where key is character, and value as the number of instances it occurs.
2. Now go through each character in magazine_text, and decrement corresponding value in Dictionary. If we try to decrease a value already zero, we return false. If we reach at the end of ransom note, we return true.
Now go through each character in ransom note, and decrement corresponding value in hash table. If we find the frequcncies of all the characters of ransom note. we return true. otherwise false.
Code ransom note problem
import collections
magazine_string = raw_input("Enter the magazine string:\t")
ransom_note = raw_input("Enter the ransom note:\t")
letters = collections.defaultdict(int)
def ransonNoteMake(magazine_string,ransom_note):
for letter in ransom_note:
letters[letter] += 1
print letters
for r in magazine_string:
letters[r]=letters[r]-1
print letters
for key in letters:
if letters[key]<=0:
continue
else:
return 'false'
break
return 'true'
print ransonNoteMake(magazine_string,ransom_note)
Get Answers For Free
Most questions answered within 1 hours.