## DEV Community is a community of 626,822 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

# Discussion on: Daily Challenge #216 - Rainfall

kesprit

My Swift solution, not the best ☹️:

``````func fetchCity(datas: String) -> [String: [Double]] {
func fetchCityDatas(datas: String) -> [Double] {
datas.split(separator: ",").map {
Double(\$0.split(separator: .init(" ")).last ?? "") ?? 0
}
}

return data.split(separator: "\n").reduce(into: [String:[Double]]()) {
let data = \$1.split(separator: ":")
\$0[String(data.first ?? "")] = fetchCityDatas(datas: String(data.last ?? ""))
}
}

func mean(town: String, datas: String) -> Double {
let convertedDatas = fetchCity(datas: datas)
guard convertedDatas.contains(where: { \$0.key == town }), let townDatas = convertedDatas[town] else { return -1 }

let sum = townDatas.reduce(into: 0.0) { \$0 += \$1 }

return sum / Double(townDatas.count)
}

func variance(town: String, datas: String) -> Double {
let meanValue = mean(town: town, datas: datas)
let convertedCities = fetchCity(datas: datas)
guard let convertedDatas = convertedCities[town] else { return 0 }

return convertedDatas.map { pow((meanValue - \$0),2) }
.reduce(into: 0.0) { \$0 += \$1 } / Double(convertedDatas.count)
}

mean(town: "Rome",datas: data1)
mean(town: "Beijing",datas: data)
variance(town: "Beijing",datas: data1)
variance(town: "Lima",datas: data)
``````