Skip to content

Instantly share code, notes, and snippets.

@Jberczel
Last active August 29, 2015 14:09
Show Gist options
  • Save Jberczel/7b9cdc51c31f496dfa82 to your computer and use it in GitHub Desktop.
Save Jberczel/7b9cdc51c31f496dfa82 to your computer and use it in GitHub Desktop.
WordProblem Benchmarking
require_relative 'other_word_problem'
require_relative 'word_problem'
require 'benchmark'
TEST_COUNT = 10000
Benchmark.bmbm do |b|
b.report("myWordy") do
TEST_COUNT.times do |i|
WordProblem.new('What is 1 plus 1 plus 1?').answer
WordProblem.new('What is 1 plus 5 minus -2?').answer
WordProblem.new('What is 20 minus 4 minus 13?').answer
WordProblem.new('What is 17 minus 6 plus 3?').answer
WordProblem.new('What is 2 multiplied by -2 multiplied by 3?').answer
WordProblem.new('What is -3 plus 7 multiplied by -2?').answer
WordProblem.new('What is -12 divided by 2 divided by -3?').answer
end
end
b.report("otherWordy") do
TEST_COUNT.times do |i|
OtherWordProblem.new('What is 1 plus 1 plus 1?').answer
OtherWordProblem.new('What is 1 plus 5 minus -2?').answer
OtherWordProblem.new('What is 20 minus 4 minus 13?').answer
OtherWordProblem.new('What is 17 minus 6 plus 3?').answer
OtherWordProblem.new('What is 2 multiplied by -2 multiplied by 3?').answer
OtherWordProblem.new('What is -3 plus 7 multiplied by -2?').answer
OtherWordProblem.new('What is -12 divided by 2 divided by -3?').answer
end
end
end
class WordProblem
attr_reader :text
def initialize(text)
@text = text
validate_operators
validate_numbers
end
def answer
ops = operators.dup
numbers.drop(1).reduce(numbers.first) do |a,b|
operator = ops.shift
calculate.fetch(operator)[a,b]
end
end
OPERATORS = /plus|minus|multiplied|divided|raised/
def operators
@operators ||= text.scan(OPERATORS).map(&:to_sym)
end
def numbers
@numbers ||= text.scan(/-?\d+/).map(&:to_i)
end
def calculate
{
:plus => lambda { |a,b| a + b },
:minus => lambda { |a,b| a - b },
:multiplied => lambda { |a,b| a * b },
:divided => lambda { |a,b| a / b },
:raised => lambda { |a,b| a **b }
}
end
private
def validate_operators
raise ArgumentError if operators.empty?
end
def validate_numbers
raise ArgumentError if numbers.empty?
end
end
require 'forwardable'
class Fixnum
alias_method :plus, :+
alias_method :minus, :-
alias_method :multiplied, :*
alias_method :divided, :/
end
class OtherWordProblem
KNOWN_OPERATIONS = [:plus, :minus, :multiplied, :divided].freeze
extend Forwardable
def_delegators :Fixnum, *KNOWN_OPERATIONS
def initialize(question)
@question = question
validate_operators
validate_numbers
end
def answer
numbers.drop(1).reduce(numbers.first) do |a,b|
a.public_send(operators.shift, b)
end
end
def operators
@operators ||= question.scan(/#{operator_test}/).map(&:to_sym)
end
def numbers
question.scan(/-?\d+/).map(&:to_i)
end
private
attr_reader :question
def validate_operators
raise ArgumentError if operators.empty?
end
def validate_numbers
raise ArgumentError if numbers.empty?
end
def operator_test
@operator_test ||= KNOWN_OPERATIONS.join('|')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment