[2020][ruby][18] refactor

pull/2/head
Alpha Chen 4 years ago
parent 5ecce65bb5
commit 5bcd40bef6

@ -35,21 +35,21 @@ def evaluate(input)
ss = StringScanner.new(input) ss = StringScanner.new(input)
until ss.eos? until ss.eos?
ss.scan(/\s*/) ss.scan(/\s*/)
case stack << case
when n = ss.scan(/\d+/) when n = ss.scan(/\d+/)
stack << stack.pop[n.to_i] stack.pop[n.to_i]
when ss.scan(/\+/) when ss.scan(/\+/)
stack << ->(x) { stack.pop + x } ->(x) { stack.pop + x }
when ss.scan(/\*/) when ss.scan(/\*/)
stack << ->(x) { stack.pop * x } ->(x) { stack.pop * x }
when ss.scan(/\(/) when ss.scan(/\(/)
stack << ->(x) { x } ->(x) { x }
when ss.scan(/\)/) when ss.scan(/\)/)
n = stack.pop n = stack.pop
stack << stack.pop[n] stack.pop[n]
else else
fail fail
end end
end end
stack.pop stack.pop

Loading…
Cancel
Save