Editorial for ECOO '12 R3 P3 - Steam Arithmetic


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

The name "Steam" is a spoof of "Scheme", an actual dialect of Lisp that is currently in use and is taught as a first programming language at Waterloo and some other universities. You might think that programming this in a Lisp dialect would give an advantage, but because of the requirement that the program read the data from a file and interpret it, it is not necessarily so easy.

Recommended Approach

Recursion is the best approach here. Write a method (or procedure or function) that takes a 3-element list, separates the operator and the operands and applies the operator to the operands. If an operand is a list, it should be recursively evaluated before being applied.

The restrictions on spacing and the restriction of operators and operands to a single character make it easy to process the list by pulling apart the string one character at a time. There is no need for any further tokenizing, except in the case of lists as operands.

To isolate list operands, you could use a bracket counting method. That is, start to the right of the first open bracket with the counter set to 1. Step through the string one character at a time. Increment the counter at an open bracket, and decrement it at a closed bracket. When the counter hits 0 you've found the end of the list.

Test Cases

In the test cases, I tried to hit a couple of simple cases, then progressively harder ones. I tried for a few cases with very deep nesting, some with tail recursion, and some with head recursion (https://en.wikipedia.org/wiki/Tail_call).


Comments

There are no comments at the moment.