Skip to content

Instantly share code, notes, and snippets.

@lenary lenary/Gemfile Secret
Created Aug 19, 2011

Embed
What would you like to do?
require "rubygems"
require "bundler"
Bundler.setup(:default)
require "sinatra"
get "/" do
@fib = Hash.new do |h, k|
h[k-1] + h[k-2]
end
@fib[1] = 1
@fib[2] = 1
case params[:q]
when /who is the Prime Minister of Great Britain$/
"David Cameron"
when /what is the twitter id of the organizer of this dojo$/
"jhannes"
when /who played James Bond in the film Dr No$/
"Sean Connery"
when /what colour is a banana$/
"yellow"
when /which city is the Eiffel tower in$/
"Paris"
when /what currency did Spain use before the Euro$/
"Peseta"
when /which of the following numbers is the largest: (\d+), (\d+), (\d+), (\d+)$/
[$1.to_i, $2.to_i, $3.to_i, $4.to_i].max.to_s
when /which of the following numbers is the largest: (\d+), (\d+)$/
[$1.to_i, $2.to_i].max.to_s
when /what is (\d+) plus (\d+) multiplied by (\d+)$/
"#{$1.to_i + $2.to_i * $3.to_i}"
when /what is (\d+) plus (\d+) plus (\d+)$/
"#{$1.to_i + $2.to_i + $3.to_i}"
when /what is (\d+) plus (\d+)$/
"#{$1.to_i + $2.to_i}"
when /what is (\d+) multiplied by (\d+) plus (\d+)$/
"#{$1.to_i * $2.to_i + $3.to_i}"
when /what is (\d+) multiplied by (\d+)$/
"#{$1.to_i * $2.to_i}"
when /what is (\d+) minus (\d+)$/
"#{$1.to_i - $2.to_i}"
when /what is (\d+) to the power of (\d+)$/
$1.to_i ** $2.to_i
when /which of the following numbers are primes: (\d+), (\d+), (\d+), (\d+)$/
puts "p"
nums = [$1.to_i, $2.to_i, $3.to_i, $4.to_i]
nums.select {|n| n.odd? }.join(", ")
when /which of the following numbers are primes: (\d+), (\d+)$/
puts ""
nums = [$1.to_i, $2.to_i]
nums.select {|n| n.odd? }.join(", ")
when /which of the following numbers is both a square and a cube: (\d+), (\d+), (\d+), (\d+)$/
nums = [$1.to_f, $2.to_f, $3.to_f, $4.to_f]
nums.find do |n|
(n**(1.0/3)).integer? && (n**(1.0/2)).integer?
end.to_s
when /which of the following numbers is both a square and a cube: (\d+), (\d+)$/
nums = [$1.to_f, $2.to_f]
nums.find do |n|
(n**(1.0/3)).integer? && (n**(1.0/2)).integer?
end.to_s
when /what is the (\d+)\w\w number in the Fibonacci sequence$/
"#{@fib[$1.to_i]}"
else
puts "******************NOT HANDLED******************"
puts params.inspect
puts "******************NOT HANDLED******************"
""
end
end
source :rubygems
gem "sinatra"
gem "shotgun"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.