Last active
August 29, 2015 14:04
-
-
Save toshi0383/9ab290b63e87c735c436 to your computer and use it in GitHub Desktop.
Swiftでfizzbuzzの試み ref: http://qiita.com/toshi0383/items/e111fa8b172efac366cc
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
let fizz = "Fizz" | |
let buzz = "Buzz" | |
func getGenerator(num: Int, out:String) -> (Int -> String) { | |
func getStr(targetNum:Int) -> String { | |
if targetNum % num == 0 { | |
return out | |
} else { | |
return "" | |
} | |
} | |
return getStr | |
} | |
for i in 1..<101 { | |
var result:String? = ""; | |
for s in [3,5] { | |
var getStr = getGenerator(s, s == 3 ? fizz : buzz) | |
result = result! + getStr(i) | |
} | |
if result != "" { | |
println(result!) | |
} else { | |
println(i) | |
} | |
} |
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
let fizz = "Fizz" | |
let buzz = "Buzz" | |
func fizzbuzz(a:Int) { | |
if (a % 15 == 0) { | |
println(fizz + buzz) | |
} else if (a % 5 == 0) { | |
println(buzz) | |
} else if (a % 3 == 0) { | |
println(fizz) | |
} else { | |
println(a) | |
} | |
} | |
var array = [Int]() | |
for i in 1..<100 { | |
array.append(i); | |
} | |
array.map{ fizzbuzz($0) }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment