Last active
December 9, 2015 19:19
-
-
Save flakyfilibuster/4316453 to your computer and use it in GitHub Desktop.
Reverse Polish Notation Calculator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + + +') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 + + +') | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 + + +') | |
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
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).