-
-
Save xuwei-k/2699263 to your computer and use it in GitHub Desktop.
InverseFizzbuzz : http://www.jasq.org/2/post/2012/05/inverse-fizzbuzz.html
This file contains hidden or 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
// Inversefizzbuzz | |
// http://www.jasq.org/2/post/2012/05/inverse-fizzbuzz.html | |
// | |
// fork from https://gist.github.com/2699068 | |
object InverseFizzbuzz extends App { | |
def zzubzzif(pattern:Seq[String]) = { | |
def fizzbuzz(n:Int) = (n%3, n%5) match{ | |
case (0,0) => "fizzbuzz" | |
case (0,_) => "fizz" | |
case (_,0) => "buzz" | |
case _ => "" | |
} | |
val n = Stream.from(0).map(fizzbuzz).indexOfSlice(pattern) | |
(n until (pattern.size + n)) toList | |
} | |
def _p(s:Seq[String]) = { | |
println("pattern : %s" format (s.map{s => s.mkString("'", "", "'")})) | |
println(zzubzzif(s)) | |
println("") | |
} | |
_p(Seq("fizz")) | |
_p(Seq("buzz")) | |
_p(Seq("fizz","","buzz")) | |
_p(Seq("fizz","buzz")) | |
_p(Seq("buzz","fizz")) | |
_p(Seq("fizz","","buzz","fizz")) | |
_p(Seq("fizz","","","fizz")) | |
_p(Seq("fizz","","","fizz","buzz")) | |
// Result: | |
// | |
// pattern : List('fizz') | |
// List(3) | |
// | |
// pattern : List('buzz') | |
// List(5) | |
// | |
// pattern : List('fizz', '', 'buzz') | |
// List(3, 4, 5) | |
// | |
// pattern : List('fizz', 'buzz') | |
// List(9, 10) | |
// | |
// pattern : List('buzz', 'fizz') | |
// List(5, 6) | |
// | |
// pattern : List('fizz', '', 'buzz', 'fizz') | |
// List(3, 4, 5, 6) | |
// | |
// pattern : List('fizz', '', '', 'fizz') | |
// List(6, 7, 8, 9) | |
// | |
// pattern : List('fizz', '', '', 'fizz', 'buzz') | |
// List(6, 7, 8, 9, 10) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment