Problem overview
A whole team competes, stored as three parallel arrays: names, productivities and salaries. Index i in all three refers to the same employee. The winner has the highest productivity, and if two employees share the highest score, the one listed first wins. The program prints the winner's name and a bonus of 15% of their salary, rounded down.
For the starter data, the highest productivity is 150, which belongs to Alice Chen at index 3. Her salary is 250000, so the bonus is 37500:
Alice Chen
Bonus: $37500
The earlier versions compared a fixed number of employees with if statements. A loop makes the code work for any number of them, and the rest of the program stays the same.
Approach: keep the index of the best so far
The idea is a running champion. Start by assuming employee 0 is the best, and keep that employee's index in bestIndex. Then visit every other employee once. Whenever one has a strictly higher productivity than the current champion, that employee becomes the new champion.
Storing the index, not the productivity, is the key choice. One index reaches into all three arrays, so at the end names[bestIndex] and salaries[bestIndex] give the winner's name and salary together.
The comparison is strictly greater. On a tie, the challenger is not strictly better, so the earlier employee stays in front, which is the tie rule.
The loop on the starter data
| i | productivities[i] | bestIndex after the check |
|---|---|---|
| 1 | 133 | 1 |
| 2 | 97 | 1 |
| 3 | 150 | 3 |
| 4 | 120 | 3 |
Employee 1 beats employee 0 (133 against 83), and employee 3 beats employee 1 (150 against 133). Nobody beats 150, so the loop ends with bestIndex equal to 3.
Step by step
- Create
bestIndexwithvar, starting at 0. It changes while the loop runs, so it can't be alet. - Write the
forloop over1..<names.count. Employee 0 is already the champion, so there is nothing to compare at index 0, and the loop starts at 1. - Inside the loop, compare
productivities[i]withproductivities[bestIndex]. When the first is strictly greater, setbestIndextoi. - After the loop, compute
bonusassalaries[bestIndex] * 15 / 100. Integer division rounds down. - Print
names[bestIndex]on its own line. - Print the bonus line, with the number converted by
String(...)before it is joined to the text.
The solution in Swift
let names = ["John Doe", "Mary Jane", "Bob Smith", "Alice Chen", "Carlos Diaz"]
let productivities = [83, 133, 97, 150, 120]
let salaries = [135000, 335000, 210000, 250000, 180000]
var bestIndex = 0
for i in 1..<names.count {
if productivities[i] > productivities[bestIndex] {
bestIndex = i
}
}
let bonus = salaries[bestIndex] * 15 / 100
print(names[bestIndex])
print("Bonus: $" + String(bonus))
The Swift details worth knowing:
- Half-open ranges.
1..<names.countincludes 1 and stops beforenames.count. For five names,itakes the values 1, 2, 3 and 4, which are all the valid remaining indices. The closed range1...names.countwould reach index 5 and crash. - Arrays are inferred. The literals give
[String]and[Int]. Indexing an array out of bounds stops the program with a run-time error, so each index must stay between 0 andcount - 1. letarrays. The three arrays are constants, which is fine because the program only reads them. OnlybestIndexchanges.- Integer arithmetic.
Intis 64 bits in Swift, so these salaries are nowhere near overflow. If arithmetic did overflow, Swift would trap and crash, not wrap around silently.
Edge cases and common mistakes
- A tie for the top. If Mary Jane's productivity is changed to 150, both she and Alice Chen have 150. The program prints
Mary JaneandBonus: $50250, because she is listed first and Alice's 150 is not strictly greater. - Using
>=. It would let the later employee replace the earlier one on every tie. In that same tie, Alice would win. - A single employee. With one name,
1..<1is an empty range, so the loop body never runs and employee 0 wins. With the data for John Doe alone, the program printsJohn DoeandBonus: $20250. - No employees. With empty arrays, the range
1..<0is invalid in Swift, andnames[0]would not exist either, so the program crashes. The lesson's data always has at least one employee. - Tracking the productivity instead of the index. Remembering only the best score tells you who is best by score but not whose name and salary to print.
- Starting
bestIndexat 0 but looping from 0. It works, because an employee compared with themselves is not strictly greater, but it does one useless comparison.
Testing
- The starter data prints
Alice ChenandBonus: $37500. - With productivities
[83, 150, 97, 150, 120], the output isMary JaneandBonus: $50250. - With the single employee John Doe (productivity 83, salary 135000), the output is
John DoeandBonus: $20250.
