Recently I was given a problem to solve and I don't even know where to start. It's related to a project I'm working on but I'm failing to wrap my head around the logic.
let input: [(Int, Int)] = [(1, 3), (1, 4), (2, 7), (1, 8), (3, 4), (2, 5)]
let expectedOutput: [Int: [Int]] = [1: [3, 4, 8], 2: [7, 5], 3: [4]]
var actualOutput: [Int: [Int]] = [:]
/* Print "Success" by using 'input' to fill 'actualOutput'
and checking 'expectedOutput' == 'actualOutput' */
if expectedOutput == actualOutput { print("Success") } else { print("Failure") }
/* My Feeble Attempts */
/* Feeble Attempt 1 */
for x in input {
print(x)
print((x.0, x.1))
if [x.1] != actualOutput[x.0, default: [x.1]] {
actualOutput[x.0, default: [x.1]] += [x.1]
} else if [x.1] == actualOutput[x.0, default: [x.1]] { continue }
print("INSIDE", actualOutput)
}
print("OUTSIDE", actualOutput)
/* Feeble Attempt 2 */
for x in input {
if x.1 == expectedOutput.keys.0 {
}
}
My hint was "dictionary subscripting" but I clearly don't know how to take it. Would someone be able to point me in the right direction please?
Thanks
- L33t-HAXXOR