Programming, problem solving, and algorithms

CPSC 203, 2026 W1

September 24, 2026

Announcements

  • POTW 2 and 3…

  • Registration for examlet 1.

Colour Representations (Revisited)

Colour and Code

Make the poppies pop!

A field of red poppies.

PC: https://www.epicgardening.com/spring-wildflowers-from-seed/

Examine each pixel. If its red color channel is high (over 200, say), then leave it as is. Otherwise, change the pixel to grey by setting all 3 color channels to the average of their original values.

Colour and Code

If necessary, go back and review the slides on colour representation from previous lecture.

Open the List Activity on PrairieLearn and choose the Make the Poppies Grow question.

Examples of programming idiom:

  • “A commonly-used way to code a relatively small construct in a particular programming context.”
  • “Using the idioms for a programming context helps a team work together since they lower the cognitive load of the resulting code.”

Your turn: Change image or boost

Experiment with the code! Try one or more of these:

  1. Find a different image URL and boost a different color (green? blue?)
  2. Change the threshold value (try 150 instead of 200)
  3. Instead of greying, try inverting: red = 255 - red
  4. What happens if you boost pixels that are LOW in red instead of high?

Data Classes

Compound Data Types

So far we have focused on

  • Simple atomic types, such as built-ins int, str or library types np.uint8.
  • Arbitrary-sized types, such as list or tuple.

But we often want to store a fixed number of pieces of information which naturally belong together.

  • Representing a UBC student might require a student number, a first name, a last name, a date of birth, etc.
  • Representing a car might require a make, a model, a colour, a license plate, etc.
  • A license plate might require a jurisdiction and a plate “number”.

For that, we use compound data types.

Our Example: A Bead Bracelet

A colorful beaded bracelet.

What data describes a bead?

What data describes a bracelet?

Previously in CPSC 103 or CPSC 110

In CPSC 103, we used NamedTuple to create a compound type:

from typing import NamedTuple
Bead = NamedTuple('Bead', [('r', float), ('c', tuple[int, int, int])])
B1 = Bead(5.0, (255, 0, 255))

def circumference(b: Bead) -> float:
    return 2 * math.pi * b.r

In CPSC 110, we used define-struct to create a compound type:

(@htdd Bead)
(define-struct bead (r, c))
;; Bead is (make-bead Float Color)
;; interp. A bead with radius r and RGB colour c
(define B1 (make-bead 5.0 `(255, 0, 255)))

(define (circumference b)
  (* 2 pi (bead-r b)))

Dataclasses in Python

A dataclass bundles related data together into a custom type.

from dataclasses import dataclass

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

This creates a new type called Bead with two attributes.

Creating Bead Objects

Vocabulary

  • In CPSC 103 / 110 we talked about a type vs an instance of that type.
    • A type is a category of data, and explains how that data maps to information (the representation and interpretation). For example, an int is a type for storing integers.
    • An instance is a concrete example of a type. For example, the value 42 is an instance of the type int.
  • In Python (and many other languages) compound types use special terminology.
    • The type is called a class.
    • An instance of a class is called an object.

Your Turn: Beads

What would these print?

Adding Behavior: Methods

A dataclass can also have methods.

Adding Behavior: More Methods

Add a draw method:

Demo