Question

In: Computer Science

Problem 1 (Ransom Note Problem) in python A kidnapper kidnaps you and writes a ransom note....

Problem 1 (Ransom Note Problem) in python

A kidnapper kidnaps you and writes a ransom note. He does not write it by hand to avoid having his hand writing being recognized, so he uses a magazine to create a ransom note. We need to find out, given the ransom string and magazine string, is it possible to create a given ransom note. The kidnapper can use individual characters of words.

Here is how your program should work to simulate the ransom problem:

  • your program should prompt the user to enter a long String to represent the magazine and another short String to represent the required 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, and print false otherwise.
  • 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 “programming problems are weird”

If the ransom note is: “no see” your program should print true as all the characters in “no see” exist in the magazine string.

If the ransom note is “no show” your program should print false as not all the characters in “no show” exist in the magazine string as you can see the character ‘h’ does not exist.

Solutions

Expert Solution

Program

def ransom_note(magazine, ransom):
    hash_words = {}

    # Create the hash tabled with the words on the
    # magazine and put the number of occurrence in the value.
    for m_word in magazine:
        if hash_words.get(m_word) != None:
            if (hash_words[m_word] > 0):
                hash_words[m_word] += 1
        else:
            hash_words[m_word] = 1

    # Check if exist the word in the hash table
    for r_word in ransom:
        if hash_words.get(r_word) is None or hash_words[r_word] == 0:
            return False
        else:
            hash_words[r_word] -= 1

    return True


m = "programming problems are weird"
n = "are"
magazine_list = m.split(' ')
ransom_list = n.split(' ')
answer = ransom_note(magazine_list, ransom_list)

if answer:
    print ("True")
else:
    print ("False")


Output

True

Screenshot


Related Solutions

Python Problem Problem 1: In this problem you are asked to write a Python program to...
Python Problem Problem 1: In this problem you are asked to write a Python program to find the greatest and smallest elements in the list. The user gives the size of the list and its elements (positive and negative integers) as the input. Sample Output: Enter size of the list: 7 Enter element: 2 Enter element: 3 Enter element: 4 Enter element: 6 Enter element: 8 Enter element: 10 Enter element: 12 Greatest element in the list is: 12 Smallest...
The question: Write a program in Python that writes four random integers in range 1-100 on...
The question: Write a program in Python that writes four random integers in range 1-100 on a file named 'num.txt'. Write try-except block to handle at least two standard python error (any two errors). Hints: import random def main(): # Local variables # Open output file. # Write random numbers to the file. # Write it on to the file. # Close the file. # Call the main function. main() Here is my answer: please let me know if anything...
Send this as a Python file. Note: this is an example where you have the original...
Send this as a Python file. Note: this is an example where you have the original file, and are writing to a temp file with the new information. Then, you remove the original file and rename the temp file to the original file name. Don't forget the import os statement.A file exist on the disk named students.txt The file contains several records and each record contains 2 fields :1. The student's name and 2: the student's score for final exam....
Use python. redact_file: This function takes a string filename. It writes a new file that has...
Use python. redact_file: This function takes a string filename. It writes a new file that has the same contents as the argument, except that all of the phone numbers are redacted. Assume that the filename has only one period in it. The new filename is the same as the original with '_redacted' added before the period. For instance, if the input filename were 'myfile.txt', the output filename would be 'myfile_redacted.txt'. Make sure you close your output file.
Please use Python 3 3). Write a function that writes a series of random numbers to...
Please use Python 3 3). Write a function that writes a series of random numbers to a text file named ‘random_number.txt’. Each random number should be in the range of 1 through 500. The function should let the user specify how many random numbers the file will hold. Then write another function that reads the random numbers from the ‘random_number.txt’ file, displays the numbers, and then displays the total of the numbers and the number of random numbers read from...
Problem 1 Part A (11 Marks) Note: It is suggested that you read all both parts...
Problem 1 Part A Note: It is suggested that you read all both parts of Problem 1 before you start so that you structure your space requirements for the answer appropriately. Several friends grouped themselves as shareholders and Emerald Delivery Limited opened for business September 1 2018. The transactions for the month are listed. You are to create the journal entries in good form for these transactions. Sept 12015 - Shareholders started the company with 20,000 of their own cash...
Problem 1: python For the first problem of this homework, we’re going to try something a...
Problem 1: python For the first problem of this homework, we’re going to try something a little different. I’ve created the start of a file, which you’ll edit to finish the assignment: count_words_in_the_raven.py The program has three functions in it. I’ve written all of break_into_list_of_words()--DO NOT CHANGE THIS ONE. All it does is break the very long poem into a list of individual words. Some of what it's doing will not make much sense to you until we get to...
PYTHON PYTHON Recursive Functions. In this problem, you are asked to write three recursive functions. Implement...
PYTHON PYTHON Recursive Functions. In this problem, you are asked to write three recursive functions. Implement all functions in a module called problem1.py. (10 points) Write a recursive function called remove char with two parameters: a string astr and a character ch. The function returns a string in which all occurrences of ch in astr are removed. For example, remove char("object oriented", ’e’) returns the string "objct orintd". Your implementation should not contain any loops and may use only the...
Seller writes Buyer on 1/1/09 and says: “I will sell you my house for $100K closing will be 12/31/09.” Buyer writes Seller: “Ok”.
Seller writes Buyer on 1/1/09 and says: “I will sell you my house for $100K closing will be 12/31/09.” Buyer writes Seller: “Ok”. As the result of this written interchange, what are the terms of the contract? a) Seller and Buyer have a contract to sell and buy, respectively, a $100K house with date of closing being 12/31/09. b) Seller and Buyer have a contract to sell and buy, respectively, a $100K house. c) Seller and Buyer have a contract to sell and...
PYTHON Write a python program that encrypts and decrypts the user input. Note – Your input...
PYTHON Write a python program that encrypts and decrypts the user input. Note – Your input should be only lowercase characters with no spaces. Your program should have a secret distance given by the user that will be used for encryption/decryption. Each character of the user’s input should be offset by the distance value given by the user For Encryption Process: Take the string and reverse the string. Encrypt the reverse string with each character replaced with distance value (x)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT