Skip to content

Instantly share code, notes, and snippets.

@flakyfilibuster
Last active December 9, 2015 19:19
Show Gist options
  • Save flakyfilibuster/4316453 to your computer and use it in GitHub Desktop.
Save flakyfilibuster/4316453 to your computer and use it in GitHub Desktop.
Reverse Polish Notation Calculator
class RPNCalculator
def cleaner(idx)
@data_array.delete_at(idx)
@data_array.delete_at(idx-1)
end
def add(val,idx)
@data_array[idx-2] = (@data_array[idx-2].to_i).+((@data_array[idx-1].to_i))
cleaner(idx)
end
def sub(val,idx)
@data_array[idx-2] = (@data_array[idx-2].to_i).-((@data_array[idx-1].to_i))
cleaner(idx)
end
def mul(val,idx)
@data_array[idx-2] = (@data_array[idx-2].to_i).*((@data_array[idx-1].to_i))
cleaner(idx)
end
def evaluate(data)
@data_array = data.split
while @data_array.length > 1
@data_array.each_with_index do |val,idx|
if @data_array[idx] == '+'
add(val,idx)
elsif @data_array[idx] == '*'
mul(val,idx)
elsif @data_array[idx] == '-'
sub(val,idx)
end
end
end
return @data_array.first
end
end
# uncomment below to try out the RPN Calc:
#calc = RPNCalculator.new
#calc.evaluate('6 2 *')
#calc.evaluate('20 10 5 4 + * -')
#calc.evaluate('1 2 + 3 4 + *')
#calc.evaluate('1 2 3 4 + + +')
# NB: This one is not fully functional, but I wanted to leave it here anyways.
# I guess for the audiences viewing pleasure :)
class RPNCalculator
def initialize
@op = ['+','-','*']
end
def cleaner(index)
@ca.delete_at(index)
@ca.delete_at(index-1)
end
def add(value,index)
@ca[index-2] = (@ca[index-2].to_i).+((@ca[index-1].to_i)).to_s
cleaner(index)
end
def sub(value,index)
@ca[index-2] = (@ca[index-2].to_i).-((@ca[index-1].to_i)).to_s
cleaner(index)
end
def mul(value,index)
@ca[index-2] = (@ca[index-2].to_i).*((@ca[index-1].to_i)).to_s
cleaner(index)
end
def evaluate(n)
@ca = n.split
while @ca.length > 1
@ca.each_with_index do |value,index|
if @op.include?(value)
add(value,index) if @ca[index] == '+'
sub(value,index) if @ca[index] == '-'
mul(value,index) if @ca[index] == '*'
end
end
end
return @ca.first
end
end
# uncomment below to try out the RPN Calc:
#calc = RPNCalculator.new
#calc.evaluate('6 2 *')
#calc.evaluate('20 10 5 4 + * -')
#calc.evaluate('1 2 + 3 4 + *')
#calc.evaluate('1 2 3 4 + + +')
# even shorter, thanks to a method called 'eval'
# it evaluates a ruby expression within a string - just what I needed :)
# let's me get rid of the three different calculation functions and the if / elsif statements!
class RPNCalculator
def cleaner(idx)
@data_array.delete_at(idx)
@data_array.delete_at(idx-1)
end
def do_math(val,idx)
@data_array[idx-2] = eval("#{@data_array[idx-2]} #{val} #{@data_array[idx-1]}")
cleaner(idx)
end
def evaluate(data)
@data_array = data.split
while @data_array.length > 1
@data_array.each_with_index do |val,idx|
if ['+','*','-'].include? val
do_math(val,idx)
end
end
end
return @data_array[0].to_i
end
end
# uncomment below to try out the RPN Calc:
#calc = RPNCalculator.new
#calc.evaluate('6 2 *')
#calc.evaluate('20 10 5 4 + * -')
#calc.evaluate('1 2 + 3 4 + *')
#calc.evaluate('1 2 3 4 + + +')
@flakyfilibuster
Copy link
Author

So...my first attempt wasn't working in all cases...

Added a second which should do the trick.
Cleaner code, changed some variable names and got rid of the initialize method (not needed).

@flakyfilibuster
Copy link
Author

And another version with even more refactoring!
This time supersexy via eval method :)
I guess I should read through ALL of the core ruby documentation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment