Skip to content

Instantly share code, notes, and snippets.

@mikkelbd
Created October 20, 2011 17:51
Show Gist options
  • Save mikkelbd/1301792 to your computer and use it in GitHub Desktop.
Save mikkelbd/1301792 to your computer and use it in GitHub Desktop.
BEKK kodekveld - Extreme Startup
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.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"
)
resolvers += "Sonatype OSS Snapshots" at "http://oss.sonatype.org/content/repositories/snapshots/"
object Kodekveld {
def answer(q: String): String = {
if (q.contains("what is your name"))
return svar("Mikkel")
if (q.contains("which city is the Eiffel tower in"))
return svar("Paris")
if (q.contains("what is the twitter id of the organizer of this dojo"))
return svar("@olemartin")
if (q.contains("who is the Prime Minister of Great Britain"))
return svar("David Cameron")
if (q.contains("what colour is a banana"))
return svar("yellow")
if (q.contains("who played James Bond in the film Dr No"))
return svar("Sean Connery")
if (q.contains("what currency did Spain use before the Euro"))
return svar("peseta")
// math
val pat = """(\d+) ([a-z ]+) (\d+)""".r
pat.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 svar((num1 + num2) toString)
case "minus" => return svar((num1 - num2) toString)
case "divided by" => return svar((num1 / num2) toString)
case "multiplied by" => return svar((num1 * num2) toString)
case "to the power of" => return svar(scala.math.pow(num1, num2) toString)
}
}
}
if (q.startsWith("which of the following numbers is the largest: ")) {
val numbers = q.substring(q.indexOf(": ") + 2)
val nums = numbers.split(",");
return svar(nums.map(f => f.trim.toInt).max.toString)
}
if (q.startsWith("which of the following numbers are primes: ")) {
val numbers = q.substring(q.indexOf(": ") + 2)
val nums = numbers.split(",");
}
if (q.contains("my name is"))
q.substring(q.indexOf("."))
svar("")
}
def svar(svar: String) : String = {
println("SVAR: " + svar)
svar
}
}
import org.scalatest.matchers._
import org.scalatest.{FunSuite}
class KodekveldTest extends FunSuite with ShouldMatchers {
test("what is your name") {
Kodekveld.answer("what is your name") should be ("Mikkel")
}
test("addition") {
Kodekveld.answer("what is 4 plus 4") should be("8")
}
test("multiplication") {
Kodekveld.answer("what is 4 multiplied by 4") should be("16")
}
test("large numbers") {
Kodekveld.answer("which of the following numbers is the largest: 12, 15, 5, 17") should be ("17")
}
}
import org.scalatra._
class MyScalatraServlet extends ScalatraServlet {
get("/") {
val q = request.getParameter("q");
println("q = " + q)
try {
Kodekveld.answer(q)
} catch {
// Default return empty and 200 OK and NOT HTTP 501
case _ => ""
}
}
notFound {
print(request.getRequestURI)
resourceNotFound()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment