Programming, problem solving, and algorithms

CPSC 203, 2025 W2

January 16, 2026

Announcements

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

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?
12:00

A little math puzzle break

\(1 + 3 + 5 + \ldots + 1337\) = ____________


How many terms?

puzzle continued…

English description:

The sum of the first \(k\) odd integers is ________

What about \(11 + 13 + \ldots + 23\)?



And \(2 + 4 + 6 + \ldots + 1338\)?

Python Demo: Lists

Exploratory code lives at https://us.prairielearn.com/pl/course_instance/193300/assessment/2587679

Don’t worry if you haven’t set up your own machine. The infrastructure on PL is adequate for most things we’ll do.

Back to handcraft

A quilt in different colors—green and pink—and patterns—paisley and flowers.

A white doily with a spiral pattern on a wooden surface.

A pen made from pinecone.

A vibrant woven textile with intricate geometric patterns in various colors the main being red, displayed against a mossy wooden background.

A violin made from intricately quilled green paper with a bow.

A bicycle covered in crocheted yarn in various colors.

Representing handcraft

How do we represent a craft project in code?

We need to think about:

  • What data describes the object?
  • What operations can we perform on it?

Let’s start with something simpler than a full quilt…

A bead bracelet

A colorful beaded bracelet.

What data describes a bead?


What data describes a bracelet?

Dataclasses in Python

A dataclass bundles related data together into a custom type.

This creates a new type called Bead with two attributes.

Creating Bead objects

Your turn: Beads

What would these print?

02:00

Adding behavior: methods

A dataclass can also have methods.

Calling methods

What you learned today

  • The sum of the first k odds is _____
  • Lists are more complex than they first appear.
  • Dataclasses let us define our own types
  • Attributes are the data associated with an instance of a class
  • Methods are functions that belong to a class

Next time: The Bracelet