import util.matching.Regex object ExtremeStartup { def answer(q: String): Any = { if (q.contains("what is your name")) return "Mikkel" if (q.contains("which city is the Eiffel tower in")) return "Paris" if (q.contains("what is the twitter id of the organizer of this dojo")) return "olemartin" if (q.contains("who is the Prime Minister of Great Britain")) return "David Cameron" if (q.contains("what colour is a banana")) return "yellow" if (q.contains("who played James Bond in the film Dr No")) return "Sean Connery" if (q.contains("what currency did Spain use before the Euro")) return "Peseta" """my name is (\w+). what is my name""".r.findFirstMatchIn(q) match { case Some(m) => return m.group(1) case _ => } // math """what is (\d+) ([a-z ]+) (\d+)""".r.findAllIn(q).matchData.foreach { m => { val num1 = m.group(1).toInt val op = m.group(2) val num2 = m.group(3).toInt op match { case "plus" => return num1 + num2 case "minus" => return num1 - num2 case "divided by" => return num1 / num2 case "multiplied by" => return num1 * num2 case "to the power of" => return math.pow(num1, num2) } } } """.*which of the following numbers is the largest: (.*)""".r.findFirstMatchIn(q) match { case Some(m) => return getNumberList(m.group(1)).max case None => } """which of the following numbers is both a square and a cube: (.*)""".r.findFirstMatchIn(q) match { case Some(m) => return getFilteredNumbersString(m, isSquareAndCube) case None => } """which of the following numbers are primes: (.*)""".r.findFirstMatchIn(q) match { case Some(m) => return getFilteredNumbersString(m, isPrime) case None => } """what is the (\d+)th number in the Fibonacci sequence""".r.findFirstMatchIn(q) match { case Some(m) => return fibonacci(m.group(1).toInt) case None => } "" } def getFilteredNumbersString(m: Regex.Match, p: Int => Boolean) = getNumberList(m.group(1)).filter(p(_)).mkString(", ") def getNumberList(numberString: String) = numberString.split(',').map(_.trim.toInt).toList def isPrime(n: Int) = (2 until n) forall (n % _ != 0) def fibonacci(num: Int) = fibonacciTR(num, 1, 0) def fibonacciTR(num: Int, nxt: Int, res: Int): Int = { num match { case 0 => res case _ => fibonacciTR(num - 1, nxt + res, nxt) } } def isSquareAndCube(x : Int) = math.sqrt(x) % 1 == 0 && math.cbrt(x) % 1 == 0 }