In: Computer Science
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.
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 :)