Question

Using python 3.5 or later, write the following program. A kidnapper kidnaps Baron Barton and writes...

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.

Homework Answers

Answer #1

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)

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
I need python code for this. Write a program that inputs a text file. The program...
I need python code for this. Write a program that inputs a text file. The program should print the unique words in the file in alphabetical order. Uppercase words should take precedence over lowercase words. For example, 'Z' comes before 'a'. The input file can contain one or more sentences, or be a multiline list of words. An example input file is shown below: example.txt the quick brown fox jumps over the lazy dog An example of the program's output...
** Language Used : Python ** PART 2 : Create a list of unique words This...
** Language Used : Python ** PART 2 : Create a list of unique words This part of the project involves creating a function that will manage a List of unique strings. The function is passed a string and a list as arguments. It passes a list back. The function to add a word to a List if word does not exist in the List. If the word does exist in the List, the function does nothing. Create a test...
Need in Java coding Write a program that will allow users to enter different types of...
Need in Java coding Write a program that will allow users to enter different types of books into the program, and at the end print out everything that the user entered. Allow the user to enter as many books as they want. Please collect the following information and break down the classes as follows using inheritance/classes. I have added the extra properties that each class should include. Book types: Paper Book (nothing extra) Comic Book (issue number) Magazine (publisher, issue...
JAVA Exercise_Pick a Card Write a program that simulates the action of picking of a card...
JAVA Exercise_Pick a Card Write a program that simulates the action of picking of a card from a deck of 52 cards. Your program will display the rank and suit of the card, such as “King of Diamonds” or “Ten of Clubs”. You will need: • To generate a random number between 0 and 51 • To define two String variables: a. The first String should be defined for the card Suit (“Spades”, “Hearts”, “Diamonds”, “Clubs”) b. The second String...
In Python: This will require you to write several functions, and then use them in a...
In Python: This will require you to write several functions, and then use them in a program. Logical Calculator The logical calculator does math, but with logical operators. In logic, we represent a bit with 0 as false and a bit with 1 as true. The logical operators are NOT, AND and OR. Bitwise logical calculations operate on each bit of the input. The NOT operator works on just one three-bit argument. NOT 011 = 100 The AND operator works...
Using Python, write the following code. You are to allow the user to enter the daily...
Using Python, write the following code. You are to allow the user to enter the daily temperature as a floating-point number. You should make the assumption that you are recording temperatures in Fahrenheit. You should allow the user to continue entering temperatures until the value -999 is entered. This number should not be considered a temperature, but just a flag to stop the program. As the user enters a temperature, you should display the following each time: Current Temperature: 999...
Using c++ Valid Palindrome In this assignment, you need to implement a bool isPalindrome(string s) function....
Using c++ Valid Palindrome In this assignment, you need to implement a bool isPalindrome(string s) function. Given a string s, isPalindrome(s) can determine if s is a palindrome, considering only alphanumeric characters and ignoring cases. Note: for the purpose of this problem, we define empty string as valid palindrome. Example 1: Input: ”A man, a plan, a canal: Panama” Output: true Example 2: Input: ”race a car” Output: false Requirement: There are many methods to check if a string is...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total 52 case sensitive) and five special characters (‘.’, ‘,’, ‘:’, ‘;’ and ‘!’) in all the .txt files under a given directory. The program should include a header count.h, alphabetcount.c to count the frequency of alphabet letters; and specialcharcount.c to count the frequency of special characters. Please only add code to where it says //ADDCODEHERE and keep function names the same. I have also...
write a code in python Write the following functions below based on their comments. Note Pass...
write a code in python Write the following functions below based on their comments. Note Pass is a key word you can use to have a function the does not do anything. You are only allowed to use what was discussed in the lectures, labs and assignments, and there is no need to import any libraries. #!/usr/bin/python3 #(1 Mark) This function will take in a string of digits and check to see if all the digits in the string are...
---------------- Overview: ---------------- This lab will consist of several independent exercises that must all use recursion...
---------------- Overview: ---------------- This lab will consist of several independent exercises that must all use recursion to solve various problems. Some problems will have fairly short solutions, but declaring and using classes/objects is still required. Some could benefit from having some kind of data structure being passed between calls. Feel free to use your LinkedList if you think it could help. Some solutions will be a mix of iteration and recursion, but all solution need to be recursive in some...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT