Hand out bonuses to the top 2 most productive employees: the best one first, then the second best. You don't need any sorting. Find the best employee with the loop from v4, then find the best of the other employees with a second loop.
You start with:
names = ["John Doe", "Mary Jane", "Bob Smith", "Alice Chen", "Carlos Diaz"]
productivities = [83, 133, 97, 150, 120]
salaries = [135000, 335000, 210000, 250000, 180000]
Build:
Alice Chen: $37500 Mary Jane: $50250
Rules:
- Index
iin all three lists refers to the same employee. - The best employee is the one with the highest productivity; the second best is the one with the highest productivity among all the other employees.
- If two employees have the same productivity, the one listed first wins.
- Each bonus is 15% of the salary, rounded down (using
//). - Print two lines, the best employee first:
name: $bonus.
Find the best employee first, exactly as in v4. Remember: strictly greater, so the first listed wins a tie.
For the second place, loop over all the indices and ignore the best one. The whole condition is
if i != best_index and (second_index == -1 or productivities[i] > productivities[second_index]):. "Nobody chosen yet" must also let an employee in.
names = ["John Doe", "Mary Jane", "Bob Smith", "Alice Chen", "Carlos Diaz"]
productivities = [83, 133, 97, 150, 120]
salaries = [135000, 335000, 210000, 250000, 180000]
best_index = 0
for i in range(1, len(names)):
if productivities[i] > productivities[best_index]:
best_index = i
second_index = -1
for i in range(len(names)):
if i != best_index and (second_index == -1 or productivities[i] > productivities[second_index]):
second_index = i
best_bonus = salaries[best_index] * 15 // 100
second_bonus = salaries[second_index] * 15 // 100
print(names[best_index] + ": $" + str(best_bonus))
print(names[second_index] + ": $" + str(second_bonus))
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check
