I specifically wrote the Ruby one as short as possible. I could have made it more readable (and the first versions, in fact, were).
Example input: 1 2 + 4 * 5 6 7 - - -
So, basically. You have a stack. Read the string from left to right. 1 -> push onto stack (1) 2 -> push onto stack (1 2) + -> perform operation on two top items on stack, pop them, push result (3) 4 -> push onto stack (3 4) * -> perform, pop, push (12) 5 -> push (12 5) 6 -> push (12 5 6) 7 -> push (12 5 6 7) - -> perform, pop, push (12 5 -1) - -> perform, pop, push (12 6) - -> perform, pop, push (6) Output 6.
Notes on the Ruby implementation: string[string2] returns string2 if string contains it, nil (ie. null, which obviously evaluates to false) otherwise. Therefore the while clause string[" "] makes the loop continue as long as the string contains a space. This is usually used with regexpes. midval stores the current token, string the unread part of the input. eval performs the string given as if it was actual code. #{expression} in a string evaluates the expression, calls to_s on the result and substitutes it for the #{}. Also, negative indices on arrays take items from the right. In for a = [1, 2, 3, 4, 5], a[-1] = 5. "#{stack[-2]}#{midval}#{stack[-1]" could for example become "1+2", which then is evalled and returns 3 (which is pushed onto the stack).
stack.push(eval("1+2")).push(stack.pop(3)[2]) push returns the stack itself, hence the chainpush. In my implementation, the stack (1 2) first becomes (1 2 3), and then (3), this is because some operations have a set order (substraction, for example), and I can't do #{stack.pop}#{midval}#{stack.pop}, because that'd reverse the order. So I just push the result, pop three items, repush the result.
Once the string contains no more spaces, one last token is left in string. It has to be an operation, so I just perform it and return the result.
...that's pretty much everything I can think of.
|