Iteration (practice)

Bisc syntax and examples.

Overview

This page includes solved code-reading exercises on loops.

Warmu Up Exercise

How many times will each of A, B, and C be printed on the screen if we run the following code?

for i in range(1000):
    print('A')
    print('C')
    for j in range(1000):
        print('B')
        print('C')
Solution
  • A : 1,000
  • B : 1,000,000
  • C : 1,001,000

Exercise 1

Choose a meaningful function name for the following code. Choose the name based on your understanding of what the code does overall.

def foo(n):
    for i in range(1, n):
        if i * i == n:
            return i
    return -1
Suggested Solution integer_square_root.

To understand what the code does, ask yourself: What does the function return in each of the following cases?
  • If n=1
  • If n=4
  • If n=16
  • If n=1000000
  • If n=3
  • If n=10
  • If n=-5

Note. The name square_root is not the best name, because the function only returns integer square roots.

Exercise 2

Choose a meaningful function name for the following code. Choose the name based on your understanding of what the code does overall.

def mystery(n):
    x = 0
    c = abs(n)  # absolute value

    while c > 0:
        x = x + 2
        c = c - 1

    if n >= 0:
        return x
    else:
        return -x
Suggested Solution double or multiply_by_2.

The above function returns the value of `n * 2, but it does the work in a complicated way. It repeats n times adding the number 2. It also handles negative numbers.
To understand the code better, try tracing it on a piece of paper for the following values of n:
  • n=1
  • n=2
  • n=4
  • n=-1
  • n=-4

Exercise 3

Choose a meaningful function name for the following code. Choose the name based on your understanding of what the code does overall.

def mystery():
    result = None
    while True:
        num = int(input("Enter a number (0 to stop): "))
        if num == 0:
            break
        if num % 2 == 0:
            result = num
    return result
Suggested Solution last_even

The function updates result each time it encounters an even number. When the loop terminates, the function returns whatever is inside result. If no even number was encountered, result would have None. If one or more even numbers were encountered, it would have the last even number stored.

To understand the code better, try tracing it on a piece of paper for the following values:
  • 0
  • 1, 3, 5, 0
  • 1, 2, 0
  • 2, 3, 4, 5, 0
  • 8, 7, 6, 5, 4, 1, 0

Exercise 4

Choose a meaningful function name for the following code. Choose the name based on your understanding of what the code does overall.

def mystery(n):
    temp1 = -1
    temp2 = -1

    for i in range(n):
        value = int(input("Enter a positive number: "))
        if i == 0:
            temp1 = value
        elif value > temp1:
            temp2 = temp1
            temp1 = value
        elif value > temp2:
            temp2 = value
    
    return temp2
Suggested Solution second_largest

To understand the code better, try tracing it on a piece of paper for the following values:
  • n = 0 and no values are given.
  • n = 1 and value is 10
  • n = 2 and the values are: 10, 5
  • n = 6 and the values are: 10, 5, 4, 3, 2, 1
  • n = 2 and the values are: 5, 10
  • n = 6 and the values are: 1, 2, 3, 4, 5, 6

Remember that you can use PythonTutor to trace the code step-by-step and see how the variable values change in each iteration.


© Ibrahim Albluwi. · Last updated: