Contest Python: ACSL › Unit 6 — What Does This Program Do? (Looping)

Exercises: Unit 6 — What Does This Program Do? (Looping)

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

  • Short-answer (marked short answer): a program is 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: FOR includes its end value, WHILE tests at the top of each pass, and int rounds down, even below zero. Every program outputs exactly one line; when it outputs several values, they are separated by one space.
  • 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/ex7/1.in.

Each exercise is marked with the lowest ACSL division it belongs to. This unit is the Junior Contest 2 category; Intermediate and Senior students can use it as loop-tracing drill. Exercises marked Challenge are extra practice for when you want more.

Exercise 1: Up in Threes

  • Division: Junior
total = 0
FOR k = 2 TO 20 STEP 3
   total = total + k
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 2: Range Stops Early

  • Division: Junior
total = 0
count = 0
for k in range(3, 30, 4):
    if k % 3 == 0:
        total = total + k
    else:
        count = count + 1
print(total, 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: 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 3: Counting Down

  • Division: Junior
n = 0
p = 1
FOR k = 15 TO 1 STEP -4
   n = n + 1
   p = p * 2 + k
NEXT k
OUTPUT n, p

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 4: Zero Passes or More?

  • Division: Junior
x = 25
FOR k = 10 TO 4
   x = x + k
NEXT k
y = 0
WHILE x > 20
   x = x - 3
   y = y + 1
END WHILE
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: 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: Digits Backwards

  • Division: Junior
n = int(input())
rev = 0
odd = 0
while n > 0:
    d = n % 10
    rev = rev * 10 + d
    if d % 2 == 1:
        odd = odd + 1
    n = n // 10
print(rev, odd)

Input

40721

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 6: The Best Remainder

  • Division: Junior
INPUT n
best = 0
where = 0
FOR k = 1 TO 8
   v = (n * k) % 11
   IF v > best THEN
      best = v
      where = k
   END IF
NEXT k
OUTPUT best, where

Input. n = 7

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 7: Up and Down to One (predict, then verify)

  • Division: Junior

This ACSL program starts from a whole number n. While n is not 1, it halves n when n is even, and otherwise replaces n with 3 * n + 1. It counts the steps and remembers the highest value n reaches, counting the starting value.

INPUT n
steps = 0
peak = n
WHILE n != 1
   IF n % 2 == 0 THEN
      n = int(n / 2)
   ELSE
      n = 3 * n + 1
   END IF
   steps = steps + 1
   IF n > peak THEN
      peak = n
   END IF
END WHILE
OUTPUT steps, peak
  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 the whole number n. Print the number of steps and the peak, separated by one space.

Constraints

  • 1 <= n <= 1000

Sample Input

6

Sample Output

8 16

How this is checked: Checked by running your program on 5 test inputs (1 sample, 4 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 8: Distance From the Line

  • Division: Junior
a = 3
s = 0
FOR k = 1 TO 6
   s = s + abs(a - k * 2)
   a = a + 1
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 9: Floor Division Below Zero

  • Division: Junior
x = 7
total = 0
for k in range(4):
    x = x - 5
    total = total + x // 2
print(x, total)

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 10: Halving Past Zero

  • Division: Junior

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

x = 45
c = 0
WHILE x > -20
   x = int(x / 2) - 12
   c = c + 1
END WHILE
OUTPUT x, c

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: Biggest Leftover (predict, then verify)

  • Division: Junior

This ACSL program divides m by every whole number from n down to 1. It finds the largest remainder, and the divisor that first gives it.

INPUT n, m
best = -1
at = 0
FOR k = n TO 1 STEP -1
   r = m % k
   IF r > best THEN
      best = r
      at = k
   END IF
NEXT k
OUTPUT best, at
  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 n and m, separated by one space. Print the largest remainder and its divisor, separated by one space.

Constraints

  • 1 <= n <= 100
  • 1 <= m <= 1000

Sample Input

7 20

Sample Output

6 7

How this is checked: Checked by running your program on 5 test inputs (1 sample, 4 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 12: Pairs That Add to Threes

  • Division: Junior
c = 0
t = 0
FOR i = 1 TO 5
   FOR j = i TO 5
      c = c + 1
      IF (i + j) % 3 == 0 THEN
         t = t + i * j
      END IF
   NEXT j
NEXT i
OUTPUT c, 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 13: Stepping Down Inside

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

Input

6

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 14: Powers of Three

  • Division: Junior
INPUT n
p = 1
k = 0
s = 0
WHILE p * 3 <= n
   p = p * 3
   k = k + 1
   s = s + n % p
END WHILE
OUTPUT k, s

Input. n = 100

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 15: Biggest Jump

  • Division: Junior
prev = 5
biggest = 0
where = 0
for k in range(1, 8):
    cur = (prev * 7 + k) % 23
    gap = abs(cur - prev)
    if gap >= biggest:
        biggest = gap
        where = k
    prev = cur
print(biggest, where)

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 16: Doubling and Shrinking

  • Division: Junior
a = 1
b = 100
n = 0
WHILE a < b
   a = a * 2
   b = b - a
   n = n + 1
END WHILE
OUTPUT a, b, n

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 17: Pairs With a Multiple (predict, then verify)

  • Division: Junior

This ACSL program counts the pairs of different whole numbers i and j, with 1 <= i < j <= n, whose product i * j is a multiple of m.

INPUT n, m
count = 0
FOR i = 1 TO n - 1
   FOR j = i + 1 TO n
      IF (i * j) % m == 0 THEN
         count = count + 1
      END IF
   NEXT j
NEXT i
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 and run it on the sample input.

The input is one line holding n and m, separated by one space. Print the count.

Constraints

  • 2 <= n <= 200
  • 1 <= m <= 1000

Sample Input

6 4

Sample Output

6

How this is checked: Checked by running your program on 6 test inputs (1 sample, 5 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 18: Divisors Counted Down

  • Challenge
  • Division: Junior
INPUT n
total = 0
FOR i = n TO 1 STEP -2
   FOR j = 1 TO int(i / 2)
      IF i % j == 0 THEN
         total = total + j
      ELSE
         total = total - 1
      END IF
   NEXT j
NEXT i
OUTPUT total

Input. n = 11

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 19: Odd Digit Sums

  • Challenge
  • Division: Junior
count = 0
best = 0
FOR k = 100 TO 60 STEP -7
   m = k
   s = 0
   WHILE m > 0
      s = s + m % 10
      m = int(m / 10)
   END WHILE
   IF s % 2 == 1 THEN
      count = count + 1
   END IF
   IF s > best THEN
      best = s
   END IF
NEXT k
OUTPUT count, best

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 6 — What Does This Program Do? (Looping)