Last active
August 29, 2015 14:26
-
-
Save nicholas-balcolm/aaacf7ebedbee9f4774c to your computer and use it in GitHub Desktop.
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
extension Int | |
{ | |
func isMultipleOf(x: Int) -> Bool | |
{ | |
return (self % x) == 0 | |
} | |
func hasDigit (x: Int) -> Bool | |
{ | |
var powTen = 1 | |
while (powTen < self) | |
{ | |
if ((self / (powTen)) % 10 == x) | |
{ | |
return true | |
} | |
powTen *= 10 | |
} | |
return false | |
} | |
} | |
func say( theWord : String, | |
forTheNumber i : Int, | |
whenMultipleOf x : Int, | |
orHasDigit includeDigit : Bool = false ) -> Bool | |
{ | |
if( i.isMultipleOf(x) | |
|| (includeDigit && i.hasDigit(x))) | |
{ | |
print(theWord) | |
return true | |
} | |
return false | |
} | |
func play ( with wordNumberPairs : [(String, Int)] = [("Fizz", 3),("Buzz", 5)], | |
from theStartPoint : Int = 1, | |
to theEndPoint: Int = 100, | |
includingDigits includeDigit : Bool = false) | |
{ | |
for currentNumber in [Int](theStartPoint...theEndPoint) | |
{ | |
var hasSpoken = false | |
for (theWord, theWordsNumber) in wordNumberPairs | |
{ | |
hasSpoken = say ( theWord, | |
forTheNumber: currentNumber, | |
whenMultipleOf: theWordsNumber, | |
orHasDigit: includeDigit) || hasSpoken | |
} | |
if(hasSpoken) | |
{ | |
println() | |
} | |
else | |
{ | |
println(currentNumber) | |
} | |
} | |
} | |
play() // the game | |
println("change the rules") | |
play( with: [("Fizz", 3),("Buzz", 5),("Flop",7)], | |
from: 20, | |
to: 200, | |
includingDigits: true ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment