A document from MCS 260 Fall 2021, instructor David Dumas. You can also get the notebook file.

MCS 260 Fall 2021 Homework 7

  • Course instructor: David Dumas

Instructions:

  • Complete the problems below, which ask you to write Python scripts.
  • Upload your python code directly to gradescope, i.e. upload the .py files containing your work. (If you upload a screenshot or other file format, you won't get credit.)

Deadline

This homework assignment must be submitted in Gradescope by 10am CST on Tuesday, October 5, 2021.

Topic

This homework focuses on JSON and CSV files, as well as the data structures stack and queue.

Collaboration

Collaboration is prohibited, and you may only access resources (books, online, etc.) listed below.

Resources you may consult

The course materials you may refer to for this homework are:

Point distribution

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

What to do if you're stuck

Ask your instructor or TA a question by email, in office hours, or on discord.

( 1. There's no problem 1 )

Gradescope will show the results of the automated syntax check of all submitted files as the score for problem 1.

2. Multiple delimiter matching

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. Too many left delimiters: some brackets or parentheses remain open at the end of the expression, e.g. [(1+2)-3 or ([5+6]
  2. Too many right delimiters: A ] or ) appears when there isn't anything to match it with, e.g. (1+2)] or ((1-2)+4))
  3. Delimiter type mistmatch: A ] 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!

3. Chemical element CSV to JSON

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.

Revision history

  • 2021-10-07 Initial release