Contest Python: ACSL › Checkpoint 2 — Contest 2 Practice

Questions: Checkpoint 2 — Contest 2 Practice

This is a practice paper modelled on ACSL Contest 2: short-answer questions from the four Contest 2 categories, then one programming problem. The Junior paper and the Intermediate/Senior paper share their first four questions, so this checkpoint holds both six-question papers.

Questions Category Division
1–2 Prefix/Infix/Postfix Notation Junior and above
3–4 Bit-String Flicking Junior and above
5–6 What Does This Program Do? – Looping Junior
7–8 LISP Intermediate and above
9 Programming problem Junior and above

Choose your path.

  • Junior: Questions 1–6, then Question 9.
  • Intermediate or Senior: Questions 1–4 and 7–8, then Question 9. Questions 5–6 are optional extra loop practice.
  • Classroom (no programming): Questions 1–4 and 7–8. Questions 5–6 are optional, and Question 9 is left out.

Take it as a timed practice.

  • Put away your notes and set a timer.
  • Give yourself 30 minutes for the six-question paper on your path, as in the real contest. Work with pencil and paper only: no computer, no calculator. Write each answer on paper or in your notebook.
  • On the Junior, Intermediate or Senior path, then give yourself about 60 minutes for Question 9 (the Classroom path skips it). Write a complete program that reads the input with input() and prints exactly the output described. Save it as a .py file and run it from a terminal, typing the sample input or feeding it a file: python my_solution.py < sample.txt. Print nothing extra: no prompts in input(), and no words the problem did not ask for.

These times are a practice pace, not the official rules; the real limits for your division are published by ACSL at acsl.org.

How to write short answers.

  • Give exactly one answer, written the way the question asks for it.
  • For a prefix or postfix expression, separate the tokens with single spaces and keep the operands in their original order.
  • For a numeric value, write a whole number when the value is whole, and otherwise a decimal such as 13.5.
  • For a bit string, write every bit, at the full length given in the question.
  • For a list of bit strings, write them from smallest to largest, separated by a comma and a space.
  • For a LISP list, write it in parentheses with single spaces, such as (A B C), using the capital letters the question uses.
  • For a program, write exactly the one line it outputs.

When you finish, check yourself.

  • Short answers have no sample to test against. Mark the questions you were unsure of, then work each one again by its unit's method: redo the stack or the trace, or turn a conversion back and check that you get the question. Compare with a partner, or with a second attempt made without looking at the first.
  • The programming question is checked against its sample, and against inputs of your own made from its constraints: the smallest cases, the largest, and the tricky ones.

Then go back to the unit for any category that cost you points.

Question 1: Moving the Operators Back

  • Division: Junior

Convert the infix expression

A - (B + C) * D ↑ E / F

to postfix. ↑ means "to the power of".

Write the answer as tokens separated by single spaces.

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 `↑`.

For parents and teachers: report a problem

Question 2: Evaluate the Prefix

  • Division: Junior

Evaluate the prefix expression

/ - * 12 ↑ 2 3 + 15 * 3 7 + 4 * 2 3

↑ means "to the power of". All operands are whole numbers written in base 10.

Write the value as a number.

How this is checked: Checked by comparing your answer with the expected answer.

Answer format: a number

For parents and teachers: report a problem

Question 3: Flick the Bits

  • Division: Junior

Evaluate the expression

NOT 10110 OR RSHIFT-1 01101 AND LCIRC-2 11010

Write the answer as a 5-bit string.

How this is checked: Checked by comparing your answer with the expected answer.

Answer format: A bit string at its full length, with the 0s on the left kept (`00110`, not `110`).

For parents and teachers: report a problem

Question 4: Solve for x

  • Division: Junior

Find all 5-bit strings x that make this equation true:

x OR 10100 = 10111

Write every solution as a 5-bit string, from smallest to largest, separated by a comma and a space.

How this is checked: Checked by comparing your answer with the expected answer.

Answer format: Every solution in increasing order, each a bit string at its full length (0s on the left kept: `00110`, not `110`), separated by a comma and a space.

For parents and teachers: report a problem

Question 5: Paired Up

  • Division: Junior
n = int(input())
total = 0
for i in range(1, n + 1):
    for j in range(i, n + 1, 2):
        if (i + j) % 3 == 0:
            total = total + i * j
print(total)

Input

6

What exactly does the program print?

How this is checked: Checked by comparing your answer with the expected answer.

Answer format: a number

For parents and teachers: report a problem

Question 6: Counting Down by Fives

  • Division: Junior

This program is written in ACSL pseudocode. ACSL papers also write keywords in lowercase, as here. % is the remainder, / is real division, and int(x) is the greatest integer less than or equal to x. A for loop includes its end value when the counter reaches it exactly.

input n
c = 0
m = 0
for k = n to 1 step -5
   x = k
   s = 0
   while x > 0
      s = s + x % 10
      x = int(x / 10)
   end while
   if s > m then
      m = s
   end if
   c = c + s
next k
output c - m

What value does the program output when the input value is 47?

How this is checked: Checked by comparing your answer with the expected answer.

Answer format: a number

For parents and teachers: report a problem

Question 7: Define and Call

  • Division: Intermediate

Evaluate the last expression, after the first three have been evaluated in order.

(SETQ A 7)
(SETQ B (SUB A 12))
(DEFUN F (X Y) (DIV (ADD (SQUARE X) Y (MULT 2 X)) 4))
(F A B)

Write the value of (F A B) as a number.

How this is checked: Checked by comparing your answer with the expected answer.

Answer format: a number

For parents and teachers: report a problem

Question 8: Taking Lists Apart

  • Division: Intermediate

Evaluate the last expression, after the first one has been evaluated.

(SETQ L '((RED (BLUE GREEN)) (4 5 6) YELLOW))
(REVERSE (CONS (CADDR L) (CADR (CAR L))))

Write the value as a LISP list.

How this is checked: Checked by comparing your answer with the expected answer.

Answer format: A LISP list: round brackets, single spaces between items, the same capital letters as the question, and no quote mark, as in `(A (B C) D)`.

For parents and teachers: report a problem

Question 9: Brackets Away

  • Division: Junior

A fully bracketed infix expression has one pair of brackets round every operator and its two operands, like the first step of the brackets-first method in Unit 4: ((A+B)*(C-(D/E))). The brackets say exactly which operator is done when, so no order of operations is needed.

The input is one line: a fully bracketed infix expression, with no spaces. Each operand is one capital letter or one digit, and each operator is +, -, *, / or ^ (the power ↑). Print two lines: the expression in postfix, and then the expression in prefix, each with its tokens separated by single spaces. The operands are never worked out: print each one exactly as it is written.

Constraints

  • The expression has between 1 and 400 characters.
  • Every operator and its two operands are inside exactly one pair of brackets, and there are no other brackets. So an expression that is a single operand has no brackets at all, and any longer expression starts with ( and ends with ).

Sample Input

((A+B)*(C-(D/E)))

Sample Output

A B + C D E / - *
* + A B - C / D E

The brackets make D/E the first operator done and * the last, so * comes last in the postfix and first in the prefix. The operands keep their order, A B C D E, in both.

How this is checked: Checked by running your program on 10 test inputs (1 sample, 9 hidden) and comparing its output line by line.

For parents and teachers: report a problem

Back to Checkpoint 2 — Contest 2 Practice