Programming, problem solving, and algorithms

CPSC 203, 2025 W1

September 18, 2025

Announcements

  • EX1 in the books. Nice work. A few little bugs in questions, but avg is already 93%.

  • Many office hours this week to help w machine setup. You’ll need it for Projects, and for your future.

Back to handcraft

A quilt in different colours—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 colours 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 colours.

Where are we?

A teal knitted dishcloth with a basketweave pattern and two red knitting needles resting on it.

A 2D grid with one square extended, showing it as a smaller grid pattern that repeats across the larger grid.

Program design

A chevron knitting pattern with zigzag lines.

A knitting pattern with diagonal stripes.

A knitting pattern with diagonal stripes.

A handcraft is a collection of __________. Every __________ has a __________, and a collection of __________. Every __________ is a collection of __________. Every __________ is either “knit” or “purl,” and is drawn as a __________.

Design Strategies

  1. Decompose a problem into classes

  2. List the data associated with each class

  3. Write the “driver” code that illustrates the functionality you expect from each class.

  4. Implement the functions you expect.

  5. Run the driver code to test your functionality.

Which classes?

A 2D grid with one square extended, showing it as a smaller grid pattern that repeats across the larger grid.

Handcraft:

Block:

Row:

Stitch:

Image:

Translation

From stitch coordinates to pixel coordinates

ImageDraw.Rectangle(xy, fill):

  • xy is [(x0,y0),(x1,y1)] a bounding box, inclusive.
  • fill is a color

Let’s Write

https://us.prairielearn.com/pl/course_instance/193300/assessment/2590319

Interface

A row has a list of stitches:

class row:
  ...
  stitches: list[stitch]
  ...

But we want to declare rows like:

r1 = row('KKKPPPKP')

To do this, we need a function to translate between string of K, P and stitch objects.

Define __init__: special function invoked for you by the system when variable is declared.