In: Computer Science
Write the following Python script:
Problem Statement
We want to know how many different people have eaten at a restaurant this past week. The parameter meals has strings in the format "name:restaurant" for a period of time. Sometimes a person eats at the same restaurant often.
Return the number of different people who have eaten at the eating establishment specified by parameter restaurant.
For example, "John Doe:Moes" shows that John Doe ate one meal at Moes.
Write function howMany that given meals, a list of strings in the format above indicating where each person ate a meal, and restaurant, the name of a restaurant, returns the number of people that ate at least one meal at that restaurant.
Constraints
Check your work with the following examples:
------------------------------------------------------------
meals = ["Sue:Elmos"]
restaurant = "Guglhupf"
returns 0
------------------------------------------------------------
meals = []
restaurant = "Sushi Love"
returns 0
------------------------------------------------------------
meals = ["Sue:Elmos", "Sue:Elmos", "Sue:Elmos"]
restaurant = "Elmos"
returns 1
------------------------------------------------------------
meals = ["Sue:Elmos", "Joe:Mad Hatter", "Sue:Mad Hatter", "Kristin:Sitar"]
restaurant = "Mad Hatter"
returns 2
------------------------------------------------------------
meals = ["Sue:Elmos", "Joe:Mad Hatter", "Sue:Mad Hatter", "Bill:Elmos", "Owen:Elmos", "Joe:Mad Hatter", "Sue:Mad Hatter", "Nora:Elmos", "Nora:Sitar", "Joe:Elmos","Mary:Sitar"]
restaurant = "Mad Hatter"
returns 2
------------------------------------------------------------
meals = ["Sue:Elmos", "Joe:Mad Hatter", "Sue:Mad Hatter", "Bill:Elmos", "Kristin:Sitar", "Owen:Elmos", "Joe:Mad Hatter", "Sue:Mad Hatter", "Nora:Elmos", "Nora:Sitar", "Joe:Elmos", "Mary:Sitar", "Joe:Elmos", "Mary:Sitar", "Kristin:Sitar"]
restaurant = "Elmos"
returns 5
I have uploaded the Images of the code, Typed code and Output of the Code. I have provided explanation using comments (read them for better understanding).
Images of the Code:
Note: If the below code is missing indentation please refer
code Images
Typed Code:
# A function named howMany
# with input parameters meals (a list of strings) and restaurant (a
string)
def howMany(meals,restaurant):
# A list to store person names
# who visited the restaurant
customers = []
# for all values in list of meals
for x in meals:
# spliting the person name and restaurant name
# using split function
person_name,restaurant_name = x.split(":")
# if the restaurant_name is same function parameter
restaurant
if(restaurant_name == restaurant):
# checking whether person_name is not in the list of
customers
if person_name not in customers:
# appending then person name into the customers list
customers.append(person_name)
# Note: inserting into the customers list only if name is not in
list
# as sometimes same person eats at the restaurant more than
once
# returning length of customers list
return len(customers)
# A list of meals
meals = ["Sue:Elmos"]
# name of the restaurant
restaurant = "Guglhupf"
# calling function howMany with parameters meals and
restaurant
# and printing the returned value
print("Number of People ate at ",restaurant ,"are ",howMany(meals,
restaurant))
# A list of meals
meals = []
# name of the restaurant
restaurant = "Sushi Love"
# calling function howMany with parameters meals and
restaurant
# and printing the returned value
print("Number of People ate at ",restaurant ,"are ",howMany(meals,
restaurant))
# A list of meals
meals = ["Sue:Elmos", "Sue:Elmos", "Sue:Elmos"]
# name of the restaurant
restaurant = "Elmos"
# calling function howMany with parameters meals and
restaurant
# and printing the returned value
print("Number of People ate at ",restaurant ,"are ",howMany(meals,
restaurant))
# A list of meals
meals = ["Sue:Elmos", "Joe:Mad Hatter", "Sue:Mad Hatter",
"Kristin:Sitar"]
# name of the restaurant
restaurant = "Mad Hatter"
# calling function howMany with parameters meals and
restaurant
# and printing the returned value
print("Number of People ate at ",restaurant ,"are ",howMany(meals,
restaurant))
#code ended here
Output:
If You Have Any Doubts. Please Ask Using Comments.
Have A Great Day!