Created
February 4, 2009 14:42
-
-
Save ymnk/58126 to your computer and use it in GitHub Desktop.
This file contains 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
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