Skip to content

Algorithm Extraction

Last time you turned an algorithm into code. This lab runs the machine in reverse: you're handed four lines of working Python and asked to recover the algorithm hiding inside them. Working, that is, except when it isn't — there's a bug in here, and reading the code closely enough to extract its algorithm is exactly how you'll find it.


Overview

You'll read a short piece of code, describe its purpose in one sentence, imagine it as a function, write the algorithm it implements in natural language, and then find and fix the bug that makes it wrong for certain inputs.

Builds on: Monty Hall Algorithm — you'll use the same natural-language algorithm skills, pointed in the opposite direction.

You've got it when…

  • You can state the code's purpose in one sentence.
  • You've named the function, its parameters, and its return value.
  • Your extracted algorithm matches what the code actually does.
  • You've found the bug, named an input that triggers it, and fixed it.

Collaboration & AI

Work: On your own. Compare bug theories with a neighbor after you've written yours down.

AI — AIAS Level 1, No AI: Reading code without a tool explaining it to you is the skill under construction here. What the levels mean.

Recording your answers

Write your answers in your notebook, numbered to match the tasks below.


The Code

Consider this code.

numbers = [3, 7, 2, 9]

max_val = 0
for n in numbers:
    if n > max_val:
        max_val = n

Tasks

  1. What does this code do? Write a single sentence that describes its purpose.

  2. If you were to put this code into a function:

    1. What would you name this function?
    2. What arguments/parameters would this function accept?
    3. What would this function return?
  3. Write the algorithm that this code implements.

  4. This code contains a bug and will produce an incorrect result with certain inputs. What is the bug?

    Stuck? Open for a hint.

    The bug isn't in the loop — it's in an assumption made before the loop starts. What inputs would make that assumption false? Try inventing lists of numbers until one produces an answer you know is wrong.

  5. Fix the bug. Write your corrected code in your notebook, and test it against the input that fooled the original.


Turn It In

  • Your answers to all five tasks, in your notebook. You do not need to produce a Python (.py) file.

How It's Graded

This lab is worth up to 4 points. One score covers everything you turn in.

Score What it looks like
4 — Excellent All five tasks answered in the notebook: a one-sentence purpose, a sensible function name with parameters and return value, an extracted algorithm that matches what the code actually does, the bug named with an input that triggers it, and a fix tested against that input.
3 — Above Average All five answered; the algorithm drifts slightly from the code, or the bug is fixed without naming an input that exposes it.
2 — Average Purpose and a partial algorithm, but the bug is misidentified or left unfixed — the half of the lab that was the point.
1 — Below Average One or two tasks attempted.
0 — Failing Nothing in the notebook.