A2 Worksheet - Tracing Code (Incomplete)
A2 Worksheet - Tracing Code (Incomplete)
Tracing code
Task 1 .
The Python code in Figure 1 is an implementation of the Russian multiplication algorithm. This
method calculates the product of two numbers as a sum by using integer division and modulo
(MOD). Use the table below to help you investigate the algorithm in Python.
1 print("Numbers:")
2 a = int(input())
3 b = int(input())
4 sum = 0
5 while b > 0:
6 if b % 2 == 1:
7 sum = sum + a
8 a = 2*a
9 b = b // 2
10 print(sum)
Figure 1
Complete the trace table below using the algorithm in Figure 1. The values of a and b have been
provided and the first iteration of the while loop has been filled in for you.
In this task, you are going to analyse a piece of code to check if it is working correctly. The program
is meant to find the lowest number from a list of integers called items and store the lowest value from
this list in the variable lowest.
1 lowest = items[0]
2 for current in range(1, len(items)):
3 if lowest < items[current]:
4 lowest = items[current]
Figure 2
Complete the trace table below using the algorithm in Figure 2. The list of items and the first two
lines of code have been filled in for you.
items
Line lowest current Condition [0] [1] [2] [3] [4]
24 16 35 42 7
1 24
2 1
number of iterations then the inner loop. Note that the end number of the range is not included in the
generated sequence because it is used as the stop point.
1 total = 0
2 for i in range(1,3):
3 for j in range(2,5):
4 total = total + j
5 print(total)
Figure 3
Complete the trace table below using the algorithm in Figure 3.
Resources are updated regularly — the latest version is available at: ncce.io/tcc.
This resource is licensed under the Open Government Licence, version 3. For more information on this licence, see
ncce.io/ogl.