Skip to content

Instantly share code, notes, and snippets.

@ymnk
Created February 4, 2009 14:42
Show Gist options
  • Save ymnk/58126 to your computer and use it in GitHub Desktop.
Save ymnk/58126 to your computer and use it in GitHub Desktop.
import scala.io.Source
/**
* http://www.facebook.com/jobs_puzzles/index.php?puzzle_id=7
*/
object HoppityHop{
def main(arg:Array[String]){
val line = Source.fromFile(arg(0)).getLine(1)
// val Array(n) = line.split(" ").filter(_.length!=0)
// val r = "\\s*([0-9]+)\\s*".r
// val r(n) = line
// val n = line match {
// case r(n) => n
// }
// val n = "\\s*([0-9]+)\\s*".r.unapplySeq(line) match{
// case Some(n::_) => n
// case _ => ""
// }
// without mattern matching.
val n = "\\s*([0-9]+)\\s*".r.unapplySeq(line).get.first
object HoppityHop{
def unapply(n:Int):Option[String] =
if(n%3 == 0 && n%5 == 0) Some("Hop")
else if(n%3 == 0) Some("Hoppity")
else if(n%5 == 0) Some("Hophop")
else None
}
(1 to (n.toInt)).foreach{
case HoppityHop(s) => println(s)
case _ =>
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment