Skip to main content
AP CSP course

C34 · Topic 3.9 · 40 minutes · AAP-2.L, AAP-2.M

Building and combining algorithms

By the end you can:

  • Compose algorithms using their outputs and inputs.
  • Preserve order and data types.
  • Refine a stated specification with tests.

Before this lesson: topics 3.6, 3.8.

Equivalent algorithms
Algorithms with the same required result or effect for every allowed input under the stated contract.
Counterexample
A permitted input that disproves a general claim.

Understand

A larger algorithm can combine smaller ones when their interfaces fit. Filtering returns a collection; counting its length returns a number. State the intermediate representation so a later step does not accidentally treat a number as a list or a first match as a complete result.

Modify an algorithm around an explicit requirement and preserve behaviours still needed by its callers. A new condition such as an upper bound changes which entries qualify; test both boundaries, duplicates and an empty list. Composition does not eliminate the need to reason about each component and their connection.

Worked example

Build a recommendation count for values between 5 and 8 inclusive.

  1. Filter[1,5,8,5,10]to[5,8,5].
  2. Take the filtered list’s length to obtain 3.
  3. A direct full-traversal range counter should also return 3; returning at the first match would not.

Explore each concept

Choose a concept, change its inputs and follow the explanation. Each example models the stated rule or scenario; completing it is practice rather than evidence of mastery.

  • Minimum and maximum
  • Sum and average
  • Divisibility and composition
Open this example on its own

Change it · step through · explain

Change an input, predict what will happen, then advance one step. Read why the state changes, reset and experiment. This fixed teaching model does not save progress or award XP.

Ready · Make a prediction

Ready to explore: Build algorithms from running summaries
  1. Values, comma separated (0–100; max 8)2,7,3,7Waiting
  2. AlgorithmsumWaiting

Ready to explore: Build algorithms from running summaries

Why: Change an input, predict the first effect, then advance one step.

Try this: Change one input or choose another listed case. Predict the effect, reset and compare the states and governing rule.

Step through and experiment

Change it · step through · explain

Change an input, predict what will happen, then advance one step. Read why the state changes, reset and experiment. This fixed teaching model does not save progress or award XP.

Ready · Make a prediction

Ready to experiment
  1. Input 12,7,3,7,0Waiting
  2. Input 25Waiting

Ready to experiment

Why: Predict the first change, then advance one step.

Try this: Try repeated boundary values, no matches and an empty list.

Check a misconception

Claim: A filter and a count return interchangeable data types.

A filter returns a list; its length is a number. Their contracts must fit the next step.

Guided practice

Write a precise plain-language pipeline that displays qualifying values and their count.

Work with a partner. Make a prediction, compare your reasoning, then identify the rule that supports your answer.

Independent transfer

Add an inclusive upper bound to a minimum-only filter and give distinguishing tests.

Exit check

What should be checked before replacing two copied filters with a common procedure?

Write your reasoning before comparing with a classmate. If your teacher assigned this task, submit it through My classes to save your work and receive feedback.

Check yourself

  1. Question 1 of 2Which pipeline counts entries selected by a filter?

    Select one answer.

  2. Question 2 of 2For[1,5,8,5,10], inclusive bounds 5…8 give what count?

    Select one answer.

Your practice so far

Check an answer to see its rationale.

Teacher guidance and scoring