{ "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 os\n", "import os.path\n", "\n", "datadir = \"publicdata\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q1** Assume a CSV file with the same format used in our variations of `topnames` with a header line containing the four column names, and then $x$ rows of comman-separated field values. Write a function\n", "\n", " readTopNamesLoD(path)\n", "\n", "that reads the file and creates a LoD representation and returns the structure." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "e25f39844d9d2748bff03c53cbc941a4", "grade": false, "grade_id": "cell-3ac611d5fbae072d", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# YOUR CODE HERE\n", "raise NotImplementedError()\n", "tn10data = readTopNamesLoD(os.path.join(datadir, \"tn10.csv\"))\n", "print(tn10data)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "4e11b79c99ee82d12b348abc23ebb02e", "grade": true, "grade_id": "cell-ab5935ca57c293ca", "locked": true, "points": 2, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "tn10data = readTopNamesLoD(os.path.join(datadir, \"tn10.csv\"))\n", "assert isinstance(tn10data, list)\n", "assert len(tn10data) == 10\n", "assert len(tn10data[0]) == 4\n", "assert isinstance(tn10data[0], dict)\n", "assert 'year' in tn10data[0]\n", "assert tn10data[0]['sex'] == 'Female'" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "576595eabedcb299d2517a2a60baf438", "grade": true, "grade_id": "cell-0367eb87726497a0", "locked": true, "points": 1, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "# hiddent tests here\n", "assert True\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q2** Write a function \n", "\n", " addCatColumnLoD(tnLoD, threshold1, threshold2)\n", " \n", "that adds a categorical column to a LoL representation in parameter `tnDoL` with the new column named `category` whose values are the strings `\"small\"` when count is below `threshold1`, is `\"medium\"` when count is greater than or equal to `threshold1` and less than `threshold2`, and `\"large\"` when count is greater than or equal to `threshold2`. The function should perform its modifications in place." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "e2e468bb34e3efd8d81862ac6851f059", "grade": false, "grade_id": "cell-0d9878d3fcbb29fa", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# YOUR CODE HERE\n", "raise NotImplementedError()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "38069b13b60b1e9d8fcf2bfa00cb9228", "grade": false, "grade_id": "cell-234c176a7a087ee7", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "LoD = [{'year': 2014, 'sex': 'Female', 'name': 'Emma', 'count': 20936}, \n", " {'year': 2014, 'sex': 'Male', 'name': 'Noah', 'count': 19305}, \n", " {'year': 2015, 'sex': 'Female', 'name': 'Emma', 'count': 20455}, \n", " {'year': 2015, 'sex': 'Male', 'name': 'Noah', 'count': 19635}, \n", " {'year': 2016, 'sex': 'Female', 'name': 'Emma', 'count': 19496}, \n", " {'year': 2016, 'sex': 'Male', 'name': 'Noah', 'count': 19117}, \n", " {'year': 2017, 'sex': 'Female', 'name': 'Emma', 'count': 19800}, \n", " {'year': 2017, 'sex': 'Male', 'name': 'Liam', 'count': 18798}, \n", " {'year': 2018, 'sex': 'Female', 'name': 'Emma', 'count': 18688}, \n", " {'year': 2018, 'sex': 'Male', 'name': 'Liam', 'count': 19837}]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "0688c41a24079d2cb3edb6306c9a4bea", "grade": true, "grade_id": "cell-1da7403c4d9cd8aa", "locked": true, "points": 3, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "addCatColumnLoD(LoD, 19000, 19500)\n", "assert len(LoD) == 10\n", "assert isinstance(LoD[0], dict)\n", "assert len(LoD[0]) == 5\n", "assert 'category' in LoD[0]\n", "assert [ rowD['category'] for rowD in LoD ] == ['large', 'medium', 'large',\n", " 'large', 'medium', 'medium', 'large', 'small', 'small', 'large']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q3** Write a function\n", "\n", " dropColumnLoD(LoD, columnname)\n", " \n", "that drops the column specified by `columnname` from the list of dictionary representation given in `LoD`. This should be done \"in place\" and should **not** assume any particular columns. If the specified `columnname` is not present, there should be no error, nor effect in calling this function." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "3f74489e6c9f3186aac30ebd738cd729", "grade": false, "grade_id": "cell-f4c135a24d6f1851", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# YOUR CODE HERE\n", "raise NotImplementedError()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "LoD = [{'year': 2014, 'sex': 'Female', 'name': 'Emma', 'count': 20936}, \n", " {'year': 2014, 'sex': 'Male', 'name': 'Noah', 'count': 19305}, \n", " {'year': 2015, 'sex': 'Female', 'name': 'Emma', 'count': 20455}, \n", " {'year': 2015, 'sex': 'Male', 'name': 'Noah', 'count': 19635}, \n", " {'year': 2016, 'sex': 'Female', 'name': 'Emma', 'count': 19496}, \n", " {'year': 2016, 'sex': 'Male', 'name': 'Noah', 'count': 19117}, \n", " {'year': 2017, 'sex': 'Female', 'name': 'Emma', 'count': 19800}, \n", " {'year': 2017, 'sex': 'Male', 'name': 'Liam', 'count': 18798}, \n", " {'year': 2018, 'sex': 'Female', 'name': 'Emma', 'count': 18688}, \n", " {'year': 2018, 'sex': 'Male', 'name': 'Liam', 'count': 19837}]\n", "dropColumnLoD(LoD, 'sex')\n", "LoD" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "56fd72f2531a5e2252b033f8ac96b27a", "grade": true, "grade_id": "cell-0fbea25dc33f6bdc", "locked": true, "points": 3, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "LoD = [{'year': 2014, 'sex': 'Female', 'name': 'Emma', 'count': 20936}, \n", " {'year': 2014, 'sex': 'Male', 'name': 'Noah', 'count': 19305}, \n", " {'year': 2015, 'sex': 'Female', 'name': 'Emma', 'count': 20455}, \n", " {'year': 2015, 'sex': 'Male', 'name': 'Noah', 'count': 19635}, \n", " {'year': 2016, 'sex': 'Female', 'name': 'Emma', 'count': 19496}, \n", " {'year': 2016, 'sex': 'Male', 'name': 'Noah', 'count': 19117}, \n", " {'year': 2017, 'sex': 'Female', 'name': 'Emma', 'count': 19800}, \n", " {'year': 2017, 'sex': 'Male', 'name': 'Liam', 'count': 18798}, \n", " {'year': 2018, 'sex': 'Female', 'name': 'Emma', 'count': 18688}, \n", " {'year': 2018, 'sex': 'Male', 'name': 'Liam', 'count': 19837}]\n", "\n", "dropColumnLoD(LoD, 'count')\n", "assert len(LoD) == 10\n", "assert isinstance(LoD[0], dict)\n", "assert len(LoD[0]) == 3\n", "assert 'count' not in LoD[0]" ] } ], "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.7.6" } }, "nbformat": 4, "nbformat_minor": 4 }