public
Created

A CoffeeScript solution for @jhannes's evil Extreme Startup question set

  • Download Gist
extremestartup.coffee
CoffeeScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
# 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/"

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.