Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
A CoffeeScript solution for @jhannes's evil Extreme Startup question set
# To install dependencies:
# $ npm install express mongoose connect-mongoose
fs = require "fs"
express = require 'express'
mongoose = require 'mongoose'
sessionStore = require("connect-mongoose")(express)
Schema = mongoose.Schema
mongo_uri = "mongodb://localhost/bodilpwnz"
console.log "Connecting to #{mongo_uri}"
mongoose.connect mongo_uri
# Optimised Fibonacci (stolen from the internets)
MAXIMUM_JS_FIB_N = 1476
fib_bits = (n) ->
bits = []
while n > 0
[n, bit] = divmodBasic n, 2
bits.push bit
bits.reverse()
return bits
fibFast = (n) ->
if n < 0
return
[a, b, c] = [1, 0, 1]
for bit in fib_bits n
if bit
[a, b] = [(a+c)*b, b*b + c*c]
else
[a, b] = [a*a + b*b, (a+c)*b]
c = a + b
return b
divmodBasic = (x, y) ->
return [(q = Math.floor x/y), (r = if x < y then x else x % y)]
# Nth root solver - no built-in cube root in JS ;(
nth_root = (num, nArg, precArg) ->
n = nArg || 2
prec = precArg || 12
x = 1
for i in [0..prec-1]
x = 1/n * ((n-1)*x + (num / Math.pow(x, n-1)))
x
cube_root = (n) -> nth_root n, 3
square_and_cube = (n) ->
(Math.floor(Math.sqrt n) * Math.floor(Math.sqrt n) == n) && (Math.floor(cube_root n) * Math.floor(cube_root n))
# Dumb prime checker
is_prime = (n) ->
if n < 4
return true
if n % 2 == 0
return false
for i in [3..(n - 1)]
if n % i == 0
return false
true
# Simple persistent global state (which turned out to be unnecessary)
state = JSON.parse fs.readFileSync "state", "utf-8"
save_state = ->
fs.writeFileSync "state", (JSON.stringify state), "utf-8"
# Configure the Express HTTP server
app = express.createServer()
# Configure an HTTP session handler using MongoDB to persist session state
app.use express.cookieParser()
app.use express.session
secret: "foo"
store: new sessionStore()
# The dumb C style answer function
answer = (q, req, res) ->
m = q.match /which of the following numbers is both a square and a cube: (.*)/
if m
nums = ((parseInt x, 10) for x in (m[1].split ", "))
nums = (x for x in nums when square_and_cube x) # I love list comprehension <3
return nums.join ", "
m = q.match /which of the following numbers is the largest: (.*)/
if m
nums = ((parseInt x, 10) for x in (m[1].split ", "))
return Math.max.apply null, nums
m = q.match /which of the following numbers are primes: (.*)/
if m
nums = ((parseInt x, 10) for x in (m[1].split ", "))
nums = (x for x in nums when is_prime x)
return nums.join ", "
m = q.match /what is (\d+) minus (\d+)/
if m
n1 = parseInt m[1]
n2 = parseInt m[2]
return n1 - n2
m = q.match /what is (\d+) multiplied by (\d+) plus (\d+)/
if m
n1 = parseInt m[1]
n2 = parseInt m[2]
n3 = parseInt m[3]
return n1 * n2 + n3
m = q.match /what is (\d+) plus (\d+) multiplied by (\d+)/
if m
n1 = parseInt m[1]
n2 = parseInt m[2]
n3 = parseInt m[3]
return n1 + n2 * n3
m = q.match /what is (\d+) plus (\d+)/
if m
n1 = parseInt m[1]
n2 = parseInt m[2]
return n1 + n2
m = q.match /what is (\d+) divided by (\d+)/
if m
n1 = parseInt m[1]
n2 = parseInt m[2]
return n1 / n2
m = q.match /what is (\d+) multiplied by (\d+)/
if m
n1 = parseInt m[1]
n2 = parseInt m[2]
return n1 * n2
m = q.match /what is the (\d+).* in the Fibonacci sequence/
if m
n = parseInt m[1]
return fibFast n
m = q.match /my name is (\w+). what is my name/
if m
req.session.my_name = m[1]
return req.session.my_name
if q.match /what is my name/
return req.session.my_name
if q.match /what currency did Spain use before the Euro/
return "peseta"
if q.match /what colour is a banana/
return "yellow"
if q.match /who is the Prime Minister of Great Britain/
return "David Cameron"
if q.match /who played James Bond in the film Dr No/
return "Sean Connery"
if q.match /what is the twitter id of the organizer of this dojo/
return "jhannes"
if q.match /which city is the Eiffel tower in/
return "Paris"
if q.match /what products do you have for sale \(comma separated\)/
return ("product#{x}" for x in [1..3]).join ", "
m = q.match /how many dollars does one product(\d+) cost/
if m
n = parseInt m[1], 10
return n + ".00"
m = q.match /please put (\d+) product(\d+) in my shopping cart/
if m
quantity = parseInt m[1], 10
cost = parseInt m[2], 10
total = quantity * cost
try
req.session.total += total
catch error
req.session.total = total
return "okay"
if q.match /what is my order total/
return req.session.total + ".00"
"Bruce Schneier" # Default answer, ref. http://www.schneierfacts.com/fact/1
app.get '/', (req, res) ->
q = req.param("q")
console.log "q:\"#{q}\""
a = answer q, req, res
console.log "a:\"#{a}\""
res.send a
app.listen 1337
console.log "Listening on http://0.0.0.0:1337/"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.