Obsolete Programming
Statement
Goal
How is your CSB ? Very bad, in fact, and you have been moved from the prestigious Bot Division to the awful Legacy Softwares Division where you must maintain obsolete and crappy code that runs 90% of your corporation's business.Today you must write an interpreter for a strange and forgotten language, written in a narrow character set : only uppercase letters, digits, minus sign, space and newline.
Basically the language is based on RPN (Reverse Polish Notation) with the abilty to define new instructions.
Lines are split in instructions separated by space(s).
If the instruction is a
The operations (ADD, SUB, MUL, DIV, MOD) pop the last two numbers out of the stack and push the result back on top.
For example after, 2 5 SUB 8, the stack is -3 8.
DIV is the integer quotient and MOD the remainder of the division.
There are also operators that act on the stack itself:
POP removes the top number.
DUP duplicates the top number.
SWP swaps the two top numbers. 4 5 SWP 6 swaps
ROT brings to the top the third number of the stack. If the stack is 1 2 3 4, ROT changes it in 1 3 4 2.
OVR copies the second top number of the stack on the top. If the stack is 1 2 3 4, OVR changes it in 1 2 3 4 3.
POS removes the top number, push 1 if this number is ≥ 0, otherwise push 0.
NOT removes the top number, push 1 if this number is 0, otherwise push 0.
OUT removes the top number and print it on the standard output, followed by a newline char.
To define a new instruction the syntax is :
DEF
The instructions between DEF and END are not executed immediately but stored to be executed when
Note that
For example
DEF SQ DUP MUL END defines the instruction SQ
when 4 SQ appears,
Inside a definition, conditionals are available :
IF RPN instructions FI
remove the top number, if this number is not zero, the instructions between IF and FI are executed, and continue with instructions after FI. If the number is zero, the execution continues after FI.
IF RPN instructions 1 ELS RPN instructions 2 FI
Remove the top number, if this number is not zero, the instructions between IF and ELS are executed, and continue with instructions after FI. If the number is zero, the instructions between ELS and FI are executed and continue execution after FI.
The two conditional structures can be nested.
Input
Line 1: N : number of input lines of code
N lines: Obsolete code
N lines: Obsolete code
Output
any number of lines: whatever the obsolet program outputs
Constraints
1 ≤ N ≤ 100
All the numbers are signed integer (minimum width : 30 bits).
All tests are correct (ie don't use undefined instruction) and use MOD and DIV instructions only with positive arguments.
All the numbers are signed integer (minimum width : 30 bits).
All tests are correct (ie don't use undefined instruction) and use MOD and DIV instructions only with positive arguments.
Example
Input
6 10 5 ADD OUT 10 5 SUB OUT 12 24 SUB OUT 30 10 MUL OUT 50 7 DIV OUT 50 7 MOD OUT
Output
15 5 -12 300 7 1
Tags
Difficulty
Hard
Test cases
Arithmetic test Test
Input
6
10 5 ADD OUT
10 5 SUB OUT
12 24 SUB OUT
30 10 MUL OUT
50 7 DIV OUT
50 7 MOD OUT
Output
15
5
-12
300
7
1
Arithmetic test Validator
Input
6
5 5 ADD OUT
15 5 SUB OUT
100 240 SUB OUT
6 7 MUL OUT
98 13 DIV OUT
98 13 MOD OUT
Output
10
10
-140
42
7
7
Stack Manipulations Test
Input
4
101 200 854 POP SWP DUP OUT OUT OUT
9 76 100 ROT OUT OUT OUT
5 4 OVR OUT OUT OUT
0 1 4 ROT ROT DUP ROT ADD ROT OUT OUT OUT
Output
101
101
200
9
100
76
5
4
5
4
1
1
Stack Manipulations Validator
Input
4
18 20 OVR OUT OUT OUT
9 8 3 POP SWP DUP OUT OUT OUT
1001 11 13 ROT OUT OUT OUT
1 1 3 ROT ROT DUP ROT ADD ROT OUT OUT OUT
Output
18
20
18
9
9
8
1001
13
11
3
2
1
"Logic" Test
Input
5
401 13 NOT NOT OUT OUT
402 178 NOT OUT OUT
403 89 POS OUT OUT
404 0 POS OUT OUT
405 -56 POS OUT OUT
Output
1
401
0
402
1
403
1
404
0
405
"Logic" Validator
Input
5
-12 POS OUT
0 POS OUT
8 POS OUT
96 NOT OUT
-8 NOT NOT OUT
Output
0
1
1
0
1
Simple function - square Test
Input
2
DEF SQ DUP MUL END
4 SQ OUT 10 SQ OUT 71 SQ OUT
Output
16
100
5041
Simple function - cubic Validator
Input
2
DEF TST DUP DUP MUL MUL END
2 TST OUT 17 TST OUT 9 TST OUT
Output
8
4913
729
Function and test Test
Input
4
DEF MAX OVR OVR SUB POS NOT IF SWP FI POP END
5 3 MAX 3 7 MAX MAX -2 MAX 4 MAX OUT
-8 -3 -7 2 -4 MAX MAX MAX MAX OUT
0 1 MAX 13 MAX DUP OUT 20 MAX 7 MAX OUT
Output
7
2
13
20
Function and test Validator
Input
4
DEF TST OVR OVR SUB POS IF SWP FI POP END
5 3 TST 3 7 TST TST -2 TST 4 TST OUT
-8 -3 -7 2 -4 TST TST TST TST OUT
0 1 TST 13 TST DUP OUT 20 TST -3 TST OUT
Output
-2
-8
0
-3
Function calling function and nested if Test
Input
17
DEF ABS DUP POS NOT IF 0 SWP SUB FI END
51 ABS OUT -5 ABS OUT 0 ABS OUT
DEF NZ
OVR ABS OVR ABS SUB
DUP NOT
IF POP DUP POS IF SWP FI
ELS
POS IF SWP FI
FI
POP
END
1 -2 NZ -8 NZ 4 NZ 5 NZ OUT
-12 -5 NZ -137 NZ OUT
42 -5 NZ 12 NZ 21 NZ 5 NZ 24 NZ OUT
42 5 NZ 12 NZ 21 NZ -5 NZ 24 NZ OUT
-5 -4 NZ -2 12 NZ NZ -40 4 NZ 2 18 NZ NZ NZ
11 5 NZ NZ OUT
Output
51
5
0
1
-5
5
5
2
Function calling function and nested if Validator
Input
10
DEF T0 DUP POS NOT IF 0 SWP SUB FI END
DEF TST OVR T0 OVR T0 SUB
DUP NOT
IF POP DUP POS IF SWP FI
ELS POS IF SWP FI FI
POP
END
7 -10 TST 13 1 TST TST -1 8 TST TST OUT
3 -5 TST 13 4 TST TST -1 8 TST TST OUT
4 -10 TST 13 -4 TST TST -5 8 TST TST OUT
Output
1
-1
4
the Queen of functions Test
Input
5
DEF F1
DUP 2 SUB POS
IF DUP 1 SUB F1 MUL ELS POP 1 FI
END
7 F1 OUT 10 F1 OUT 0 F1 OUT 3 F1 OUT
Output
5040
3628800
1
6
the Queen of functions Validator
Input
5
DEF TST
DUP POS
IF DUP 1 SUB TST ADD ELS POP 0 FI
END
7 TST OUT 10 TST OUT 1 TST OUT 3 TST OUT
Output
28
55
1
6
I have no loop and I must iterate Test
Input
8
DEF SQ DUP MUL END
DEF PL DUP OUT SQ OUT END
DEF PR OVR PL
SWP 1 ADD OVR OVR SUB POS
IF SWP PR ELS POP POP FI
END
1 5 PR
30 33 PR
Output
1
1
2
4
3
9
4
16
5
25
30
900
31
961
32
1024
33
1089
I have no loop and I must iterate Validator
Input
6
DEF TST OVR DUP OUT DUP DUP MUL MUL OUT
SWP 1 ADD OVR OVR SUB POS
IF SWP TST ELS POP POP FI
END
1 5 TST
30 33 TST
Output
1
1
2
8
3
27
4
64
5
125
30
27000
31
29791
32
32768
33
35937
Hello Fibonacci ! Test
Input
6
DEF RFIB DUP
IF 1 SUB ROT ROT DUP ROT ADD ROT RFIB
ELS POP POP FI
END
DEF FIB 0 1 ROT RFIB END
5 FIB OUT 6 FIB OUT 2 FIB OUT 10 FIB OUT
Output
5
8
1
55
Hello Lucas ! Validator
Input
6
DEF TST DUP IF 1 SUB ROT ROT DUP ROT
ADD ROT TST ELS POP POP FI END
2 1 0 TST OUT
2 1 9 TST OUT
2 1 5 TST OUT
2 1 7 TST OUT
Output
2
76
11
29
Integer square root Test
Input
20
DEF ESTIM DUP 4 SUB POS
IF 4 DIV SWP 2 MUL SWP ESTIM
ELS POP
FI
END
DEF NA SWP OVR DIV ADD 2 DIV END
DEF RS
OVR OVR NA
OVR OVR SUB DUP 1 SUB MUL
IF
SWP POP RS
ELS
ROT POP OVR OVR SUB POS IF SWP FI POP
FI
END
DEF ASQRT
DUP 1 SWP ESTIM RS
END
5040 ASQRT OUT 10 ASQRT OUT
1001 ASQRT OUT 65000 ASQRT OUT
Output
70
3
31
254
Integer cubic root Validator
Input
17
DEF T0 DUP 8 SUB POS
IF 8 DIV SWP 2 MUL SWP T0
ELS POP FI END
DEF T2 SWP OVR DIV OVR DIV SWP 2 MUL ADD
3 DIV END
DEF T1
OVR OVR T2
OVR OVR SUB DUP 1 SUB MUL
IF
SWP POP T1
ELS
ROT POP OVR OVR SUB POS IF SWP FI POP
FI
END
DEF TST DUP 1 SWP T0 T1 END
5040 TST OUT 10 TST OUT
1001 TST OUT 65000 TST OUT
Output
17
2
10
40
Solution language
Solution
Stub generator input