Question

In: Computer Science

Write the following Python script: Problem Statement We want to know how many different people have...

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

  • meals is a list with 0 or more strings. Each string is in the format above, so each string has length at least 3 and contains a colon. The string before the ":" and the string after it are at least one character long.
  • len(restaurant) >=1

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

Solutions

Expert Solution

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!


Related Solutions

Write the following Python script: Problem Statement A string X is an anagram of string Y...
Write the following Python script: Problem Statement A string X is an anagram of string Y if X can be obtained by arranging all characters of Y in some order, without removing any characters and without adding new characters. For example, each of the strings "baba", "abab", "aabb" and "abba" is an anagram of "aabb", and strings "aaab", "aab" and "aabc" are not anagrams of "aabb". A set of strings is anagram-free if it contains no pair of strings which...
If you want to know if there is any difference at all in how many people...
If you want to know if there is any difference at all in how many people prefer any of five different salad dressings, you would do what kind of test? Chi square goodness of fit Chi square test of independence Independent sample (simple) ANOVA Repeated measures(matched sample)ANOVA Factorial ANOVA
How do I write a script for this in python in REPL or atom, NOT python...
How do I write a script for this in python in REPL or atom, NOT python shell Consider the following simple “community” in Python . . . triangle = [ ["top", [0, 1]], ["bottom-left", [0, 0]], ["bottom-right", [2, 0]], ] This is the calling of function. >>> nearestneighbor([0, 0.6], triangle, myeuclidean) 'top' The new point is (0, 0.6) and the distance function is Euclidean. Now let’s confirm this result . . . >>> myeuclidean([0, 0.6], [0, 1]) 0.4 >>> myeuclidean([0,...
You want to know how many different products were produced by a new reaction you have...
You want to know how many different products were produced by a new reaction you have devised. You spot the product mixture on a TLC plate and develop it in petroleum ether and observe a single spot at Rf 0.05. a) Why does this experiment not confirm that only a single product was produced in the     reaction?                    b) What change would you make to the mobile phase to gain more information about the composition of...
Write the following Python script: Imagine you live in a world without modules in Python! No...
Write the following Python script: Imagine you live in a world without modules in Python! No numpy! No scipy! Write a Python script that defines a function called mat_mult() that takes two lists of lists as parameters and, when possible, returns a list of lists representing the matrix product of the two inputs. Your function should make sure the lists are of the appropriate size first - if not, your program should print “Invalid sizes” and return None. Note: it...
Write the following Python script: Imagine you live in a world without modules in Python! No...
Write the following Python script: Imagine you live in a world without modules in Python! No numpy! No scipy! Write a Python script that defines a function called mat_mult() that takes two lists of lists as parameters and, when possible, returns a list of lists representing the matrix product of the two inputs. Your function should make sure the lists are of the appropriate size first - if not, your program should print “Invalid sizes” and return None. Note: it...
Write a python script to solve the 4-queens problem using. The code should allow for random...
Write a python script to solve the 4-queens problem using. The code should allow for random starting, and for placed starting. "The 4-Queens Problem[1] consists in placing four queens on a 4 x 4 chessboard so that no two queens can capture each other. That is, no two queens are allowed to be placed on the same row, the same column or the same diagonal." Display the iterations until the final solution Hill Climbing (your choice of variant)
This problem requires python(Project: Evaluate Word Problems) Write a script that enables the user to enter...
This problem requires python(Project: Evaluate Word Problems) Write a script that enables the user to enter mathematical word problems like "two times three" and " seven minus five", then use string processing to break apart the string into the numbers and the operation and return the result. So “two times three” would return 6 and “seven minus five would return 2. To keep things simple, assume the user enters only the words for the numbers 0 through 9 and only...
In our society, we have people from many other countries with many different backgrounds, education, culture,...
In our society, we have people from many other countries with many different backgrounds, education, culture, and skills. As such, they can be identified in many different diverse group sets. Diversity is referred to as, “A characteristic of a group or organization whose members differ from one another along one or more important dimensions, such as age, gender, or ethnicity” (Griffin, 2016, p. 244). As businesses hire people to help in the manufacturing of goods or the provision of services,...
How do you write pseudocode for this problem? The decides are provided below. Problem Statement We...
How do you write pseudocode for this problem? The decides are provided below. Problem Statement We are going to create a game called TwentyOne, which is similar to the Blackjack and War card games. This game has a dealer/computer and 1-4 players, and the game will generate random numbers from 1 to 11 to simulate the values in cards. All players start with a specified amount of money, which they determine at the start of the game, and the goal...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT