Tracks
/
Swift
Swift
/
Exercises
/
Santa's Swifty Helper
Santa's Swifty Helper

Santa's Swifty Helper

Learning Exercise

Introduction

Tuples group multiple values into a single compound value. Unlike arrays, the values in a tuple do not need to share the same type, and a tuple's size is fixed once created.

Create a tuple by enclosing comma-separated values in parentheses:

let order = ("biscuits", 2.99, 3)

You can also specify type annotations explicitly:

let order: (String, Double, Int) = ("biscuits", 2.99, 3)

To extract individual elements from an unnamed tuple, use zero-based index syntax (.0, .1, etc.):

let item = order.0     // "biscuits"
let quantity = order.2 // 3

Named Tuples

You can give names to individual tuple elements when defining the tuple:

let order = (item: "biscuits", price: 2.99, quantity: 3)

Naming elements makes code more readable and allows you to access elements using dot notation with their labels:

let item = order.item         // "biscuits"
let quantity = order.quantity // 3

You can also include element names in the type annotation:

let order: (item: String, price: Double, quantity: Int) = (item: "biscuits", price: 2.99, quantity: 3)

Modifying Tuples

If a tuple is declared as a variable (var), you can update its elements:

var order = ("biscuits", 2.99, 3)
order.0 = "cookies"
order.1 = 1.99
order.2 = 5

print(order)
// Prints ("cookies", 1.99, 5)

Returning Multiple Values

While Swift functions can only return a single value, returning a tuple lets you package and return multiple values from a function:

func divide(_ dividend: Int, by divisor: Int) -> (quotient: Int, remainder: Int) {
    let quotient = dividend / divisor
    let remainder = dividend % divisor
    return (quotient, remainder)
}

let result = divide(14, by: 4)
print("Quotient: \(result.quotient), Remainder: \(result.remainder)")
// Prints "Quotient: 3, Remainder: 2"

Tuple Decomposition

You can decompose (unpack) a tuple's values into separate constants or variables:

let order = ("biscuits", 2.99, 3)
let (item, price, quantity) = order

print(item) // Prints "biscuits"

Tuple decomposition also works directly in for-in loops:

let orders = [("biscuits", 2.99, 3), ("cookies", 1.99, 5)]

for (item, price, quantity) in orders {
    print("Item: \(item), Price: \(price), Quantity: \(quantity)")
}

Instructions

You are an elf working in Santa Claus' logistics division and you have been given a pair of tasks from the boss for upgrading the system's software.

1. Get name of the toy

The system needs to be able to get the name of a toy from the database. The database is made of tuples of type (name: String, quantity: Int).

Implement the function getName(_:), that takes an argument which hold a tuple of type (name: String, quantity: Int).

let toy = (name: "Doll", quantity: 10)
getName(toy)
// Returns "Doll"

2. Add a new toy to the database

The toy market is a quick and changing market, what is popular changes very rapidly. Thereby a function which creates a new toy is needed.

Implement the function createToy(name:amount:), that takes an argument name which holds the name of the toy, and amount which holds the quantity of the toy.

createToy(name: "Doll", amount: 10)
// Returns (name: "Doll", amount: 10)

3. Update quantity of a toy

The elfs in santas workshop is constantly making more toys, and santa needs to keep track of the quantity of each toy. To do this, the system needs to be able to update the quantity of a toy in the database.

Implement the function updateQuantity(_:toy:Amount:), that takes the arguments:

  • toys which holds an array of tuples of type (name: String, quantity: Int).
  • toy which holds the name of the toy to update.
  • amount which holds the new quantity of the toy.

You can assume that the toy is already in the database, and don't need to check for that.

let toys = [(name: "Doll", quantity: 10), (name: "Car", quantity: 5), (name: "Train", quantity: 3)]
updateQuantity(toys, toy: "Doll", amount: 15)
// Returns [(name: "Doll", quantity: 15), (name: "Car", quantity: 5), (name: "Train", quantity: 3)]

4. Add category key to a toy

Santa wants to be able to categorize the toys in the database. This is so they can easily find the toys when they need to make a delivery, and to be able to sort the toys in the database.

However, when the system was first implemented, the category key was forgotten. Now the system needs to be able to add a category key to a toy in the database.

Implement the function addCategory(_:category:), that takes the arguments:

  • toys which holds an array of tuples of type (name: String, quantity: Int).
  • category which holds the category key to add to the toys.

You can assume that the toy is already in the database, and don't need to check for that. You can also assume that there is no category key already in the toy.

let toys = [(name: "Car", quantity: 5), (name: "Train", quantity: 3)]
addCategory(toys, category: "Vehicle")
// Returns [(name: "Car", quantity: 5, category: "Vehicle"), (name: "Train", quantity: 3, category: "Vehicle")]
Edit via GitHub The link opens in a new window or tab
Swift Exercism

Ready to start Santa's Swifty Helper?

Sign up to Exercism to learn and master Swift with 35 concepts116 exercises, and real human mentoring, all for free.