Skip to content

Instantly share code, notes, and snippets.

@sferrini
Created April 23, 2016 12:59
Show Gist options
  • Save sferrini/6c03e4d751be22c9ddd4fb34ad92c2ac to your computer and use it in GitHub Desktop.
Save sferrini/6c03e4d751be22c9ddd4fb34ad92c2ac to your computer and use it in GitHub Desktop.
reports func must be more Swifty.
//: Playground - noun: a place where people can play
enum State {
case Happy
case Sad
}
struct Day {
var name: String
var state: State
}
struct Month {
var name: String
var days: [Day]
}
func reports(months: [Month]) -> (happy: Int, sad: Int, total: Int) {
var total: Int = 0
var happy: Int = 0
var sad: Int = 0
for month in months {
for day in month.days {
if day.state == .Happy {
happy += 1;
} else {
sad += 1;
}
total += 1;
}
}
return (happy, sad, total)
}
let months = [Month(name: "Month 1", days: [
Day(name: "Day 1", state: .Happy),
Day(name: "Day 2", state: .Happy),
Day(name: "Day 3", state: .Sad)]),
Month(name: "Month 2", days: [
Day(name: "Day 1", state: .Happy),
Day(name: "Day 2", state: .Sad),
Day(name: "Day 3", state: .Happy),
Day(name: "Day 4", state: .Happy)])]
let report = reports(months)
print("\(report.happy) happy days, \(report.sad) sad days, \(report.total) total")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment