Invert FizzBuzz by mtgto

import util.Random

object Solve {
  def main(args: Array[String]):Unit = {
    def isfb(n: Int): Boolean = {n%3==0||n%5==0}
    def fblist(start: Int, end: Int): Seq[String] = {
      (start to end).filter(isfb).map(x=>if(x%15==0)"FizzBuzz"else if (x%3==0)"Fizz"else"Buzz")
    }
    val limit = 200
    val map = collection.mutable.Map.empty[Seq[String],(Int,Int)]
    for (start <- 3 to limit; if (isfb(start))) {
      for (end <- start to limit; if (isfb(end))) {
        val list = fblist(start, end)
        map.get(list) match {
          case Some((s, e)) => if ((e-s) > (end-start)) { map += (list -> (start, end)) }
          case _ => map += (list -> (start, end))
        }
      }
    }
    def printAnswer(list: Seq[String]) = {
      map.get(list) match {
        case Some((s, e)) => println(s+" "+e)
        case _ => println()
      }
    }
    val sc = new java.util.Scanner(System.in)
    while (sc.hasNextLine()) {
      printAnswer(sc.nextLine().split(" ").toList)
    }
  }
}

Note that non-ascii characters in the above source code will be escaped (such as \x9f).

download

return to the top page