This page is still under construction.

Parts of this page are still being built. What you see may change.

APL Lives!

Time limit2sMemory limit1024 MB

Summary
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.

ExpressionDescription
var = 1 2 3Stores the vector 1 2 3 in var, replacing its previous value. The value of the expression is 1 2 3. The left operand of the = operator must be a variable.
var + 4Displays the value of var with 4 added to each element (result: 5 6 7). The stored var is not modified.
- / varDisplays the value of var as if a - operator were inserted between each element in each row (result: 2). If var has two dimensions, the result is a vector. If var has three dimensions, the result is a two-dimensional array. * / and + / behave the same way.
iota 5Generates a vector with the values 1 2 3 4 5.
2 2 rho 1 2 3 4Reshapes the vector 1 2 3 4 into a 2 by 2 array. 1 and 2 are in the first row, and 3 and 4 are in the second row.
2 2 rho 1 2 3 4 5 6Same result as above.
2 3 rho 1 2 3 4Another reshaping, yielding a first row of 1 2 3 and a second row of 4 1 2. If the right argument does not have enough elements, its elements are reused from the beginning, in row-major order.
2 drop iota 5Result: 3 4 5. Drops the two leading elements of iota 5.
1 2 * 3 4Result: 3 8. Element-wise multiplication. Operands must be conformable: they have the same shape, or at least one is a one-element vector (see the second example).
( ( a = 1 ) drop 1 2 3 ) - 5Result: -3 -2. Shows the use of parentheses.
a + ( a = 5 ) + a + ( a = 6 )Result: 22. Shows the evaluation order.

Integers in the input are non-negative and less than 10410^4. All computed integer values, including intermediate values, have absolute values less than 10410^4. A matrix has at most 10410^4 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.

Examples1

  1. Example 1

    Input
    var = 1 2 3
    var + 4
    - / var
    iota 5
    2 2 rho 1 2 3 4
    2 3 rho 1 2 3 4
    2 drop iota 4
    1 2 * 3 4
    ( ( a = 1 ) drop 1 2 3 ) – 5
    a + ( a = 5 ) + a + ( a = 6 )
    ( 2 2 rho 2 drop iota 6 ) + 100
    1 2 3 + 4 5 6
    2 3 rho 1 2 3 4 5 + 1 2 3 4 5
    + / 2 3 4 rho iota 2 * 3 * 4
    ( 2 4 5 rho iota 2 * 4 * 5 ) - 99
    #
    
    Expected output
    Case 1: var = 1 2 3
     1 2 3
    Case 2: var + 4
     5 6 7
    Case 3: - / var
     2
    Case 4: iota 5
     1 2 3 4 5
    Case 5: 2 2 rho 1 2 3 4
     1 2
     3 4
    Case 6: 2 3 rho 1 2 3 4
     1 2 3
     4 1 2
    Case 7: 2 drop iota 4
     3 4
    Case 8: 1 2 * 3 4
     3 8
    Case 9: ( ( a = 1 ) drop 1 2 3 ) - 5
     -3 -2
    Case 10: a + ( a = 5 ) + a + ( a = 6 )
     22
    Case 11: ( 2 2 rho 2 drop iota 6 ) + 100
     103 104
     105 106
    Case 12: 1 2 3 + 4 5 6
     5 7 9
    Case 13: 2 3 rho 1 2 3 4 5 + 1 2 3 4 5
     2 4 6
     8 10 2
    Case 14: + / 2 3 4 rho iota 2 * 3 * 4
     10 26 42
     58 74 90
    Case 15: ( 2 4 5 rho iota 2 * 4 * 5 ) - 99
     -98 -97 -96 -95 -94
     -93 -92 -91 -90 -89
     -88 -87 -86 -85 -84
     -83 -82 -81 -80 -79
     -78 -77 -76 -75 -74
     -73 -72 -71 -70 -69
     -68 -67 -66 -65 -64
     -63 -62 -61 -60 -59