Start learning free

LESSON 277 OF 678 · TUPLES, SORTING & BUILT-INS

Price List in Swift: Numbered Lines with zip and enumerated

Practice Read the full explanation

You get an array of strings items and an array of integers prices. Return an array of strings with one line per item.

The price at each index belongs to the item at the same index.

A line looks like "1. apple: 3". It has the item's number, then the item, then its price.

The numbers start at 1. The lines keep the order of items.

 

Example 1:

Input: items = ["apple", "bread", "milk"], prices = [3, 2, 4]
Output: ["1. apple: 3", "2. bread: 2", "3. milk: 4"]
Explanation: The apple is first and costs 3.
The bread is second and costs 2.
The milk is third and costs 4.

Example 2:

Input: items = ["tea"], prices = [5]
Output: ["1. tea: 5"]
Explanation: One item gives one line, number 1.

Example 3:

Input: items = ["pen", "pen", "ink"], prices = [2, 3, 0]
Output: ["1. pen: 2", "2. pen: 3", "3. ink: 0"]
Explanation: The shop sells two kinds of pen.
Each one gets its own line, number and price.
The ink is free, so its price is 0.

 

Constraints:

  • items and prices have the same length.
  • They have 1 to 100 entries.
  • Each item is one word of lowercase letters.
  • Each price is a whole number from 0 to 1000.
STEP 1 OF 4
    Ctrl + Enter to check

    SWIFT · TUPLES, SORTING & BUILT-INS

    Price List in Swift, explained

    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

    1. Start with an empty array: var lines = [String](). It is a var because lines are appended to it.
    2. Loop over zip(items, prices).enumerated() and name the parts: the count i, then the pair (item, price) in its own parentheses. The count comes first.
    3. Append one line built with string interpolation: "\(i + 1). \(item): \(price)". The + 1 makes the first line number 1.
    4. Return lines after 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.
    • zip stops 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 i instead of i + 1 gives "0. apple: 3".
    • Swapped parts. The count is first and the pair second. for ((item, price), i) in ... doesn't match the order of enumerated().
    • 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"].
    Keep going with Swift500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free