Optimising code with AI tools

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

Optimising code with AI tools

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. You ask an AI tool to make your function faster. What should you do before accepting its new version?
A Replace your code straight away because the AI says it is faster
B Re-run the same test cases and compare the results with the original
2. Which prompt is better for asking an AI tool to optimise a module?
A Make this function more efficient without changing its output. Explain each change.
B Fix my code.
3. After optimisation, test case 4 gives a different result from before. What should you do?
A Accept it because the new code looks shorter and cleaner
B Reject the change, or ask the AI to fix it, then test again
4. Which statement about comparing before and after is correct?
A Run the same test data on both versions and check that the outputs are identical
B Run the old version on old test data and the new version on brand new test data
5. What does a regression test show after AI optimisation?
A That the optimised code still gives the same results as before
B That the AI tool is always reliable and does not need checking

Match the Pairs

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

Left columnRight column
1Make this function faster and keep the same output AA prompt asking an AI tool for a more efficient version while requiring that behaviour stays unchanged
2assert total_cost(3, 2.5) == 7.5 BIf any result changes, the optimised code has broken behaviour and the change must be rejected or fixed, even if it is faster.
3Compare before and after: both versions must give the same result for the same input CA test case run on both the original and optimised code to check the result is identical
4Re-run the same test cases on the optimised code to prove it still works DShorter or cleaner code is not proof it works. The optimised code must be re-tested with the same test cases before it is accepted.
5The AI version looks shorter, so I will keep it without testing. Eprint(old_total(data) == new_total(data))
6Test 4 now fails on the AI version, but it runs faster, so I will keep it. Ffor t in tests: assert new_total(t[0]) == t[1]

Definitions

regression test

Fill in the Blanks

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

1.
assert newAvg(data) __ oldAvg(data)
2.
______ sumNew([1, 2, 3]) == 6
3.
if newResult __ oldResult: print("Reject the change")
4.
for x __ tests: assert fast(x) == slow(x)
5.
def totalFast(numbers): return ___(numbers)

Spot the Error

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

Question 1
A old = total_old([1, 2, 3])
B new = total_new([1, 2, 3])
C print(old, new)
D assert old = new
Question 2
A cases = [[1, 2], [5], []]
B for c in cases
C old = slow_sum(c)
D new = fast_sum(c)
E assert old == new
Question 3
A old = slow_max([4, 9, 2])
B new = fast_max([4, 9, 2])
C if old == new:
D print('Reject change')
E else:
F print('Reject change')
Question 4
A tests = [(2, 4), (3, 9)]
B for x, want of tests:
C got = square_new(x)
D print(got == want)
Question 5
A data = [3, 1, 2]
B old = sort_slow(data
C new = sorted(data)
D print(old == new)

Answer Key

Optimising code with AI tools
Section A: Quick Questions
Q1
B — Re-run the same test cases and compare the results with the original
Q2
A — Make this function more efficient without changing its output. Explain each change.
Q3
B — Reject the change, or ask the AI to fix it, then test again
Q4
A — Run the same test data on both versions and check that the outputs are identical
Q5
A — That the optimised code still gives the same results as before
Section B: Match the Pairs
Q1
Make this function faster and keep the same output → A prompt asking an AI tool for a more efficient version while requiring that behaviour stays unchanged
Q2
assert total_cost(3, 2.5) == 7.5 → A test case run on both the original and optimised code to check the result is identical
Q3
Compare before and after: both versions must give the same result for the same input → print(old_total(data) == new_total(data))
Q4
Re-run the same test cases on the optimised code to prove it still works → for t in tests: assert new_total(t[0]) == t[1]
Q5
The AI version looks shorter, so I will keep it without testing. → Shorter or cleaner code is not proof it works. The optimised code must be re-tested with the same test cases before it is accepted.
Q6
Test 4 now fails on the AI version, but it runs faster, so I will keep it. → If any result changes, the optimised code has broken behaviour and the change must be rejected or fixed, even if it is faster.
Section C: Definitions
Q1
regression test — Re-running the same test cases on AI-optimised code to prove it still gives identical results as before. If any result changes, reject the change.
Section D: Fill in the Blanks
Q1
assert newAvg(data) == oldAvg(data)
Q2
assert sumNew([1, 2, 3]) == 6
Q3
if newResult != oldResult: print("Reject the change")
Q4
for x in tests: assert fast(x) == slow(x)
Q5
def totalFast(numbers): return sum(numbers)
Section E: Spot the Error
Q1
D — A single = assigns a value and is invalid in an assert. The regression test must compare the old and new results with ==: assert old == new.
Q2
B — A for loop header must end with a colon. It should be: for c in cases:. This loop re-runs every test case on both versions.
Q3
D — The logic is backwards. When old and new results match, the AI change passes the regression test and should be kept: print('Keep change'). Rejecting belongs in the else branch.
Q4
B — Python loops use the keyword in, not of. It should be: for x, want in tests:. The loop runs the same test cases on the optimised function.
Q5
B — The closing bracket is missing from the function call. It should be: old = sort_slow(data). This stores the original version's result for comparing with the AI's version.