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

  1. Define a function book_size (along with a testing function test_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:

    raise ValueError("error message")

    And here is an example of testing that an error is raised in Python that is analogous to f(-1) raises "positive" in Pyret:

    with pytest.raises(ValueError, match="positive"):
        f(-1)
  2. Define book_sizes_sample to 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.

  3. Define a dataclass Book with 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:

      PRINTED = "printed"
      ONLINE = "online"

      Then use PRINTED and ONLINE instead of the raw string literals.

  4. Define a function book_description that takes a book and returns a string that contains the book title and the description of the book size (according to book_size) in the following format: "Title is a long book".

  5. Define a function all_long that 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"]) is true, all_long(["short", "long"]) is false, and all_long([]) is true. The function should be implemented using a for-loop.

  6. Define a function only_positive (along with a test function test_only_positive) that takes a list of float numbers 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

  1. Define a function my_map that 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.

  2. Define a testing function test_my_map that checks the correctness of my_map against the built-in map function in two ways:

    • First, test that my_map produces the same result as map for a few example lists and functions.
    • Second, write a for-loop over a list of lists and, inside the loop, test that applying my_map and map produce the same result for each list in the outer list.
  3. Define a function my_running_sum on 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.