Problem overview
Version 3 of the brand ranking project handles any number of brands. They are stored in two parallel lists: brands and ratings, where ratings[i] is the rating of brands[i]. The program finds the highest-rated brand with a loop and stores a ranked entry in output. If two brands share the top rating, the one listed first wins.
With the starting data, the ratings are 4, 5, 3, 5 and 4. The top rating, 5, appears twice: MegaCorp is at position 1 and NeoByte at position 3. MegaCorp comes first, so it wins:
1. MegaCorp (5)
Approach: track the position of the best so far
For a list of any length, the idea is to compare ratings repeatedly in a loop. Keep best_index, the position of the best brand seen so far, and start with position 0. Then look at each remaining position, and whenever its rating is strictly higher than the best one's, update best_index.
The loop stores a position instead of a rating on purpose. A position identifies the brand and the rating at the same time, because both lists use the same index. Storing only the highest rating would give the number but not the brand it belongs to.
The comparison is strict (>), so a later brand with an equal rating does not take over. That is exactly the tie rule: MegaCorp keeps the lead against NeoByte.
Step by step
- Create
best_index = 0. Before looking at anything else, the first brand is the best known one. - Loop over the remaining positions.
for i in range(1, len(ratings))givesithe values 1 up to the last position. Position 0 is already the best so far, so the loop starts at 1. The sameiis valid in both lists. - Compare in the loop. Write an
ifthat checksratings[i] > ratings[best_index]. - Update the best. Inside the
if, assignbest_index = i. - Format the result after the loop. Build
outputfrom"1. ",brands[best_index]," (",str(ratings[best_index])and")".
The solution in Python
brands = ["TechPro", "MegaCorp", "Quantum", "NeoByte", "Zenith"]
ratings = [4, 5, 3, 5, 4]
best_index = 0
for i in range(1, len(ratings)):
if ratings[i] > ratings[best_index]:
best_index = i
output = "1. " + brands[best_index] + " (" + str(ratings[best_index]) + ")"
Some details worth knowing:
range(1, len(ratings))excludes its end. With five ratings,itakes the values 1, 2, 3 and 4. Those are all the valid positions except 0.- The loop leaves
ibehind. After the loop,iis the last position visited (4 here). Usebest_indexto read the winner. - The formatting line is unindented. It runs once, after the loop. If it were indented under the loop, it would run on every pass and
outputwould only keep the last value. - The conversion. The string is a ranked entry in which
str()converts the rating, so that+can join it.
Tracing the example
The scan begins with best_index = 0 (TechPro, 4). At i = 1, 5 is greater than 4, so best_index becomes 1. At i = 2, 3 is not greater than 5. At i = 3, 5 is not strictly greater than 5, so MegaCorp stays in front. At i = 4, 4 is not greater than 5. The final best_index is 1, which gives 1. MegaCorp (5).
Edge cases and common mistakes
- Using
>=. NeoByte would replace MegaCorp on the tie, and the output would be1. NeoByte (5). - Starting at 0 in the loop. It works, because the first comparison is a brand against itself, but it adds a pointless step.
- The best brand is first. With ratings
[5, 4, 3], the condition never fires, andbest_indexstays 0, which is correct. - A single brand.
range(1, 1)is empty, so the loop does nothing and that brand wins. - An empty list.
brands[0]would raise anIndexError. The lesson's data always has brands, so it does not check. - Saving the rating instead of the index. The loop would then know the top number but not whose it is.
Testing
ratings = [1, 2, 3, 4, 5]gives1. Zenith (5).ratings = [5, 5, 5, 5, 5]gives1. TechPro (5), the first of the ties.
