-
-
Save samklr/3664557 to your computer and use it in GitHub Desktop.
BEKK kodekveld - Extreme Startup - refac
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
organization := "bekk" | |
name := "kodekveld" | |
version := "0.1.0-SNAPSHOT" | |
scalaVersion := "2.9.1" | |
seq(webSettings:_*) | |
libraryDependencies ++= Seq( | |
"org.scalatra" %% "scalatra" % "2.0.1", | |
"org.scalatra" %% "scalatra-scalate" % "2.0.1", | |
"org.scalatra" %% "scalatra-scalatest" % "2.0.1" % "test", | |
"org.eclipse.jetty" % "jetty-webapp" % "7.4.5.v20110725" % "jetty", | |
"org.eclipse.jetty" % "jetty-webapp" % "7.4.5.v20110725", | |
"javax.servlet" % "servlet-api" % "2.5" % "provided", | |
"org.scalatest" % "scalatest_2.9.0" % "1.6.1" % "test" | |
) | |
resolvers += "Sonatype OSS Snapshots" at "http://oss.sonatype.org/content/repositories/snapshots/" |
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 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 | |
} |
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 org.scalatra._ | |
class ExtremeStartupServlet extends ScalatraServlet { | |
get("/") { | |
val q = params("q") | |
println("QUESTION = " + q) | |
try { | |
val answer = ExtremeStartup.answer(q).toString | |
println("ANSWER = " + answer) | |
answer | |
} catch { | |
case _ => "" | |
} | |
} | |
notFound { | |
println(request.getRequestURI) | |
"" | |
} | |
} |
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 org.scalatra.test.scalatest.ScalatraFunSuite | |
class ExtremeStartupServletTest extends ScalatraFunSuite { | |
test("what is your name") { | |
answerTo("fc480360: what is your name") should be("Mikkel") | |
} | |
test("my name is") { | |
answerTo("ff4970f0: my name is bob. what is my name") should be ("bob") | |
} | |
test("addition") { | |
answerTo("05450bb0: what is 9 plus 9") should be("18") | |
} | |
test("multiplication") { | |
answerTo("05450bb0: what is 4 multiplied by 4") should be("16") | |
} | |
test("subtraction") { | |
answerTo("05450bb0: what is 9 minus 17") should be("-8") | |
} | |
test("largest number") { | |
answerTo("05450bb0: which of the following numbers is the largest: 12, 15, 31, 17") should be ("31") | |
} | |
test("prime numbers") { | |
answerTo("05450bb0: which of the following numbers are primes: 5, 7, 149, 720, 553, 257") should be ("5, 7, 149, 257") | |
} | |
test("fibonacci") { | |
answerTo("0b6768b0: what is the 14th number in the Fibonacci sequence") should be ("377") | |
} | |
test("square and cube") { | |
answerTo("05450bb0: which of the following numbers is both a square and a cube: 729, 303") should be ("729") | |
} | |
addServlet(classOf[ExtremeStartupServlet], "/*") | |
def question(q: String) : (String, String) = ("q", q) | |
def answerTo(q: String) : String = { | |
get("/", question(q)) { | |
status should be(200) | |
body | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment