Question

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

A))

(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, USING LISTS, no dictionaries:

  • 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.

B)) Short one

Write Python code to define a class called Student.

The Student class should have the following private fields:

  1. name – to store the name of a student
  2. attend – to store how many times a student attends the class
  3. grades – a list of all student’s grades

The Student class should have the following methods:

  1. a constructor (init method)
  2. getter method for the name field
  3. attendClass method: that increments the attend field by 1 (should be called every time a student attends the class).
  4. addGrade method: adds a new grade to the grades list
  5. __str__ method: prints all student’s information

In the same file, write a main function to test the functionality of the Student class.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code for Question - A: Ransom Note Problem in python.

# Taking Magazine text from User
magazine=input("Enter Magazine Text: ")

# Taking ransom text from User
ransom=input("Enter Ransom text: ")

# Running the loop for Small Alphabets a to z
# a=97 ........................ z=122
for i in range(97,123):
c = chr(i) # Type casting - Getting alphabet using ascii value

# Check character count in magazine and ransom text
# if count of character in ransom less then count of character in magazine
# If condition accepted and print False and loop will break
if(magazine.count(c)<ransom.count(c)):
print('False')
break

# For-Else Loop
# If loop runs without any break else condition will be executed
else:
print('True')
  

Code Screenshot:

Output Screenshots:

Code for Question -B : Write Python code to define a class called Student.

# Student class to store Students data
class Student:
# Class intialization method with arguments
def __init__(self,name='', attend=0, grades=[]):
# Assigning to respected values
self.name = name
self.attend = attend
self.grades = grades

# Method to get name from Class instance object
def get_name(self):
return self.name
  
# Method to add attendance to Class instance object
def attendClass(self):
self.attend += 1
# Method to add grades to Class instance object
def addGrade(self, grade):
self.grades.append(grade)

def __str__(self):
result = 'Name: ' + self.name + '\n'
result += 'Number of classes attending: ' + str(self.attend) + '\n'
result += 'Grades are: ' + str(self.grades)
return result

# Main function
if __name__=="__main__":
# Creating Class instance and passign name string
s = Student(name='Ronaldo')
  
# Increasing the class attendance
s.attendClass()

# Adding Grades
s.addGrade(97)
# Increasing the class attendance
s.attendClass()
# Adding Grades
s.addGrade(92)

# Printing Instance Values using class method
print(s.__str__())

Code Screenshots:

Output Screenshot:

Note: If you any Quiries, ask me in comments...

Please vote up. your vote is more valuable to me.

Add a comment
Know the answer?
Add Answer to:
A)) (Ransom Note Problem) in python A kidnapper kidnaps you and writes a ransom note. He...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • For each problem, you must: Write a Python program Test, debug, and execute the Python program...

    For each problem, you must: Write a Python program Test, debug, and execute the Python program Save your program in a .py file and submit your commented code to your Student Page. Note regarding comments: For every class, method or function you create, you must provide a brief description of the what the code does as well as the specific details of the interface (input and output) of the function or method. (25 pts.) Write a program that reads in...

  • Java - In NetBeans IDE: • Create a class called Student which stores: - the name...

    Java - In NetBeans IDE: • Create a class called Student which stores: - the name of the student - the grade of the student • Write a main method that asks the user for the name of the input file and the name of the output file. Main should open the input file for reading . It should read in the first and last name of each student into the Student’s name field. It should read the grade into...

  • Create a java class for an object called Student which contains the following private attributes: a...

    Create a java class for an object called Student which contains the following private attributes: a given name (String), a surname (family name) (String), a student ID number (an 8 digit number, String or int) which does not begin with 0. There should be a constructor that accepts two name parameters (given and family names) and an overloaded constructor accepting two name parameters and a student number. The constructor taking two parameters should assign a random number of 8 digits....

  • c++ I need help! create Student.h In this class, you are provided with a class skeleton...

    c++ I need help! create Student.h In this class, you are provided with a class skeleton for the Student type. This type should contain two member variables: a string called name a vector of doubles called grades Additionally, you should declare the following functions: A constructor which accepts a single string parameter called name A void function, addGrade which accepts a single double parameter and adds it to the grades vector A function which accepts no parameters and returns the...

  • In this assignment you are asked to write a python program to maintain the Student enrollment...

    In this assignment you are asked to write a python program to maintain the Student enrollment in to a class and points scored by him in a class. You need to do it by using a List of Tuples What you need to do? 1. You need to repeatedly display this menu to the user 1. Enroll into Class 2. Drop from Class 3. Calculate average of grades for a course 4. View all Students 5. Exit 2. Ask the...

  • PYTHON PROGRAMMING Instructions: You are responsible for writing a program that allows a user to do...

    PYTHON PROGRAMMING Instructions: You are responsible for writing a program that allows a user to do one of five things at a time: 1. Add students to database. • There should be no duplicate students. If student already exists, do not add the data again; print a message informing student already exists in database. • Enter name, age, and address. 2. Search for students in the database by name. • User enters student name to search • Show student name,...

  • Java program Program: Grade Stats In this program you will create a utility to calculate and...

    Java program Program: Grade Stats In this program you will create a utility to calculate and display various statistics about the grades of a class. In particular, you will read a CSV file (comma separated value) that stores the grades for a class, and then print out various statistics, either for the whole class, individual assignments, or individual students Things you will learn Robustly parsing simple text files Defining your own objects and using them in a program Handling multiple...

  • We will build one Python application via which users can perform various analytics tasks on data...

    We will build one Python application via which users can perform various analytics tasks on data in 2-D table (similar to Spreadsheet) format which looks like: Column Name 1 Column Name 2 Column Name 3 Column Name N … … … … … In this table, each row represents the data of one object that we are interested in. Columns, on the other hand, represent the attributes of these objects. For example, this table could represent students’ academic records. Each...

  • Instructions: Consider the following C++ program. At the top you can see the interface for the...

    Instructions: Consider the following C++ program. At the top you can see the interface for the Student class. Below this is the implementation of the Student class methods. Finally, we have a very small main program. #include <string> #include <fstream> #include <iostream> using namespace std; class Student { public: Student(); Student(const Student & student); ~Student(); void Set(const int uaid, const string name, const float gpa); void Get(int & uaid, string & name, float & gpa) const; void Print() const; void...

  • Do in Python Problem 2 Assume s is a string of lower case characters. Write a...

    Do in Python Problem 2 Assume s is a string of lower case characters. Write a program that prints the number of times the string ' lol' occurs in s For example, if s= 'azclololegghakl', then your program should print S Number of times lol occu rs is: 2 Problem 3 Assume s is a string of lower case characters Write a program that prints the longest substring of s in which the letters occur in alphabetical order. For example,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT