.py
files containing your work. (If you upload a screenshot or other file format, you won't get credit.)This homework assignment must be submitted in Gradescope by 10am CST on Tuesday, October 5, 2021.
This homework focuses on JSON and CSV files, as well as the data structures stack and queue.
Collaboration is prohibited, and you may only access resources (books, online, etc.) listed below.
The course materials you may refer to for this homework are:
json
or csv
in the name.This homework assignment has 2 problems, numbered 2 and 3. The grading breakdown is:
Points | Item |
---|---|
2 | Autograder |
4 | Problem 2 |
4 | Problem 3 |
10 | Total |
Ask your instructor or TA a question by email, in office hours, or on discord.
Gradescope will show the results of the automated syntax check of all submitted files as the score for problem 1.
Modify the sample program parentheses.py we developed in class to create a new program called hwk7prob2.py
that knows about both brackets [
,]
and parentheses (
,)
and can check for proper matching in an expression that may contain both. Brackets can match brackets, and parentheses can match parentheses, but a bracket cannot match a parenthesis. There's no restriction on which grouping symbol can be used inside the other, so all of these expressions are valid:
[(1+2) - (3+4)]
([1 + (2-3)]+4)
[5 * [6 - 7]]
Notice that there are three kinds of errors that the program needs to be able to report:
[(1+2)-3
or ([5+6]
]
or )
appears when there isn't anything to match it with, e.g. (1+2)]
or ((1-2)+4))
]
would match an earlier (
, or a )
would match an earlier [
, e.g. [1+2)
or ((5+6)-7]
The first two errors are also present in the example parentheses.py
, but the third type is new to this modified version of the program and you need to add code to detect and report it.
Hint: The basic structure of the program won't change very much, but you will need to change what is stored on the stack. The original parentheses.py
stores a position (integer), and that's no longer enough information. I suggest you make it so that instead of pushing an integer onto the stack, the new program pushes a list like ["(",7]
or ["[",5]
that indicates both the delimiter type (first element) and the position in the string (second element). This will, of course, change the way you access the position when you pop something off the stack. Another option is to push a dictionary like {"delimiter": "(", "position": 7}
.
VERY IMPORTANT: For full credit, you must not leave any comments or docstrings in parentheses.py
that are inaccurate because of your changes. Whenever you edit a program, you need to make sure you edit any comments that are affected by your changes!
Suppose I have a CSV file called elements.csv
containing a list of chemical elements that looks like this:
number,abbreviation,name
11,Na,Sodium
80,Hg,Mercury
Write a program hwk7prob3.py
that will read this file and write a JSON file elements.json
in the following format:
[ {"number": 11, "abbreviation": "Na", "name": "Sodium"},
{"number": 80, "abbreviation": "Hg", "name": "Mercury"} ]
Notice how the output JSON file contains a list of objects, and each object has keys that match the three column names from the CSV file. Also, the atomic number field is an integer in the JSON output (whereas it will be a string when you read it using the csv
module).
You can use the CSV example above as the content of a file elements.csv
for testing purposes, but your program needs to be able to handle any CSV file listing chemical elements in that format (with three columns, "number", "abbreviation", and "name", appearing in that order).
Your program can assume that elements.csv
is in the current working directory when it is run.
Restricted methods note: For full credit your answer needs to use the csv
module to read the file elements.csv
.