Skip to content

Instantly share code, notes, and snippets.

@jervi
Created October 20, 2011 17:42
Show Gist options
  • Save jervi/1301768 to your computer and use it in GitHub Desktop.
Save jervi/1301768 to your computer and use it in GitHub Desktop.
Extreme Startup - PJ QuizSolvers - Ruby
# encoding: utf-8
require 'rubygems'
require 'sinatra'
class ExtremeStartup
lastName = ""
def fib(n)
curr = 0
succ = 1
n.times do |i|
curr, succ = succ, curr + succ
end
return curr
end
def is_prime?(n)
return true if n <= 2
return false if n & 1 == 0
# runs the block for each number from 2 to sqrt(n)
# only returns true if the block *never* returns true
(2..(Math.sqrt(n).to_i)).none? { |i| n%i == 0 }
end
def answer(q)
if q =~ /what is (\d+) plus (\d+) plus (\d+)/
return ($1.to_i + $2.to_i + $3.to_i).to_s
elsif q =~ /what is the twitter id of the organizer of this dojo/
return "olemartin"
elsif q =~ /what currency did Spain use before the Euro/
return "peseta"
elsif q =~ /what is (\d+) plus (\d+) multiplied by (\d+)/
return ($1.to_i + $2.to_i * $3.to_i).to_s
elsif q =~ /what is (\d+) multiplied by (\d+) plus (\d+)/
return ($1.to_i * $2.to_i + $3.to_i).to_s
elsif q =~ /what is (\d+) plus (\d+)/
return ($1.to_i + $2.to_i).to_s
elsif q =~ /which city is the Eiffel tower in/
return "Paris"
elsif q =~ /who is the Prime Minister of Great Britain/
return "David Cameron"
elsif q =~ /who played James Bond in the film Dr No/
return "Sean Connery"
elsif q =~ /what colour is a banana/
return "yellow"
elsif q =~ /what is (\d+) to the power of (\d+)/
return ($1.to_i ** $2.to_i).to_s
elsif q =~ /what is (\d+) minus (\d+)/
return ($1.to_i - $2.to_i).to_s
elsif q =~ /what's santa clause's real name/
return "Julenissen"
elsif q =~ /what is your name/
return "PJ QuizSolvers"
elsif q =~ /my name is (\S+)\. what is my name/
lastName = $1
return lastName
elsif q =~ /what is my name/
return lastName
elsif q =~ /which of the following numbers is the largest: (.*)/
return $1.split(", ").collect{ |i| i.to_i }.max
elsif q =~ /what is (\d+) multiplied by (\d+)/
return ($1.to_i * $2.to_i).to_s
elsif q =~ /which of the following numbers is both a square and a cube: (.*)/
int_array = $1.split(", ").collect{ |i| i.to_i }
int_array.each do |i|
squareRoot = Math.sqrt(i).to_i
puts squareRoot
cubeRoot = i**(1.0/3.0)
puts cubeRoot.to_i
if (squareRoot * squareRoot == i and cubeRoot.to_i**3 == i)
return i
end
end
return ""
elsif q =~ /what is the (\d+)th number in the Fibonacci sequence/
return fib($1.to_i).to_s
elsif q =~ /which of the following numbers are primes: (.*)/
int_array = $1.split(", ").collect{ |i| i.to_i }
output = ""
int_array.each do |i|
output = output + i.to_s + ", " if is_prime?(i)
end
return output[0..output.length-3]
else
return ""
end
end
end
server = ExtremeStartup.new
configure do
set :port, 9392
end
get '/' do
puts "A request has arrived: " + params[:q]
answer_string = server.answer(params[:q]).to_s
puts "Answered: " + answer_string
answer_string
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment