Due: Tuesday, November 22th, 2022 at 11:59PM ET.
The late deadline for this assignment is Tuesday November 29 (after break ends). Late days will "freeze" from Wednesday to Sunday (during the break). The first counted late day will be the Monday after break (the 28th). You can have a max of two late days (the Tuesday after break) for this assignment.
This assignment mostly has you working with memory diagrams, which we cover to help you understand what your code actually does when updating data. Wednesday's lecture shows the form of memory diagrams expected for this assignment. The last part gives you some practice with dictionaries (Friday's topic), which you will need for the final project.
The goals of this assignment are to:
hw7_code.py
which contains your codetest_hw7.py
file which contains your tests (if you have gotten pytest to work, put import pytest
at the top of this file)You will be submitting three separate files to Gradescope under Homework 7:
tracing.txt
, tracing.pdf
, or tracing.png
with your answers to the "Memory Tracing" section exercisestest_hw7.py
, which will contain the testing function from the "Testing Under Mutation" sectionhw7_code.py
with you answers to the catalog_dict
dictionary and an updated version of update_price1
from the "Dictionaries" sectionYou can submit your answers to this one of the following ways:
If you would like to use a spreadsheet the way we did in class, you can use this template!. To edit it, make a copy (File -> Make a copy) and then when you are done and want to turn it in, take a screenshot and insert it into the document with your answers.
Name your file tracing.txt
, tracing.pdf
, or tracing.png
accordingly.
Here is part of a program for managing orders in online store, one of the first of its kind in the early 2000s. There is a data structure for the Items
for sale, as well as a catalog of all items. There is a data structure for Orders
, which capture the name of a customer and a list of items that the customer wants to buy. There are also two versions of a function to update the price of an item in the catalog.
At the bottom of the file is a sequence of expressions to evaluate.
Task A-1: Without running the program or drawing a memory diagram, predict what will be in each of k_order
, m_order
, and catalog
(items and their prices) after the four expressions evaluate. Write down your answers.
Note: You will not lose points for an inaccurate prediction. The point of the question is to help you calibrate your understanding of memory as you work through the problem.
Note: In the handout, you will see a line that says global catalog
. This just means that the function is going to update the value of catalog
which was defined outside the function.
Task A-2: There are four comments in the code marked "Memory point." Show the contents of memory (known names and memory) at each of these four points. Make sure to include a different memory diagram for each memory point (you should have 4 distinct diagrams).
Task A-3: Write down the contents of k_order
, m_order
, and catalog
as if the entire code has been run, based on your work on the memory diagram.
Note: You will not lose points for an inaccurate answer for this task (as long as you appeared to take the problem seriously)
Task A-4: Now, copy and run the code and compare the final contents of k_order
, m_order
, and catalog
to your answer to the previous task. If they differ, that is an indication that you have a mistake in your memory diagram
Just like we did in class, add a pass
statement at the end of this code, and add a breakpoint (click to the left of the pass
statement's line number, so that a red dot appears).
Run the debugger (Run -> Debug) and observe what the contents of k_order
, m_order
, and catalog
are on the left side of the screen.
Task A-5: Revise your memory diagrams from Task A-2 as needed and write down your final answers for each of the four memory points.
You can add breakpoints in other parts of your code and step through to check if the values each variable takes on are the same as you put in the memory diagram at each step. Refer back to Lab 9 for instructions on using the debugger.
Task A-6: Reflect on your process: were your predictions correct? If yes, write down one way you approached the tasks that you feel was particularly helpful in your thinking. If no, write down a conceptual misconception (not just a wrong answer, but where you think you had a mistake in your thinking) you had and fixed.
You can write these in a text file or draw (and scan to PDF or png) pictures of memory drawn on paper. For the memory diagram, make sure the information for each memory point is clearly labeled so we see which memory layout goes with each point.
Note: Your final file should include:
Remember to check the Handin section for more details!
The online store code contains two versions of a function called update_price
. Assume we want a version of this function in which changes in price reflect in the catalog, but not in any orders that are still open.
Task B-1: Write, in prose (your own words), a list of the changes that you do or do not expect to see after a price is updated. Put this in a comment at the top of your testing file.
Python doesn't have block comments, so just use a multi-line string via #
.
A neat trick that you can do to comment out multiple lines at once is to select all the lines of code and then press ctrl + /
or command + /
!
Task B-2: Use your list to write a test function test_update_price
(using assertions). Follow the structure of the test function that we showed in lab 10 (this week). This test method does not have to run, we will only read your test cases to check conceptual understanding of how a test should look like and would hypothetically run if you had written that function yourself and tested it.
Your function will need to set up some data (catalogs, items, and orders), make one or more calls to update_price
, then run a series of assertions to check the impacts of the update_price
call on the data. The general form of your function should be as follows:
You are only writing a test function for this question. You are NOT writing code for update_price
itself. The goal of this question is to see whether you are understanding how to test the behavior of a function that updates data.
We will cover the material for this part on Friday
Task C-1: In hw7_code.py
, using the provided catalog
list and the relevant items (hp_book, radio, laptop
), create a dictionary named catalog_dict
that maps the item's description to its price.
catalog_dict
should be a dictionary whose keys are strings (the item description). The values should be a float that represents the item's price.
Task C-2: In hw7_code.py
, rewrite the function update_price1
such that it updates an item's price in catalog_dict
.
new_update_price1
. Do not modify update_price1
.new_update_price1
should take the same inputs as update_price1
, and it shouldn't return anything. It should update the existing catalog_dict
dictionary.
Task C-3: You now have two different implementations of catalog
. One that uses lists, and another that uses a dictionary. In hw7_code.py
, write a multiline comment describing the tradeoffs between both implementations, which one do you think is better?
Write down your answer in a multi-line comment using #
.
tracing
either pdf
, png
, or txt
Tasks to be submitted in this component:
test_hw7.py
Tasks to be submitted in test_hw7.py
:
test_update_price
function using the list from Task B-1hw7_code.py
Tasks to be submitted in hw7_code.py
:
catalog_dict
dictionarynew_update_price1
function
Space Traveling [Lo-Fi / Jazz Hop / Chill Mix] by The Jazz Hop Café.
Brown University CSCI 0111 (Spring 2022)
Do you have feedback? Fill out this form.