Created
July 31, 2017 20:46
-
-
Save sgade/fbeb9032f7279c7e17e4b6a714aa0aa8 to your computer and use it in GitHub Desktop.
A small fizz buzz implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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