A11 — Revisiting familiar concepts in Python
Revisit familiar concepts (functions, conditionals, and simple structured data) but in Python.
Due Nov 11, 2026
Estimated time to complete (just the assignment, excluding the prep materials): 60–90 minutes.
Prep materials:
What to do
Download a11.py and add all your code between imports and pytest.main([__file__]). Make sure you follow this guide: use mypy to check your code for type errors and test your code as expected by pytest.
Submit the file to Assignment 11 Submission on Brightspace. Remember 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 and always write tests even if they are not explicitly required or autograded (you have a better chance of creating a correct solution that way).
Essentials
Define a function
book_size(along with a testing functiontest_book_size) that takes an integer number of pages and returns a string describing the size of a book: “short” if the book has fewer than 200 pages, “medium” if it has at least 200 but fewer than 400 pages, and “long” otherwise. If the given number of pages is zero or negative, the function should raise an error. Hint.Here is an example of raising an error in Python that is analogous to
raise "error message"in Pyret:And here is an example of testing that an error is raised in Python that is analogous to
f(-1) raises "positive"in Pyret:Define
book_sizes_sampleto be a list of book-size descriptions corresponding to the following list of page values: 112, 34, 670, 199, 1025, 80. Do not write any new functions here; you have to use built-in Python functions to compute the answer. Hint.Define a dataclass
Bookwith four fields: title (a string), year (an integer), pages (an integer), and format (either printed or online).Until we learn about enumerations, use the string type to represent format.
To make it less likely to make a typo when dealing with format values, create constants for the valid format values:
Then use
PRINTEDandONLINEinstead of the raw string literals.
Define a function
book_descriptionthat takes a book and returns a string that contains the book title and the description of the book size (according tobook_size) in the following format:"Title is a long book".Define a function
all_longthat takes a list of book-size descriptions and returns true if every description in the list is"long", and false otherwise. For example,all_long(["long", "long", "long"])istrue,all_long(["short", "long"])isfalse, andall_long([])istrue. The function should be implemented using a for-loop.Define a function
only_positive(along with a test functiontest_only_positive) that takes a list offloatnumbers and returns a list of the positive numbers from the list, in the same order. The function should be implemented using a for-loop.
Dig Deeper
Define a function
my_mapthat takes a list and a function and applies the function to each element of the list, returning a new list of the results. Do not write any type annotations for this function.Define a testing function
test_my_mapthat checks the correctness ofmy_mapagainst the built-inmapfunction in two ways:- First, test that
my_mapproduces the same result asmapfor a few example lists and functions. - Second, write a for-loop over a list of lists and, inside the loop, test that applying
my_mapandmapproduce the same result for each list in the outer list.
- First, test that
Define a function
my_running_sumon a list of integer numbers that does the same things as this Pyret version from the prep materials but uses a for-loop instead of recursion. What plays the role of the accumulator in your function? Post an answer to this question on Discord, in[A11] my_running_sum, or like previous posts that express the same idea.
Grading
Auto-graded for correctness and some aspects of style, with some of the named test functions auto-graded for coverage.