This is my solution to the Silver and Gold challenges. Obviously, nobody understands zip codes outside the US, so I translated them into proper British ones first Any suggestions for improvements are welcome!
(BTW the formatting of the Gold solution is correct in the Xcode console, but doesn’t translate here…)
// Silver challenge
var postcodeArray : [String] = []
var Cheshire : Dictionary<String, Array<String>> = [:]
Cheshire["Chester"] = ["CH1", "CH2", "CH3", "CH4", "CH5"]
Cheshire["Crewe"] = ["CW1", "CW2", "CW3", "CW4", "CW5"]
Cheshire["Northwich"] = ["NW1", "NW2", "NW3", "NW4", "NW5"]
var postCodeString = " ["
for (key, value) in Cheshire {
for code in value {
if code != value.last {
postCodeString += "\(code), "
} else {
postCodeString += "\(code)"
}
}
if keyCount < Cheshire.count {
postCodeString += ","
} else {
postCodeString += "]"
}
}
print("Cheshire has the following post codes: \(postCodeString)")
// Cheshire has the following post codes: [CH1, CH2, CH3, CH4, CH5,CW1, CW2, CW3, CW4, CW5,NW1, NW2, NW3, NW4, NW5]
// Gold challenge
var postcodeArray : [String] = []
var Cheshire : Dictionary<String, Array<String>> = [:]
Cheshire["Chester"] = ["CH1", "CH2", "CH3", "CH4", "CH5"]
Cheshire["Crewe"] = ["CW1", "CW2", "CW3", "CW4", "CW5"]
Cheshire["Northwich"] = ["NW1", "NW2", "NW3", "NW4", "NW5"]
var postCodeString = " ["
var keyCount = 1
for (key, value) in Cheshire {
for code in value {
if code != value.last {
postCodeString += "\(code), "
} else {
postCodeString += "\(code)"
}
}
if keyCount < Cheshire.count {
postCodeString += "\n\t\t\t\t\t\t\t"
} else {
postCodeString += "]"
}
keyCount += 1
}
print("Cheshire has the following post codes: \(postCodeString)")
// Cheshire has the following post codes: [CH1, CH2, CH3, CH4, CH5
CW1, CW2, CW3, CW4, CW5
NW1, NW2, NW3, NW4, NW5]