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....
Using Python, create a program that will act as a number convertor. This program will convert...
Using Python, create a program that will act as a number convertor. This program will convert an input decimal integer into binary and hexadecimal formats. a) Define a main function named numberconvertor(). Inside the main function, write all the logic of your code. [5% marks] b) Looping indefinitely (Hint: use infinite while loop), the program should give the user the 3 options given below and allow the user to select one among them. [15% marks] 1. converting a decimal number...
How to do this in Python (using Lists): Create a python program that allows a user...
How to do this in Python (using Lists): Create a python program that allows a user to display, sort and update as needed a List of U.S States containing the State Capital and State Bird. You will need to embed the State data into your Python code. The user interface will allow the user to perform the following functions: 1. Display all U.S. States in Alphabetical order along with Capital and Bird 2. Search for a specific state and display...
USING PYTHON PROGRAM ONLY (cannot use list, max, or index function because have not learned about...
USING PYTHON PROGRAM ONLY (cannot use list, max, or index function because have not learned about it in class yet) Part C builds on parts A and B, so just need the final code from part C. USING PYTHON PROGRAM Part 4a: Addition Table Write a program that prompts the user for two integers that are greater than or equal to zero. Ensure that the first integer is less than the second integer. Next, write a program that generates an...
Develop a program using python to demonstrate data exchange using USB protocol.
Develop a program using python to demonstrate data exchange using USB protocol.
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.
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...
Using what you know about the functions of money and the charecteristics of money, is there...
Using what you know about the functions of money and the charecteristics of money, is there anything else that could substitute for actual money? What about diamonds or a bank statement? Why would or would not something else work to meet our needs?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT