Project adjectives:
The problem presented in this lab is an active research area in Computer Science called differential privacy and secure (multi-party) computation. In this lab, we won’t be able to use a differentially private algorithm, so we will assume that the users stored their data secretly somewhere and cannot see each other’s values.
Three friends finished the test and want to secretly compare their test scores. They do not want to know each other’s exact score, but they do want to know who achieved the best results for this test.
Instructions
In your main program:
- Input the names and scores of the three friends one after another. (Check out the example input below)
- Your program should store both names and scores. Store the names in the order that they were input in one tuple and the scores in another tuple.
- Call the
compare_scores()
function and print the returned name(s) of the student(s) with the highest score(s).
Write the compare_scores(names, scores)
function that expects two parameters:
- a tuple with the names
- a tuple with the scores corresponding to the respective name
The function compares the scores and returns one or more names (from the input tuple) as a string.
- If there is one best score - return the name of the friend that achieved that score.
- If there are two friends with equal highest scores - return a string with both names, separated by space, in the order of input.
- If all three friends achieved equal scores - return a string with all three names, separated by space, in the order of input.
Examples
Input
Ann
10
Maria
9
Cindy
7
Output
Ann
Input
Ann
10
Maria
9
Cindy
10
Output
Ann Cindy
Input
Ann
10
Maria
10
Cindy
10
Output
Ann Maria Cindy
Notes
- Use the <, >, >=, <= and == operators
- Use
elif
and/or nestedif
s - Be careful with the indentation (i.e., the spaces before the blocks of code), especially in nested
if
s.
my code:
def compare_scores(names, scores):
combined_name_and_score = {}
for index, value in enumerate(names):
combined_name_and_score[value] = scores[index]
combined_name_and_score = dict(sorted(combined_name_and_score.items(), key=lambda item: item[1], reverse=True))
sorted_name = combined_name_and_score.keys()
sorted_numbers = combined_name_and_score.items()
high_score = max(scores)
lowest_score = min(scores)
high_score_position = scores.index(high_score)
middle_score_position = scores.index((len(scores)/2)-1)
low_score_position = scores.index(lowest_score)
for score in scores:
if score != max(scores):
print(sorted_name[high_score_position],sorted_name[middle_score_position],sorted_name[low_score_position])
break
else:
for name in names:
print(name, end=' ')
if __name__ == "__main__":
name1 = input()
num1 = int(input())
name2 = input()
num2 = int(input())
name3 = input()
num3 = int(input())
names = (name1,name2,name3)
nums = (num1,num2,num3)
compare_scores(names, nums)