Problem overview
Three parallel arrays describe five brands: brands, ratings and reviews, where position i in each refers to the same brand. The program prints the top k brands, one per line, in the form rank. Brand (rating, reviews), starting at rank 1. The ranking has two criteria:
- The higher rating wins.
- If two ratings are equal, the brand with more reviews wins.
- If rating and reviews are both equal, the brand listed first wins.
With k = 3 on the starter data, NeoByte and MegaCorp share a rating of 5, and NeoByte has more reviews. Zenith and TechPro share a 4, and Zenith has more reviews. The output is:
1. NeoByte (5, 500)
2. MegaCorp (5, 150)
3. Zenith (4, 300)
In this version k is never larger than the number of brands, and there is no product filter: the search works on all the brands.
Approach: the same repeated search, a richer comparison
The search is the repeated one from the earlier ranking versions. Each round scans the brands that are not taken yet, keeps a bestIndex that starts at -1, marks the round's winner in a used array, and adds a line. Only the question "does this brand beat the current best?" changes.
That question now has three cases, tested in order:
- Nothing is chosen yet (
bestIndex == -1): this brand becomes the best, because it has nobody to beat. - Its rating is higher than the best one's: it becomes the best, and the reviews don't matter at all.
- The ratings are equal and it has more reviews: it becomes the best.
Writing them as an if / else if chain matters. A case runs only when the ones above it failed, so the review count is looked at only when the rating isn't strictly higher. There is no case for equal ratings with equal reviews, so the brand listed first stays, which is exactly the third rule. Every comparison uses strictly greater, and a brand never replaces one that matches it completely.
The rounds on the starter data
In round 1, TechPro (4, 200) is chosen first because nothing is chosen yet. MegaCorp (5, 150) has a higher rating and replaces it. NeoByte (5, 500) has an equal rating and more reviews, so it replaces MegaCorp. Quantum and Zenith don't beat it. NeoByte wins. In round 2 NeoByte is skipped, and MegaCorp wins for the same reasons. In round 3 only TechPro (4, 200), Quantum (3, 100) and Zenith (4, 300) are left, and Zenith has the same rating as TechPro with more reviews, so Zenith takes third place.
Step by step
- Create
usedwith onefalseper brand andlinesas an empty[String]. - Loop over the ranks with
for rank in 1...k, and start each round withvar bestIndex = -1. - Loop over the positions with
for index in ratings.indices, and skip the brands that are taken withif !used[index]. - First case: if
bestIndex == -1, setbestIndex = index. - Second case, an
else if: the rating is higher than the best one's, so the brand becomes the best. - Third case, another
else if: the ratings are equal and the review count is higher. Both parts must hold, so they are joined with&&. - After the scan, mark the winner by setting
used[bestIndex]totrue. - Add the line with the rank, the name, then the rating and the review count in parentheses, separated by a comma and a space.
- Print the lines joined with a newline.
The solution in Swift
let brands = ["TechPro", "MegaCorp", "NeoByte", "Quantum", "Zenith"]
let ratings = [4, 5, 5, 3, 4]
let reviews = [200, 150, 500, 100, 300]
let k = 3
var used = Array(repeating: false, count: ratings.count)
var lines = [String]()
for rank in 1...k {
var bestIndex = -1
for index in ratings.indices {
if !used[index] {
if bestIndex == -1 {
bestIndex = index
} else if ratings[index] > ratings[bestIndex] {
bestIndex = index
} else if ratings[index] == ratings[bestIndex] && reviews[index] > reviews[bestIndex] {
bestIndex = index
}
}
}
used[bestIndex] = true
lines.append("\(rank). \(brands[bestIndex]) (\(ratings[bestIndex]), \(reviews[bestIndex]))")
}
print(lines.joined(separator: "\n"))
Swift specifics worth knowing:
- The chain is evaluated top to bottom. Swift stops at the first condition that is true. The third condition reads
ratings[bestIndex], which is safe because the first case already handledbestIndex == -1. &&needs both sides true. A brand with more reviews but a lower rating never reaches a replacement: the second case fails because its rating isn't higher, and the third fails because the ratings aren't equal.- Interpolation with several values. The line
"\(rank). \(brands[bestIndex]) (\(ratings[bestIndex]), \(reviews[bestIndex]))"mixes text, anIntand aStringwithout any conversion. The literal comma and space between the two numbers are ordinary characters in the string. 1...kandratings.indices.1...kincludesk, andindicesgives the positions0..<5of the array.
Edge cases and common mistakes
- Reviews can't beat rating. With brands A (rating 3, 900 reviews) and B (rating 4, 5 reviews), B ranks first. The order is
1. B (4, 5)then2. A (3, 900). - Full ties. With three brands that all have a 4 and 10 reviews, the output keeps the listed order: A, B, C. Using
>=for the reviews would reverse it. - Tie on rating only. With ratings
[5, 5, 5]and reviews[10, 30, 20], the output is B (30 reviews), then C (20), then A (10). - Forgetting the equal-rating test. A condition that only checks
reviews[index] > reviews[bestIndex]would let a lower-rated brand with many reviews win. - Not marking the winner. Without
used[bestIndex] = true, every round returns NeoByte.
Testing
These outputs come from the solution on the starter arrays with other values of k.
k = 1prints1. NeoByte (5, 500).k = 5prints NeoByte (5, 500), MegaCorp (5, 150), Zenith (4, 300), TechPro (4, 200) and Quantum (3, 100), in that order.
Change the arrays in the editor above and check each of the three rules.
