Skip to content

Instantly share code, notes, and snippets.

@sgade
Created July 31, 2017 20:46
Show Gist options
  • Save sgade/fbeb9032f7279c7e17e4b6a714aa0aa8 to your computer and use it in GitHub Desktop.
Save sgade/fbeb9032f7279c7e17e4b6a714aa0aa8 to your computer and use it in GitHub Desktop.
A small fizz buzz implementation
// add your text blocks here
// values are added based on their sorted keys
let valueRepresentations = [
3: "Fizz",
5: "Buzz"
]
let sortedKeys = valueRepresentations.keys.sorted()
func representation(of value: Int) -> String {
var output = ""
for dictKey in sortedKeys {
if value % dictKey != 0 {
continue
}
if let dictValue = valueRepresentations[dictKey] {
output += "\(dictValue)"
}
}
if output == "" {
output = "\(value)"
}
return output
}
func printFizzBuzz(upTo maximum: Int) {
for i in 1...maximum {
let stringValue = representation(of: i)
print(stringValue)
}
}
printFizzBuzz(upTo: 100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment