Code optimisation

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Code optimisation

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which best describes code optimisation?
A Adding new features so the program does more
B Improving speed, memory use or readability without changing what the program does
2. A program has the same 4 lines copied three times. What is the best optimisation?
A Add a comment above each copy of the lines
B Move the repeated lines into a function and call it three times
3. Which is the better way to add up the numbers in a list called scores?
A total = sum(scores)
B Write out scores[0] + scores[1] + scores[2] + ... by hand
4. A sorted list of 1,000,000 items needs to be searched. Which is the better algorithm?
A Binary search
B Linear search
5. Which statement about optimisation is true?
A Optimised code must still give the same output as the original
B Optimised code is allowed to give different output if it runs faster

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1total = sum(marks) Amid = (low + high) // 2
2def show(x): print(x * 2) Blen(names) is recalculated on every pass of the loop instead of being stored once in a variable
3Choosing a better algorithm: binary search on a sorted list is faster than checking every item CUses a built-in function to add up a list, replacing a longer hand-written loop
4Readability vs speed: a clear variable name makes code easier to maintain even if it is not faster Daverage_mark = total / count
5for i in range(len(names)): print(len(names)) EMoves repeated code into one function that can be called many times
6print(a * 2) print(b * 2) print(c * 2) FThe same calculation is copied three times instead of using a loop or function to remove the repeated code

Definitions

optimisation

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
total = ___(marks)
2.
for i in _____(5): print("Hello")
3.
mid = (low + high) __ 2
4.
ordered = ______(scores)
5.
limit = len(items) while i < _____:

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A marks = [72, 88, 65, 91]
B total = sum(marks)
C average = total / len(total)
D print(average)
Question 2
A names = ['Ava', 'Liam']
B for name in len(names):
C print('Hi', name)
Question 3
A def total(items)
B return sum(items)
C print(total([4, 8, 15]))
Question 4
A nums = [3, 9, 4, 7]
B low = min(nums)
C high = max(nums)
D print(high - lows)
Question 5
A define find(items, x):
B if x in items:
C return items.index(x)
D return -1

Answer Key

Code optimisation
Section A: Quick Questions
Q1
B — Improving speed, memory use or readability without changing what the program does
Q2
B — Move the repeated lines into a function and call it three times
Q3
A — total = sum(scores)
Q4
A — Binary search
Q5
A — Optimised code must still give the same output as the original
Section B: Match the Pairs
Q1
total = sum(marks) → Uses a built-in function to add up a list, replacing a longer hand-written loop
Q2
def show(x): print(x * 2) → Moves repeated code into one function that can be called many times
Q3
Choosing a better algorithm: binary search on a sorted list is faster than checking every item → mid = (low + high) // 2
Q4
Readability vs speed: a clear variable name makes code easier to maintain even if it is not faster → average_mark = total / count
Q5
for i in range(len(names)): print(len(names)) → len(names) is recalculated on every pass of the loop instead of being stored once in a variable
Q6
print(a * 2) print(b * 2) print(c * 2) → The same calculation is copied three times instead of using a loop or function to remove the repeated code
Section C: Definitions
Q1
optimisation — Changing code so it runs faster, uses less memory or is easier to read, without changing what it does. For example, removing repeated code or using a built-in function.
Section D: Fill in the Blanks
Q1
total = sum(marks)
Q2
for i in range(5): print("Hello")
Q3
mid = (low + high) // 2
Q4
ordered = sorted(scores)
Q5
limit = len(items) while i < limit:
Section E: Spot the Error
Q1
C — len() needs a list, but total is a single number, so this causes a TypeError. Using the built-in sum() is a good optimisation, but the average must divide by len(marks).
Q2
B — len(names) is a number and cannot be looped over, so this causes a TypeError. A loop replaces repeated print lines, but it must loop over the list itself: for name in names:
Q3
A — A function header must end with a colon. Putting the repeated work in one function removes duplicated code, but the header must be def total(items):
Q4
D — The variable lows was never created, so this causes a NameError. The built-in min() and max() store the values in low and high, so the line should be print(high - low).
Q5
A — Python uses the keyword def, not define, to create a function. Using built-in in and .index() is shorter than writing your own loop, but the header must be def find(items, x):