Programming, problem solving, and algorithms

CPSC 203, 2025 W2

January 27, 2026

Announcements

Quick recap: Dataclasses

A dataclass bundles related data together into a custom type.

  • Attributes: data stored in the object (radius, colour)
  • Methods: functions that belong to the class (diameter)

A bead bracelet

A colorful beaded bracelet.

What data describes a bead?

What data describes a bracelet?

The Bracelet

A bracelet contains a list of beads and has a radius.

This is composition: a Bracelet contains Beads.

Creating a Bracelet

Does it fit?

The beads fit on the bracelet if the sum of their diameters is less than or equal to the circumference. We refer to the extra string as slack.

How much slack?

Adding beads safely

We only want to add a bead if it fits!

Removing beads

What if we want to swap out a bead?

Hint: Lists have a method called pop(index) that removes and returns the item at that index.

Drawing the bracelet

The full picture

@dataclass
class Bead:
    radius: float
    colour: tuple[int, int, int]

    def diameter(self) -> float: ...
    def draw(self, ax, x: float, y: float) -> None: ...

@dataclass
class Bracelet:
    radius: float
    beads: list[Bead]

    def circumference(self) -> float: ...
    def slack(self) -> float: ...
    def string_bead(self, bead: Bead) -> bool: ...
    def remove_bead(self, index: int) -> Bead: ...
    def draw(self) -> None: ...

Your turn: Implement Bracelet

Try the Bracelet Activity on PrairieLearn!

Implement these methods:

  • circumference() - return 2πr
  • slack() - circumference minus total bead diameters
  • string_bead(bead) - add bead if it fits, return True/False
  • remove_bead(index) - remove and return the bead at index

If you finish early, try Tasks 4 & 5 (brighten methods)!

30:00

Extending functionality: Name bracelets

What if we want to make a friendship bracelet with someone’s name on it?

A bracelet with letter beads spelling CS203.

We need beads that can hold letters!

Adding letters to beads

Let’s add an optional letter attribute to our Bead class:

Now beads can optionally have letters!

Python’s enumerate() function

When iterating over a list, sometimes you need both the index and the value:

enumerate() gives you pairs of (index, value) as you loop.

Naming a bracelet

Add a name() method to Bracelet that assigns letters to existing beads:

Why reversed? So the name reads correctly around the circle!

Using name()

Your turn: Letter beads

Back to PrairieLearn - try Task 6: Letter Beads!

15:00

Design Strategies

  1. Identify the objects in your problem (nouns)

  2. List the data each object needs (attributes)

  3. List the operations each object supports (methods)

  4. Decide which object is responsible for what

  5. Implement and test incrementally

What you learned

  • Dataclasses let us define custom types with attributes
  • Methods are functions that belong to a class
  • Composition: objects can contain other objects
  • Design: who owns what data and behavior matters!