Skip to content

Instantly share code, notes, and snippets.

@xuwei-k
Forked from yuroyoro/InverseFizzbuzz.scala
Created May 15, 2012 05:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save xuwei-k/2699263 to your computer and use it in GitHub Desktop.
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
//
// 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