LoginSignup
0
0

More than 5 years have passed since last update.

逆FizzBuzz

Posted at

逆FizzBuzzをScalaで。

object ReverseFizzBuzz extends App {
  def fb(n:Int):Stream[Option[(Int, String)]] = {
      if(n%15 == 0) Some((n,"FizzBuzz"))
      else if(n%3==0) Some((n,"Fizz"))
      else if(n%5==0) Some((n,"Buzz"))
      else None
  }#::fb(n+1)

  def find(input:Array[String]):String = {
    val list = fb(1).take(15*(1+scala.math.ceil(input.length/7).toInt)).collect{case Some(a) => a}
    (0 until list.length).flatMap { i =>
      val sub = list.slice(i, i + input.length)
      if(sub.map(_._2).corresponds(input){(a,b) => a==b}) Some(sub.map(_._1)) else None
    }.foldLeft("NotFound"){(z,l) =>
      if(l.last-l.head < z.last-z.head) l.mkString(",") else z
    }
  }
  println(find(io.StdIn.readLine.split(",").map(_.trim)).mkString)
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0