Problem overview
The function priceList receives an array of strings, items, and an array of integers, prices, of the same length. The price at each index belongs to the item at the same index. It returns an array of strings with one line per item, like "1. apple: 3": the item's number, a dot and a space, the item, a colon and a space, then the price. Numbers start at 1, and the lines keep the order of items.
For items = ["apple", "bread", "milk"] and prices = [3, 2, 4] the result is ["1. apple: 3", "2. bread: 2", "3. milk: 4"]. A repeated item such as "pen" gets a line each time, and a price of 0 is printed as 0.
Approach
Each line needs three things: a count, an item and a price. Two tools supply them. zip(items, prices) pairs each item with the price at the same position, so no index is needed to look up the price. enumerated() then adds a count to each pair. The count starts at 0, so the line uses i + 1.
The loop builds one string per pair and appends it to a result array, so the lines come out in the order of items.
For the first example, the loop sees three steps: count 0 with the pair ("apple", 3), count 1 with ("bread", 2), and count 2 with ("milk", 4). Adding 1 to each count gives the line numbers 1, 2 and 3. Because the numbering comes from the loop and not from the items, repeated items still get distinct numbers.
Step by step
- Start with an empty array:
var lines = [String](). It is avarbecause lines are appended to it. - Loop over
zip(items, prices).enumerated()and name the parts: the counti, then the pair(item, price)in its own parentheses. The count comes first. - Append one line built with string interpolation:
"\(i + 1). \(item): \(price)". The+ 1makes the first line number 1. - Return
linesafter the loop.
The solution in Swift
class Solution {
func priceList(_ items: [String], _ prices: [Int]) -> [String] {
var lines = [String]()
for (i, (item, price)) in zip(items, prices).enumerated() {
lines.append("\(i + 1). \(item): \(price)")
}
return lines
}
}
Swift details:
- Nested unpacking. The elements of
zip(items, prices).enumerated()look like(0, ("apple", 3)). The pattern(i, (item, price))mirrors that shape. Writing(i, item, price)doesn't compile: the compiler reports that the pattern cannot match values of that nested type. - Interpolation. Inside a string,
\( ... )evaluates an expression and inserts the result. The expression can be arithmetic, as in\(i + 1). Integers are converted to text automatically. zipstops at the shorter array. The constraints promise equal lengths, so every item gets a line. If the arrays had different lengths, the extra entries of the longer one would be skipped silently.
Edge cases and common mistakes
- One item.
items = ["tea"],prices = [5]gives["1. tea: 5"]. - Repeated items.
["pen", "pen", "ink"]with[2, 3, 0]gives["1. pen: 2", "2. pen: 3", "3. ink: 0"]. Each position is its own line, so nothing is merged. - Numbering from 0. Printing
iinstead ofi + 1gives"0. apple: 3". - Swapped parts. The count is first and the pair second.
for ((item, price), i) in ...doesn't match the order ofenumerated(). - Missing spaces or punctuation. The lines must match the format exactly: a dot and a space after the number, and a colon and a space before the price.
Testing
["apple", "bread", "milk"]with[3, 2, 4]gives["1. apple: 3", "2. bread: 2", "3. milk: 4"].["tea"]with[5]gives["1. tea: 5"].["pen", "pen", "ink"]with[2, 3, 0]gives["1. pen: 2", "2. pen: 3", "3. ink: 0"].
