Contest Python: ACSL › Unit 4 — Prefix/Infix/Postfix Notation
Exercises: Unit 4 — Prefix/Infix/Postfix Notation
There are two kinds of exercises here, just as on an ACSL contest.
- Short-answer: work it out with pencil and paper and write one exact answer.
Write a prefix or postfix expression with one space between tokens (
+ 3 * 4 2), write powers as↑, and never change the order of the operands. Write a value as a whole number when it is whole (12, not12.0), and otherwise as a decimal, such as13.5. - Programming: write a complete program that reads the input with
input()and prints exactly the output described. Save it as a.pyfile and run it from a terminal, typing the sample input or feeding it a file. In program input and output the power sign is typed as^.
Each exercise is marked with the lowest ACSL division it belongs to. Do every exercise at or below your division. Exercises 1 to 9 are an Elementary practice test: no programming, only what Lesson 1 teaches. Exercises marked Challenge are extra practice for when you want more.
Exercise 1: Evaluate Postfix
Evaluate the postfix expression 3 4 + 2 *.
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: a number
Exercise 2: Evaluate Prefix
Evaluate the prefix expression - * 3 4 / 8 2.
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: a number
Exercise 3: Infix to Postfix
Write the infix expression (5 - 2) * 3 + 4 in postfix.
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: A postfix expression: one space between tokens (as in `12 3 +`), with the operands kept in their original order.
Exercise 4: Infix to Prefix
Write the infix expression 6 + 3 * (7 - 5) in prefix.
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: A prefix expression: one space between tokens (as in `+ 3 * 4 2`), with the operands kept in their original order.
Exercise 5: A Power in Postfix
Evaluate the postfix expression 5 2 ↑ 6 2 / -.
(On the Elementary test this is written 5 2 ^ 6 2 / -.)
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: a number
Exercise 6: Prefix to Postfix
Write the prefix expression * + 2 3 - 9 4 in postfix.
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: A postfix expression: one space between tokens (as in `12 3 +`), with the operands kept in their original order.
Exercise 7: Postfix to Prefix
Write the postfix expression 8 2 / 3 1 + * in prefix.
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: A prefix expression: one space between tokens (as in `+ 3 * 4 2`), with the operands kept in their original order.
Exercise 8: Prefix With a Power
Evaluate the prefix expression + ↑ 4 2 * 3 / 6 2.
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: a number
Exercise 9: Left to Right
Write the infix expression 9 - 4 - 2 + 1 in postfix.
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: A postfix expression: one space between tokens (as in `12 3 +`), with the operands kept in their original order.
Exercise 10: Bigger Numbers
Evaluate the postfix expression 12 3 4 + * 60 5 / -.
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: a number
Exercise 11: A Power of a Sum
Evaluate the prefix expression - ↑ + 1 2 3 * 4 - 10 6.
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: a number
Exercise 12: Letters to Prefix
Write the infix expression (A + B) * C ↑ 2 - D / (E - F) in prefix.
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: A prefix expression: one space between tokens (as in `+ 3 * 4 2`), with the operands kept in their original order. Powers are written `↑`; you may type `^` for `↑`.
Exercise 13: Letters to Postfix
Write the infix expression A * (B + C * D) - E ↑ (F - G) in postfix.
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: A postfix expression: one space between tokens (as in `12 3 +`), with the operands kept in their original order. Powers are written `↑`; you may type `^` for `↑`.
Exercise 14: Postfix to Prefix Directly
Write the postfix expression A B C * + D E - / in prefix.
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: A prefix expression: one space between tokens (as in `+ 3 * 4 2`), with the operands kept in their original order.
Exercise 15: Prefix to Postfix Directly
Write the prefix expression * - A / B C + D ↑ E 2 in postfix.
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: A postfix expression: one space between tokens (as in `12 3 +`), with the operands kept in their original order. Powers are written `↑`; you may type `^` for `↑`.
Exercise 16: The Stack Calculator
The input is one line holding a postfix expression.
Its tokens are separated by single spaces.
Each operand is a whole number, and each operator is one of +, -, *, / and ^ (the power ↑).
Print the value of the expression.
Constraints
- The expression has between 1 and 40 tokens, and it is a correct postfix expression.
- Every operand is a whole number from 0 to 100.
- Every division divides exactly, every power has an exponent from 0 to 5, and every value along the way is a whole number between -1000000 and 1000000.
Sample Input
6 8 4 2 - / +
Sample Output
10
This is 6 + 8 / (4 - 2).
Print the value as a whole number: 10, not 10.0.
How this is checked: Checked by running your program on 7 test inputs (1 sample, 6 hidden) and comparing its output line by line.
Exercise 17: The Prefix Calculator
The input is one line holding a prefix expression.
Its tokens are separated by single spaces.
Each operand is a whole number, and each operator is one of +, -, *, / and ^ (the power ↑).
Print the value of the expression.
Constraints
- The expression has between 1 and 40 tokens, and it is a correct prefix expression.
- Every operand is a whole number from 0 to 100.
- Every division divides exactly, every power has an exponent from 0 to 5, and every value along the way is a whole number between -1000000 and 1000000.
Sample Input
- ^ + 1 2 3 * 4 - 10 6
Sample Output
11
This is (1 + 2) ↑ 3 - 4 * (10 - 6), which is 27 − 16.
How this is checked: Checked by running your program on 7 test inputs (1 sample, 6 hidden) and comparing its output line by line.
Exercise 18: A Non-Whole Answer
Evaluate the prefix expression / + * 3 5 ↑ 3 2 * 4 - 7 3.
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: a number
Exercise 19: A Long Postfix
Evaluate the postfix expression 3 4 ↑ 5 * 9 2 / 6 * - 8 /.
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: a number
Exercise 20: A Long Conversion
Write the infix expression (A - B / C) ↑ 2 * (D + E * (F - G)) / H in postfix.
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: A postfix expression: one space between tokens (as in `12 3 +`), with the operands kept in their original order. Powers are written `↑`; you may type `^` for `↑`.
Exercise 21: The Decimal Calculator
The first line of input gives values to some letters: each letter is followed by its value, all separated by single spaces, such as A 7 B 2.
The second line holds a postfix expression, with its tokens separated by single spaces.
Each operand is one of those letters or a whole number, and each operator is one of +, -, *, / and ^ (the power ↑).
A division does not have to come out whole.
Print the value of the expression, with each letter replaced by its value: as a whole number if it is whole, and otherwise as a decimal, the way Python prints it (such as 15.5 or -0.125).
Constraints
- The first line gives values to between 1 and 10 different capital letters, and every value is a whole number from 0 to 100.
- The expression has between 1 and 40 tokens, and it is a correct postfix expression.
- Every number operand is a whole number from 0 to 100, and every letter in the expression has a value on the first line.
- Every value along the way is a multiple of 1/8 between -1000000 and 1000000, and never 0 when it is divided by.
- Every power has a whole-number exponent from 0 to 5.
Sample Input
A 7 B 2
A B / 3 4 * +
Sample Output
15.5
With A = 7 and B = 2, this is 7 / 2 + 3 × 4: 3.5 + 12 is 15.5.
How this is checked: Checked by running your program on 9 test inputs (1 sample, 8 hidden) and comparing its output line by line.
Exercise 22: Letters With Values
Evaluate the prefix expression - / * A B C ↑ - D A 2 when A = 3, B = 5, C = 2 and D = 7.
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: a number
Exercise 23: Prefix to Postfix
The input is one line holding a prefix expression.
Its tokens are separated by single spaces.
Each operand is a single capital letter or a whole number, and each operator is one of +, -, *, / and ^.
Print the same expression in postfix, with its tokens separated by single spaces.
Constraints
- The expression has between 1 and 60 tokens, and it is a correct prefix expression.
- Every number is a whole number from 0 to 1000.
Sample Input
* - A / B C + D ^ E 2
Sample Output
A B C / - D E 2 ^ + *
This is (A - B / C) * (D + E ↑ 2).
How this is checked: Checked by running your program on 5 test inputs (1 sample, 4 hidden) and comparing its output line by line.