226k views
3 votes
On the classic gameshow The Price Is Right, contestants must guess the price of an object (guesses are distinct). The winner is determined to be the person whose bid is closest to the correct price, but without going over. For example, if the guesses were Alice 430 Bob 538 Carol 487 David 550 and the true price were 520, then Carol has the winning bid with 487. With the same bids as above, if the actual price had been 420, then no one would win, as everyone bid too high. Your job is to implement a function with signature judge (auction, price) in which auction is a nonempty list of (person, guess) tuples and price is the actual price. The function must return the name of the winning person (or None, if no one wins). For example, the first scenario above might be expressed as judge( [('Alice', 430), ('Bob', 538), ('Carol', 487), ('David', 550)], 520) and the string 'Carol' should be returned as the winner. Your function may assume that all parameters are valid. []

User Jboi
by
6.2k points

1 Answer

5 votes

Answer:

The program code is in explaination

Step-by-step explanation:

Program code below.

def judge(auction,price):

"""

Function: Returns Name who auctioned very near to give price

Input :

auction : list of tuples

price : auction price

output: returns name of best auction amount holder

"""

diff = [] #storing differences between auctionist amount and price

count = 0

for each_pair in auction:

if each_pair[1]<=price:

diff.append(price-each_pair[1])

else:

count+=1

diff.append(each_pair[1])

if count == len(auction): #check for if no one have auctioned good amount

return None

else:

more_possibility_index = diff.index(min(diff)) #finding index of best amount from diff list

return auction[more_possibility_index][0]

auction = [('Alice',430),('Bob',538),('Carol',487),('David',550)]

price = 520

print(judge(auction,price))

I kept the output at the attachment.

On the classic gameshow The Price Is Right, contestants must guess the price of an-example-1
User Manoos
by
6.3k points