Problem overview
Version 4 scales the program to any number of employees. The data is now in three parallel lists: names, productivities and salaries. Index i in each list describes the same person. The employee with the highest productivity wins, and on a tie the one listed first wins. The program prints the winner's name and then Bonus: $ followed by 15% of their salary, rounded down.
With five employees whose scores are 83, 133, 97, 150 and 120, the highest is 150, which belongs to Alice Chen at index 3. Her salary is 250000, so the bonus is 37500:
Alice Chen
Bonus: $37500
Because the data is in lists, one loop handles every employee, whatever the team size. The program needs no separate variables for each person.
Approach: remember the index of the best so far
This is the classic "find the maximum" scan. Keep a variable, here best_index, holding the position of the most productive employee seen so far. Start by assuming employee 0 is the best. Then visit every other employee once, and whenever one has a strictly higher productivity than the current best, update best_index.
Storing the position instead of the productivity is the key idea. Once the loop is done, one number gives access to everything about the winner: names[best_index] and salaries[best_index] are the right name and salary because the lists are parallel. Had the loop stored only the highest score, it would have no way to find the matching name.
The comparison is strict (>). A later employee replaces the current best only by being strictly better, so when two share the top score the earlier one stays in front, as the rules require.
Step by step
- Create
best_indexand set it to 0. Until somebody beats employee 0, employee 0 is the best candidate. - Loop over the other positions.
for i in range(1, len(names))makesitake the values 1 up to the last index. It starts at 1 because position 0 is already the current best, and comparing it with itself would change nothing. - Compare inside the loop. If
productivities[i]is strictly greater thanproductivities[best_index], assignbest_index = i. - Compute the bonus after the loop:
salaries[best_index] * 15 // 100. The//operator divides and rounds down, so the amount stays a whole number. - Print the name with
names[best_index]. - Print the bonus line, converting the number with
str()before joining it to"Bonus: $".
The solution in Python
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
bonus = salaries[best_index] * 15 // 100
print(names[best_index])
print("Bonus: $" + str(bonus))
Some details worth knowing:
range(1, len(names)). The end value is excluded, so with five employees the loop sees 1, 2, 3 and 4. List indexes start at 0, so these are the second to the fifth employee.- The loop variable stays visible. After the loop,
istill holds the last index visited (4 here), which is not the winner. Always usebest_indexafter the loop. - Everything after the loop is unindented. The bonus and the prints run once, after the scan is complete. Indenting them under the loop would compute and print on every iteration.
- Indexes pair the lists. The lists are separate objects, and nothing in Python links them. The program is correct only because the data was written in the same order in all three.
Tracing the example
The scan starts with best_index = 0 (score 83). At i = 1, 133 is greater than 83, so best_index becomes 1. At i = 2, 97 is not greater than 133, so nothing changes. At i = 3, 150 is greater than 133, and best_index becomes 3. At i = 4, 120 is not greater than 150. The loop ends with best_index equal to 3, so the program reports names[3], "Alice Chen", with a bonus of 250000 * 15 // 100 = 37500.
Edge cases and common mistakes
- Ties. With scores
[100, 100], the second is not strictly greater, so index 0 wins. Using>=would let the later employee take the title. - The best employee is first. With scores
[150, 120, 90, 80, 70]the condition never fires andbest_indexstays 0, which is correct. - One employee.
range(1, 1)is empty, so the loop body never runs, and employee 0 wins by default. - Starting at the wrong place.
range(0, ...)still works, because the first comparison is the employee against itself, but it does a pointless step.range(2, ...)would skip employee 1 and could miss the winner. - Comparing against
i - 1. Looking at the neighbour finds a local peak, not the overall best. Compare withbest_index. - Printing
names[i]after the loop. That prints the last employee, not the winner.
Testing
productivities = [150, 120, 90, 80, 70]picks John Doe:Bonus: $20250.- Scores of
[90, 150, 150, 80, 70]pick Mary Jane, the first of the two 150s.
