{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Denison CS181/DA210 Homework\n", "\n", "Before you turn this problem in, make sure everything runs as expected. This is a combination of **restarting the kernel** and then **running all cells** (in the menubar, select Kernel$\\rightarrow$Restart And Run All).\n", "\n", "Make sure you fill in any place that says `YOUR CODE HERE` or \"YOUR ANSWER HERE\"." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import json\n", "import os\n", "import os.path\n", "import io\n", "import sys\n", "from contextlib import redirect_stdout\n", "\n", "datadir = \"publicdata\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q1:** In the data directory is a file named `fib.json` containing a JSON-encoded list of numbers from the Fibonacci sequence. Write a function\n", "\n", " readJSONlist(path)\n", "\n", "that reads and returns a list from `path`, **assuming a JSON-encoded text file**. If `path` does not exist, or if the result is a data structure other than a list, return the empty list. (Hint: the Python function `isinstance()` can be helpful to verify the type of a data structure.)\n", "\n", "Then invoke the function on `fib.json` in `datadir` and assign the result to `result`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "3fca93a65c6f2a016ab519bfee14ef77", "grade": false, "grade_id": "cell-d2bae836fbc119c2", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# Solution cell\n", "\n", "# YOUR CODE HERE\n", "raise NotImplementedError()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "f71fb6b464182fce4255145e2a236b5f", "grade": true, "grade_id": "cell-82c0e42c81213cef", "locked": true, "points": 3, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "# Testing cell\n", "\n", "assert result[-1] == 89\n", "assert len(result) == 12\n", "\n", "result2 = readJSONlist(\"./foobar.json\")\n", "assert result2 == []\n", "\n", "result3 = readJSONlist(os.path.join(datadir, \"config.json\"))\n", "assert result3 == []" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q2:** Write a function\n", "\n", " writeSquares(N, filepath)\n", "\n", "that creates a list of the squares of integers from 1 to `N` and then writes them out as a JSON-encoded text file to the file location given by `filepath`. If N is less than or equal to 0, no file should be created. This function returns no value." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "3fd16cfa9b93fd5243c867cd8a63f29e", "grade": false, "grade_id": "cell-469c714260019773", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# Solution cell\n", "\n", "# YOUR CODE HERE\n", "raise NotImplementedError()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "672f8af0e387c045094b3d43c2968f2d", "grade": true, "grade_id": "cell-e64a97ee03c45a63", "locked": true, "points": 3, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "# Testing cell\n", "\n", "filepath = \"./squares.json\"\n", "writeSquares(5, filepath)\n", "result = json.load(open(filepath, 'r'))\n", "assert result == [1, 4, 9, 16, 25]\n", "writeSquares(1, filepath)\n", "result = json.load(open(filepath, 'r'))\n", "assert result == [1]\n", "os.remove(filepath)\n", "writeSquares(0, filepath)\n", "assert not os.path.isfile(filepath)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q3:** In the data directory is a file named `baby_2015_female_namecount.txt` with a line for each of the top female baby names of 2015. Each line contains tab-separated values for the name and the count of US Social Security applications for that name. Start by writing a function\n", "\n", " readNameCount(path)\n", "\n", "that reads a file formatted this way, given by `path` and returns a tuple of two lists, the first being the names and the second being the counts. The function should behave properly regardless of how many name-count lines are in the file. If the specified path does not exist, your function should return `None`. Make sure your count values are integers.\n", "\n", "Then write code that *uses* your function to obtain the lists and **writes the result** to a **JSON-encoded** text file in the current directory named `namecount.json`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "7c0c9e4ae02d2722b1c8db8dd4c48c9f", "grade": false, "grade_id": "cell-00f558d6134a208b", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# Solution cell\n", "\n", "# YOUR CODE HERE\n", "raise NotImplementedError()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "475f4c55e1eda7d3dd2e50af27f31ba4", "grade": true, "grade_id": "cell-8d778e7c955efac5", "locked": true, "points": 3, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "# Testing cell\n", "\n", "assert readNameCount(\"./foo\") == None\n", "result = readNameCount(os.path.join(datadir, \"baby_2015_female_namecount.txt\"))\n", "assert len(result) == 2\n", "assert len(result[0]) == 10\n", "assert len(result[1]) == 10\n", "jsonpath = os.path.join(\".\", \"namecount.json\")\n", "assert os.path.isfile(jsonpath)\n", "with open(jsonpath, 'r') as f:\n", " result2 = json.load(f)\n", "assert len(result2) == 2\n", "assert len(result2[0]) == 10\n", "assert len(result2[1]) == 10" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q4:** Consider the table of data:\n", "\n", "subject | name | department\n", "---------|-----|--------------\n", "CS | Computer Science | MATH\n", "MATH | Mathematics | MATH\n", "ENGL | English Literature | ENGL\n", "\n", "This could be represented in a Python program as a *dictionary* mapping from a key, `\"subjects\"`, to a *list* of dictionaries, one per subject. These inner dictionaries map from `\"subject\"`, `\"name\"`, and `\"department\"` to the respective value for the row. Be careful and follow the above specification **exactly**.\n", "\n", "Write Python code to construct the above representation as a Python in-memory data structure called `ds`, and then have your code **encode the data structure** into a JSON-formatted string called `s`. Print the string. Then take `ds` and encode as a JSON-formatted string, but include `indent=2` as an argument in the conversion, and assign to `s2` and print it." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "364d4fa4d63009ff7faa54409fb15953", "grade": false, "grade_id": "cell-0abd378083a7bdd9", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# YOUR CODE HERE\n", "raise NotImplementedError()\n", "print(s)\n", "print(s2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "38d8ce0d5ab6a78e0a425875000451ee", "grade": true, "grade_id": "cell-765331274d04ea6d", "locked": true, "points": 3, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "# Testing Cell\n", "\n", "f = io.StringIO()\n", "with redirect_stdout(f):\n", " print(s)\n", " \n", "assert f.getvalue() == '{\"subjects\": [{\"subject\": \"CS\", \"name\": \"Computer Science\", \"department\": \"MATH\"}, {\"subject\": \"MATH\", \"name\": \"Mathematics\", \"department\": \"MATH\"}, {\"subject\": \"ENGL\", \"name\": \"English Literature\", \"department\": \"ENGL\"}]}\\n'\n", "\n", "f2 = io.StringIO()\n", "with redirect_stdout(f2):\n", " print(s2)\n", "\n", "assert f2.getvalue() == '{\\n \"subjects\": [\\n {\\n \"subject\": \"CS\",\\n \"name\": \"Computer Science\",\\n \"department\": \"MATH\"\\n },\\n {\\n \"subject\": \"MATH\",\\n \"name\": \"Mathematics\",\\n \"department\": \"MATH\"\\n },\\n {\\n \"subject\": \"ENGL\",\\n \"name\": \"English Literature\",\\n \"department\": \"ENGL\"\\n }\\n ]\\n}\\n'" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.3" } }, "nbformat": 4, "nbformat_minor": 4 }