Skip to content

Instantly share code, notes, and snippets.

@kmckinley
Created July 31, 2015 02:49
Show Gist options
  • Save kmckinley/30248e217ac2e481399e to your computer and use it in GitHub Desktop.
Save kmckinley/30248e217ac2e481399e to your computer and use it in GitHub Desktop.
class FizzBuzzBang {
func start() {
var results = ""
for i in 1...100 {
results += fizzTestResult(i)
results += buzzTestResult(i)
results += bangTestResult(i)
if count(results) == 0 {
results = "\(i)"
}
println("\(i) = \(results)")
results = ""
}
}
// Divide by 7 and check if the remainder is 0
func fizzTestResult(i: Int) -> String {
if (i % 7 == 0) {
return "fizz"
} else {
return ""
}
}
// Parse index to string and test for range of 7
func buzzTestResult(i: Int) -> String {
if ("\(i)".rangeOfString("7") != nil) {
return "buzz"
} else {
return ""
}
}
// Dividing by 11 with no remainder will tell use that it's a double
func bangTestResult(i: Int) -> String {
if (i % 11 == 0) {
return "bang"
} else {
return ""
}
}
}
let fbb = FizzBuzzBang()
fbb.start()
@aterranova-bv
Copy link

@kmckinley yeah, I agree - I think it depends largely on the method as well. If it's validation-type stuff I go with the former but that's really the only rule I really stick to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment