Contest Python: ACSL › Unit 3 — What Does This Program Do? (Branching)

Exercises: Unit 3 — What Does This Program Do? (Branching)

Most exercises here are short-answer, just like the What Does This Program Do? questions on an ACSL contest.

  • Short-answer (marked short answer): a program and its input are given, either in Python or in ACSL pseudocode. Trace it by hand and write down exactly what it outputs. For pseudocode, use ACSL's rules from the dialect table in Lesson 1: int rounds down, FOR includes its end value, and substrings follow ACSL's rules. Every program outputs exactly one line.
  • Predict, then verify (programming): a program is given in ACSL pseudocode. First trace it on the sample input and write down your prediction. Then translate it into a complete Python program that reads the input with input(), run it on the sample, and compare. Save it as a .py file and run it from a terminal: python my_solution.py < assets/ex6/1.in.

Exercises 1–7 are the Junior Branching questions. Exercises 8 onwards trace loops, arrays, grids and strings, which Intermediate and Senior students meet in Contest 1. Each exercise is marked with the lowest ACSL division it belongs to; do every exercise at or below your division. Exercises marked Challenge are extra practice for when you want more.

Exercise 1: First True Branch

  • Division: Junior
score = int(input())
bonus = 0
if score >= 90:
    grade = "A"
elif score >= 80:
    grade = "B"
    bonus = 5
elif score >= 70:
    grade = "C"
    bonus = 10
else:
    grade = "D"
if score + bonus >= 90:
    grade = grade + "+"
print(grade, score + bonus)

Input

85

What exactly does the program print? Work it out by hand first; write the output exactly as it appears.

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

Answer format: Exactly the one line the program outputs: values separated by one space, strings without quotation marks, and letters in the case the program prints them.

Also check yourself

The automatic check cannot see these, so tick each one when your work does it.

For parents and teachers: report a problem

Exercise 2: Nested Decisions

  • Division: Junior
INPUT x, y
IF x > y THEN
   IF x % 2 == 0 THEN
      z = x * 2 - y
   ELSE
      z = x + y * 2
   END IF
ELSE
   IF y % 3 == 0 || x < 0 THEN
      z = y - x
   ELSE
      z = x * y
   END IF
END IF
OUTPUT z

Input. x = -4, y = -9

What exactly is output? Trace it by hand, using ACSL's rules; write the output exactly as it appears.

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

Answer format: a number

Also check yourself

The automatic check cannot see these, so tick each one when your work does it.

For parents and teachers: report a problem

Exercise 3: Shuffled Digits

  • Division: Junior
n = int(input())
a = n // 100
b = n // 10 % 10
c = n % 10
if a + c == b:
    result = n + 1
elif a > c:
    if b % 2 == 0:
        result = a * 100 + c * 10 + b
    else:
        result = c * 100 + b * 10 + a
else:
    result = n - a - b - c
print(result)

Input

734

What exactly does the program print? Work it out by hand first; write the output exactly as it appears.

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

Answer format: a number

Also check yourself

The automatic check cannot see these, so tick each one when your work does it.

For parents and teachers: report a problem

Exercise 4: Quotient and Leftover

  • Division: Junior
INPUT a, b
q = int(a / b)
r = a - q * b
IF r == 0 THEN
   OUTPUT q
ELSE IF q > r THEN
   OUTPUT q * 10 + r
ELSE
   OUTPUT r ^ 2 - q
END IF

Input. a = 47, b = 9

What exactly is output? Trace it by hand, using ACSL's rules; write the output exactly as it appears.

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

Answer format: a number

Also check yourself

The automatic check cannot see these, so tick each one when your work does it.

For parents and teachers: report a problem

Exercise 5: Counting the True Tests

  • Division: Junior
a = 6
b = -3
c = 0
count = 0
if a > 0 and b > 0:
    count = count + 1
if a > 0 or b > 0:
    count = count + 10
if not (c == 0) or a + b == 3:
    count = count + 100
if a > 5 or b > 5 and c > 5:
    count = count + 1000
if (a > 5 or b > 5) and c > 5:
    count = count + 10000
print(count)

Input. None: the program reads no input.

What exactly does the program print? Work it out by hand first; write the output exactly as it appears.

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

Answer format: a number

Also check yourself

The automatic check cannot see these, so tick each one when your work does it.

For parents and teachers: report a problem

Exercise 6: Parking Fee (predict, then verify)

  • Division: Junior

A car park works out its fee with this ACSL program. hours is how long the car stayed, and day is the day of the week, from 1 (Monday) to 7 (Sunday).

INPUT hours, day
IF day == 6 || day == 7 THEN
   fee = 5
ELSE IF hours <= 2 THEN
   fee = 0
ELSE IF hours <= 5 THEN
   fee = (hours - 2) * 3
ELSE
   fee = 9 + (hours - 5) * 4
END IF
IF fee > 20 THEN
   fee = 20
END IF
OUTPUT fee
  1. Predict: trace the program by hand on the sample input and write down what it outputs, before you look at the sample output.
  2. Verify: translate the program into Python and run it on the sample input.

The input is one line holding hours and day, separated by one space. Print the fee.

Constraints

  • 0 <= hours <= 24
  • 1 <= day <= 7

Sample Input

7 3

Sample Output

17

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

Also check yourself

The automatic check cannot see these, so tick each one when your work does it.

For parents and teachers: report a problem

Exercise 7: One IF After Another

  • Division: Junior
x = 14
y = 0
IF x > 10 THEN
   x = x - 6
   y = y + 1
END IF
IF x < 10 THEN
   x = x * 3
   y = y + 1
END IF
IF x % 4 == 0 THEN
   x = int(x / 4)
   y = y + 1
ELSE
   x = x + 1
END IF
OUTPUT x, y

Input. None: the program reads no input.

What exactly is output? Trace it by hand, using ACSL's rules; write the output exactly as it appears.

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

Answer format: Exactly the one line the program outputs: values separated by one space, strings without quotation marks, and letters in the case the program prints them.

Also check yourself

The automatic check cannot see these, so tick each one when your work does it.

For parents and teachers: report a problem

Exercise 8: Stepping Down

  • Division: Intermediate
total = 0
FOR k = 20 TO 1 STEP -3
   IF k % 2 == 0 THEN
      total = total + k
   ELSE
      total = total - 1
   END IF
NEXT k
OUTPUT total

Input. None: the program reads no input.

What exactly is output? Trace it by hand, using ACSL's rules; write the output exactly as it appears.

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

Answer format: a number

Also check yourself

The automatic check cannot see these, so tick each one when your work does it.

For parents and teachers: report a problem

Exercise 9: Digit Sums Until One Digit

  • Division: Intermediate
n = int(input())
steps = 0
while n >= 10:
    total = 0
    while n > 0:
        total = total + n % 10
        n = n // 10
    n = total
    steps = steps + 1
print(n, steps)

Input

98765

What exactly does the program print? Work it out by hand first; write the output exactly as it appears.

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

Answer format: Exactly the one line the program outputs: values separated by one space, strings without quotation marks, and letters in the case the program prints them.

Also check yourself

The automatic check cannot see these, so tick each one when your work does it.

For parents and teachers: report a problem

Exercise 10: Round Down, Even Below Zero

  • Division: Intermediate
a = -17
b = 4
c = a / b
d = int(c)
e = int(abs(a) / b)
OUTPUT c, d, e

Remember: ACSL's int(x) is the greatest integer less than or equal to x.

Input. None: the program reads no input.

What exactly is output? Trace it by hand, using ACSL's rules; write the output exactly as it appears.

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

Answer format: Exactly the one line the program outputs: values separated by one space, strings without quotation marks, and letters in the case the program prints them.

Also check yourself

The automatic check cannot see these, so tick each one when your work does it.

For parents and teachers: report a problem

Exercise 11: One Pass of Swaps

  • Division: Intermediate

The array A starts at position 1 and holds A(1) = 4, A(2) = 9, A(3) = 2, A(4) = 7, A(5) = 5, A(6) = 8.

n = 6
FOR j = 1 TO n - 1
   IF A(j) > A(j + 1) THEN
      t = A(j)
      A(j) = A(j + 1)
      A(j + 1) = t
   END IF
NEXT j
OUTPUT A(2), A(4), A(6)

Input. None: the array is given above.

What exactly is output? Trace it by hand, using ACSL's rules; write the output exactly as it appears.

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

Answer format: Exactly the one line the program outputs: values separated by one space, strings without quotation marks, and letters in the case the program prints them.

Also check yourself

The automatic check cannot see these, so tick each one when your work does it.

For parents and teachers: report a problem

Exercise 12: Checkerboard Total

  • Division: Intermediate
grid = [[3, 8, 1, 6],
        [7, 2, 9, 4],
        [5, 0, 6, 2]]
total = 0
for r in range(len(grid)):
    for c in range(len(grid[0])):
        if (r + c) % 2 == 0:
            total = total + grid[r][c]
        elif grid[r][c] > 5:
            total = total - grid[r][c]
print(total, grid[2][1] + grid[1][2])

Input. None: the program reads no input.

What exactly does the program print? Work it out by hand first; write the output exactly as it appears.

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

Answer format: Exactly the one line the program outputs: values separated by one space, strings without quotation marks, and letters in the case the program prints them.

Also check yourself

The automatic check cannot see these, so tick each one when your work does it.

For parents and teachers: report a problem

Exercise 13: Filling a Grid

  • Division: Intermediate

The 2D array A has rows 1 to 4 and columns 1 to 4.

FOR r = 1 TO 4
   FOR c = 1 TO 4
      IF r == c THEN
         A(r, c) = r * 2
      ELSE IF r < c THEN
         A(r, c) = c - r
      ELSE
         A(r, c) = A(r - 1, c) + c
      END IF
   NEXT c
NEXT r
s = 0
FOR k = 1 TO 4
   s = s + A(k, 5 - k)
NEXT k
OUTPUT s

Input. None: the program reads no input.

What exactly is output? Trace it by hand, using ACSL's rules; write the output exactly as it appears.

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

Answer format: a number

Also check yourself

The automatic check cannot see these, so tick each one when your work does it.

For parents and teachers: report a problem

Exercise 14: Slices and Swaps

  • Division: Intermediate
word = input()
first = word[:3]
last = word[len(word) - 3:]
middle = word[2:6]
code = last.upper() + first.lower()
if "a" in middle:
    code = code.replace("a", "@")
print(code, middle, len(code))

Input

Panorama

What exactly does the program print? Work it out by hand first; write the output exactly as it appears.

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

Answer format: Exactly the one line the program outputs: values separated by one space, strings without quotation marks, and letters in the case the program prints them.

Also check yourself

The automatic check cannot see these, so tick each one when your work does it.

For parents and teachers: report a problem

Exercise 15: Backwards and Pieces

  • Division: Intermediate

Remember ACSL's substring rules: S[:n] is the first n characters, S[n:] is the last n characters, and S[a:b] is positions a through b, both included. Positions start at 0.

S = "ACSL CONTEST"
R = ""
FOR j = len(S) - 1 TO 0 STEP -1
   R = R + S[j]
NEXT j
T = R[:4] + S[5:7] + R[3:]
OUTPUT T

Input. None: the program reads no input.

What exactly is output? Trace it by hand, using ACSL's rules; write the output exactly as it appears.

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

Answer format: Exactly the one line the program outputs: values separated by one space, strings without quotation marks, and letters in the case the program prints them.

Also check yourself

The automatic check cannot see these, so tick each one when your work does it.

For parents and teachers: report a problem

Exercise 16: Square Steps (predict, then verify)

  • Division: Intermediate

This ACSL program keeps taking away the largest square number that fits, and counts how many squares it takes away.

INPUT n
count = 0
WHILE n > 0
   r = int(sqrt(n))
   n = n - r ^ 2
   count = count + 1
END WHILE
OUTPUT count
  1. Predict: trace the program by hand on the sample input and write down what it outputs, before you look at the sample output.
  2. Verify: translate the program into Python, using math.floor and math.sqrt, and run it on the sample input.

The input is one line holding the whole number n. Print the count.

Constraints

  • 1 <= n <= 10000

Sample Input

23

Sample Output

5

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

Also check yourself

The automatic check cannot see these, so tick each one when your work does it.

For parents and teachers: report a problem

Exercise 17: Perfect Squares Only

  • Challenge
  • Division: Intermediate
import math

total = 0
for n in range(1, 30, 4):
    root = math.sqrt(n)
    if root == math.floor(root):
        total = total + n
    else:
        total = total - math.floor(root)
print(total, math.sqrt(total + 32))

Input. None: the program reads no input.

What exactly does the program print? Work it out by hand first; write the output exactly as it appears.

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

Answer format: Exactly the one line the program outputs: values separated by one space, strings without quotation marks, and letters in the case the program prints them.

Also check yourself

The automatic check cannot see these, so tick each one when your work does it.

For parents and teachers: report a problem

Exercise 18: A Walk on the Grid

  • Challenge
  • Division: Senior

The 2D array A has rows 1 to 3 and columns 1 to 3. Positions in the string S start at 0.

S = "PYTHON"
FOR r = 1 TO 3
   FOR c = 1 TO 3
      A(r, c) = (r * 2 + c * 3) % 6
   NEXT c
NEXT r
r = 1
c = 1
out = ""
steps = 0
WHILE steps < 6
   v = A(r, c)
   out = out + S[v]
   IF v % 2 == 0 THEN
      r = r % 3 + 1
   ELSE
      A(r, c) = v - 1
      c = c % 3 + 1
   END IF
   steps = steps + 1
END WHILE
OUTPUT out

Input. None: the program reads no input.

What exactly is output? Trace it by hand, using ACSL's rules; write the output exactly as it appears.

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

Answer format: Exactly the one line the program outputs: values separated by one space, strings without quotation marks, and letters in the case the program prints them.

Also check yourself

The automatic check cannot see these, so tick each one when your work does it.

For parents and teachers: report a problem

Back to Unit 3 — What Does This Program Do? (Branching)