APL Lives!
Time limit2sMemory limit1024 MB
Evaluate each line of a small APL subset with right-to-left operators, variables, rho, iota, and drop, then print the resulting vector or array.
- Level
Medium4 of 10
- Topics
- Simulation, Implementation, Recursion
- Solved
- No attempts yet
Problem
APL is an array programming language that uses a notation invented by Ken Iverson in 1957. This problem covers only a small subset of the language, which we call apl (small APL).
Each apl expression appears on a line by itself, and each expression has a value. The value is displayed right after the expression is entered. Operators in apl have no precedence like those in C, C++, or Java. They are applied right to left. Parentheses control the evaluation order. Operands of binary operators are also evaluated from right to left. Here are some examples of apl expressions.
Integers in the input are non-negative and less than . All computed integer values, including intermediate values, have absolute values less than . A matrix has at most entries. Variable names consist of one to three lowercase letters, and the names iota, rho, and drop are always operators. Exactly one space separates the elements of a statement (constants, variables, operators, and parentheses).
Constants in the input are vectors. Every intermediate value is a one-, two-, or three-dimensional array with positive dimensions. So 2 0 rho 1 2 3, 2 3 2 1 rho 5, and 3 drop iota 3 are illegal. The only arithmetic operators are + (addition), - (subtraction), and * (multiplication). Their operands must be conformable, as the examples show. Note that 1 1 rho 1 and 1 rho 1 have different shapes. The operand of iota evaluates to a one-element positive vector. The left operand of drop evaluates to a one-element non-negative vector, and its right operand evaluates to a vector. Both operands of rho evaluate to vectors.
Input
The input contains several test cases, each on a line by itself. Variables assigned in one test case keep their values for the following test cases. No expression exceeds 80 characters, including spaces. No test case produces an invalid result, such as an empty vector.
The last test case is followed by a line containing only the character #.
Output
For each test case, print a line with the case number and the input line. On the next line, print the result of evaluating the expression. A vector is printed as a single line of integers. An m by n array is printed as m lines of n values. An m by n by p array is printed as m arrays of size n by p, with a blank line between each n by p array. Values on the same line are separated by white space, and they do not need to be aligned in columns.