Programming, problem solving, and algorithms

CPSC 203, 2025 W1

September 25, 2025

Announcements

  • You should be registered for EX2 next week.

  • Post on Piazza if you still need help w machine setup!

Lab Day!

Garden

A __________ is a collection of ___________. Every ________ is a collection of __________.

Messy rows of flowers.

Class Flower

We declare a flower: fl1 = Flower(_____,_____,_____)

What data should a Flower store?

 



Class Flower

We declare a flower: fl1 = Flower(_____,_____,_____)

 

SPECIES = {"rose", "sunflower", "violet", "daisy"}

THEMES: Dict[str, Dict[str, Color]] = {
  "berry":  {"fill": (214,  76, 118), "border": (128,  33,  65)},
  "sun":    {"fill": (247, 181,   0),  "border": (168, 123,   0)},
  "leaf":   {"fill": ( 58, 142,  88), "border": ( 33,  91,  55)},
  "ocean":  {"fill": ( 84, 156, 241), "border": ( 35,  87, 170)},
}

Span:

table of 12 empty cells.

Class Flower

Notes:

  • images of flowers are already defined for you using geometry from Image library.
  • call the functions by referring to the file containing the drawers – fl.daisy(pos,span,theme), for example.
  • All locations are in terms of CELL coordinates, not pixel coordinates.
  • Start your exercises by looking at the driver code.
  • Flowers don’t need to know anything about how they’re going to be used.

Finish the Flower Class

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

15:00

If you finish early, draw more flowers!

Class Row

We declare a row: row1 = Row(_____,_____,_____)

What data should a Row store?

 



Class Row

Drawing a Row:

Given a list of flowers, and a gap:

[f1, f2, f3], gap = 2

Draw the row:

table of 12 empty cells.

 

Puzzle: How do you know where to draw each flower?

Class Row

Notes:

  • Most complex part is computing where each flower in the row should be draw.
  • Render will simply call flower renderers, with the correct locations.
  • All locations are in terms of CELL coordinates, not pixel coordinates.
  • Start your exercises by looking at the driver code.
  • Rows don’t need to know anything about how they’re going to be used, but they do need to know about Flowers.

Finish the Row Class

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

15:00

If you finish early, draw more rows!

Class Garden

We declare a garden: g1 = Garden(_____)

What data should a Garden store?

 

What functionality should a Garden have?

 

 

Class Garden

Notes:

  • Render will simply call Row renderers; Rows know their locations!
  • All locations are in terms of CELL coordinates, not pixel coordinates.
  • Start your exercises by looking at the driver code.
  • Gardens need to know about Rows, but only indirectly about Flowers.

Finish the Garden Class

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

15:00

If you finish early, make your garden fancy!

Conclusion

What did you accomplish?

  • Increased fluency in Python from navigating existing code.

  • You know that a dataclass is a collection of data and functionality, and you have created a few.

  • Via examples, you are beginning to understand how to decide upon ownership of application data.