A9 — Review, mutation, and for-loops
Revisit collective and structured data; start using variables that can change.
Due Oct 28, 2026
Estimated time to complete (just the assignment, excluding the prep materials): 60–90 minutes.
Prep materials: Week 9: Getting started with mutation and Python, in particular DCIC 9.1.8.5 for each loops in Pyret.
What to do
Download the file a9.arr and add all your code there. It already contains the games table from Assignment 3. Submit the file to Assignment 9 Submission on Brightspace. Remember to use the auto-grader, test your Brightspace submission, and send your score to Brightspace when you are done working on the submission.
Do not compute any answers by hand or type out lists that you could get from the table.
Essentials
Part 1: Review
Define
games-popularto be the table of games with at least 1800 copies sold. Hint.Define a data type
Gamewith a single constructorgamethat has four fields:title(a string),platform(a string),rating(a number), andprice(a non-negative number).Define a function
is-highly-rated(along with a check-block namedcheck-is-highly-rated) that takes aGameand returnstrueif its rating is at least 8.0, andfalseotherwise.Define a function
row-to-gamethat takes a row of thegamestable and returns the correspondingGame. For example,row-to-game(games.row-n(1))isgame("Castle Run", "Switch", 7.8, 15). Hint.Define
games-listto be the list ofGames for all the rows ofgames, in the same order as in the table. Hint.Define
games-list-highly-ratedto be the list ofGames ingames-listthat are highly rated. What is another way to get the same list without usinggames-list?
Required but not graded or submitted: create a dataclass Game in Python with the same fields as the Pyret game constructor, and define and test a function is_highly_rated.
Part 2: Variables that change and for each
Define a mutable variable
year-bornwith the value1980. Hint.Define a function
calc-agethat takes a year and returns the age of someone born inyear-bornin that year. For example,calc-age(2026)is46.After the last call to
calc-age, update the value ofyear-bornto1990. What is the value ofcalc-age(2026)now? Why?Add the following function to your file:
Do you understand what it does? Why do we need to use
blockhere? Hint.Compute the square of
3by usingmy-sqr-side-effectand store the result in (immutable)my-sqr-3. What is the value ofcalc-age(2026)now? Why?How can we write tests for
calc-age? Think about this and submit your proposal on Discord, to Testing calc-age in#questions, or react to an existing proposal if it matches your idea.Define a function
total-price(along with a check-block namedcheck-total-price) that takes a list ofGames and returns their total price. For example,total-price(games-list)is 208. For this exercise, follow the template from DCIC.
Think of at least two other ways to write this function.Define a function
titles-underthat takes a list ofGames and a price, and returns the list of titles of the games that cost less than that price, in the same order as in the input list. For example,titles-under(games-list, 20)is[list: "Castle Run", "Farm Life"]. Once again, usefor eachto implement this function.
Dig Deeper
Define a function
loop-mapwith type variablesFandTthat works likemy-mapfrom Assignment 8, but usesfor each. Check thatloop-map(lam(g): g.title end, games-list)is thetitlecolumn ofgames, and thatloop-mapworks likemy-mapand built-inmapfor a few other examples.Define
first-titleto be the first game title oftitles-under(games-list, 100)sorted alphabetically. Hint.Define a function
similarthat takes two strings and returnstrueif they are the same when ignoring case, andfalseotherwise. For example,similar("PC", "pc")istrue, andsimilar("PC", "Switch")isfalse.Define a check-block
check-first-title, check thatfirst-titleis both"battle arena"and"Battle ARENA"when ignoring case. Hint.
Grading
Auto-graded for correctness and some aspects of style, with named check-blocks auto-graded for coverage.