Question

In: Computer Science

PYTHON PROGRAM Buzz Lightyear is teaching his friends about money. At the moment, they are using...

PYTHON PROGRAM

Buzz Lightyear is teaching his friends about money. At the moment, they are using fictional $ 5, 10, and 20 Monopoly game bills. Buzz hands out the bills to his friends so that they each have the same amount of money. Then he moves the bills from one friend's pile and puts it on another's pile. Next, the toys have to figure out which one has more money and which one has less.
Sometimes Buzz doesn't move a bill so everyone has exactly the same amount of money; toys should know when this happens.

Input Format:
The first line of each scenario consists of a positive integer, N, which represents the number of toys (2 <N <= 100). Each of the next N lines contains the data for a toy. The lines contain four items, the name of the toy (a single series of 2-10 letters, lowercase except the first) followed by the number of $ 5, 10, and 20 bills (in that order) assigned to that toy. The elements are separated by single spaces. The input ends in a scenario where N equals -1. This scenario should not be processed.

Constraints:
toys (2 <N <= 100)

Output Format:
The output consists of one line for each scenario. It will be in one of the following two formats:

X has more money, Z has less money.
They all have the same amount.
X and Z are names of toys.

Solutions

Expert Solution

The python code to read toy name , number of $5,$10,$20 seperated by white spaces for n number of toys specified by user is:

# Creating two lists toys and toys_sum
# List toys is a 2D array to store n user input data
# List toys_sum stores the sum of amount each toy contain
toys = []
toys_sum=[]
# Since we did not assign the size of 2d list toys[],we should create a another 1d list
# to take input and append to the 2d list toys
n1=[]
# Taking number of toys from user
n = int(input('Enter number of toys: '))
# Using for loop to read n inputs
for i in range(0,n):
    # taking space seperated input and spliting to list using split() function
    n1 = input('Enter name of toy, number of $5,$10,$20 it has seperated by space: ').split(" ")
    # Adding the list to a 2d list using append() function
    toys.append(n1)
    # Calculating the value of amount toy i has and storing in toys_sum list
    toys_sum.append(int(toys[i][1])*5+int(toys[i][2])*10+int(toys[i][3])*20)

# Creating two variables and storing the initial value of list toys_sum to check for max,min
max=toys_sum[0]
min=toys_sum[0]
# Creating two variables to store the max,min index of the toys
max_index=0
min_index=0
j=0
# Checking for max,min amounts and storing the respective index
for j in range(0,n):
    if max<toys_sum[j]:
        max_index=j
    if min>toys_sum[j]:
        min_index=j

# If max_index and min_index are same,then all toys have same amount
if max_index==min_index:
    print("They all have same amount.")
else:
    print(toys[max_index][0]+" has more money, "+toys[min_index][0]+" has less money.")

Code snippet:

First I have taken number of toys as input from user. Then I have take the name of toy, number of $5,$10,$20 denominations it has as space seperated inputs and stored in a list using split() function. split(" ") function splits the input when a white space is encountered. Then I have calculated the sum of amount each toy contain.

To store toy data I have used a 2D array. A 2D array in python is nothing but list of list . So each toy data is a sub-list of main list. The 0th index of sub-list contain the name of the toy. 1st,2nd,3rd index of sub array contain the number of $5,$10,$20 denominations. Multiplying these no.of notes with the respective denominations will give the amount that toy has. Remember to convert these 1,2,3 indices to type int by using int() function. Since the return type of split() function is string. So to perform mathematical operations on this data we should convert them to integers.

I have provided comments in the code explaining the process and functions I used. I have tested the code with multiple inputs and validated the outputs. I am sharing few output screenshots for you reference.

Hope this answer will help you.

Thank you :)


Related Solutions

USING IDLE PYTHON: A greengrocer would like to maintain a linked lists about his products. For...
USING IDLE PYTHON: A greengrocer would like to maintain a linked lists about his products. For each product he saves it name, price and stock amount. Write a program that creates an empty linked list and then prompts to user to do one of the following: 1. Add a product to the list (anywhere) 2. Print all products in the LinkedList 3. Print all products above a certain price 4. Print all low-stock products ( Less than 20 pounds) 5....
Develop a program using python to demonstrate data exchange using USB protocol.
Develop a program using python to demonstrate data exchange using USB protocol.
Using the Python program: a/ Write a program that adds all numbers from 1 to 100...
Using the Python program: a/ Write a program that adds all numbers from 1 to 100 to a list (in order). Then remove the multiples of 3 from the list. Print the remaining values. b/ Write a program that initializes a list with ten random integers from 1 to 100. Then remove the multiples of 3 and 5 from the list. Print the remaining values. Please show your work and explain. Thanks
The code that creates this program using Python: Your program must include: You will generate a...
The code that creates this program using Python: Your program must include: You will generate a random number between 1 and 100 You will repeatedly ask the user to guess a number between 1 and 100 until they guess the random number. When their guess is too high – let them know When their guess is too low – let them know If they use more than 5 guesses, tell them they lose, you only get 5 guesses. And stop...
Python programming c++ Write a program that computes the amount of money the cheerleaders raised during...
Python programming c++ Write a program that computes the amount of money the cheerleaders raised during their candy bar fundraiser using the following data: 12 bars per case. The candy was sold for $1.00 per bar. Each case cost $8.00. They are required to give the student government association 10% of their earnings. The program should ask the user how many bars were sold. The program should calculate and display the SGA proceed's, the Cheer team's proceeds, and the appropriate...
true or fasle Per Dan Ariely, all people care about is money, and the moment we...
true or fasle Per Dan Ariely, all people care about is money, and the moment we give them money, we can direct them to work one way or another. This is why we give bonuses to bankers and pay in all kinds of ways.
In python using tkinter, I want to create a program. The program can have various classes...
In python using tkinter, I want to create a program. The program can have various classes and methods but I want it to have circles, triangles, and squares. Each shape movies in a certain way, like circles can move linearly, triangles can be affected by gravity, and squares can only go up and down. If the shapes get too close to one another then they disappear and a new shape appears somewhere else. I want this program to run continuously.
how can i make this program in python? Captain Øks is on his way to conquer...
how can i make this program in python? Captain Øks is on his way to conquer Mosefjell. He has a map of the area and he is in his camp at the designated place on the map. The map is divided into cells. For each cell, the height is given. Captain Øks is not happy, the landscape is full of hills, and he hates to climb both up and down. He has hired you to write a program that will...
In PYTHON OF JUPIDER NOTEBOOK Write a program that prompts the user to enter his/her nationality...
In PYTHON OF JUPIDER NOTEBOOK Write a program that prompts the user to enter his/her nationality (French/french, Italian/italian, or Spanish/spanish). Then ask his/her name using a prompt message in his/her own language (use Google Translate if you need). After getting the name, again greet him/her using a greeting message in his/her own language. If the user is not from any of the above nationalities just use English to prompt for name and to greet the user. Example 1: Nationality? french...
USING PYTHON Write a program to create a number list. It will call a function to...
USING PYTHON Write a program to create a number list. It will call a function to calculate the average values in the list. Define main ():                        Declare variables and initialize them                        Create a list containing numbers (int/float)                        Call get_avg function that will return the calculated average values in the list.                                       Use a for loop to loop through the values in the list and calculate avg                        End main()
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT