Nobody’s Decision

Dr. Nobody is a bit confused about not being able to remember the past and the fact that he can see the future. You are given an array containing pairs of decisions he has to make and choices he can take throughout his life. Help Dr. Nobody by counting the number of decisions, the number of choices, the number of possible outcomes and printing them.

Example

Input:

let life = [("Who will you go with?", ["mother", "father"]), ("Who will you marry?", ["Anna", "Elise", "Jean"])]

Expected value/output:

decisions = 2
choices = 5
outcomes = 6

[collapse]
Hint

The number of decisions is the number of elements in the array. The number of choices is the sum of the number of elements contained in the arrays inside the tuples. The number of outcomes is their product.

[collapse]
Solution

let life = [("Who will you go with?", ["mother", "father"]), ("Who will you marry?", ["Anna", "Elise", "Jean"])]

func numDecisions(_ life : [(String, [String])]) -> Int {
    var decisions : Int = life.count
    return decisions
}

func numChoices(_ life : [(String, [String])]) -> Int {
    var choices : Int = 0
    for decision in life {
        choices += decision.1.count
    }
    return choices
}

func numOutcomes(_ life : [(String, [String])]) -> Int {
    var outcomes : Int = 1
    for decision in life {
        outcomes *= decision.1.count
    }
    return outcomes
}

var decisions = numDecisions(life)
var choices = numChoices(life)
var outcomes = numOutcomes(life)
print(decisions)
print(choices)
print(outcomes)

[collapse]
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading...
Subscribe
We send about one email per week with our latest tutorials and updates
Never display this again :)