{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Before you turn this problem in, make sure everything runs as expected. First, **restart the kernel** (in the menubar, select Kernel$\\rightarrow$Restart) and then **run all cells** (in the menubar, select Cell$\\rightarrow$Run All).\n", "\n", "Make sure you fill in any place that says `YOUR CODE HERE` or \"YOUR ANSWER HERE\", as well as your name and collaborators below:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "NAME = \"\"\n", "COLLABORATORS = \"\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "import os.path\n", "\n", "datadir = \"publicdata\"\n", "\n", "topnamesDoL = {'year': [2018, 2018, 2017, 2017, 2016, 2016],\n", " 'sex': ['Male', 'Female', 'Male',\n", " 'Female', 'Male', 'Female'],\n", " 'name': ['Liam', 'Emma', 'Liam', 'Emma',\n", " 'Noah', 'Emma'],\n", " 'count': [19837, 18688, 18798, 19800, 19117, 19496]}\n", "\n", "topnamesLoL = [[2018, 'Male', 'Liam', 19837],\n", " [2018, 'Female', 'Emma', 18688],\n", " [2017, 'Male', 'Liam', 18798],\n", " [2017, 'Female', 'Emma', 19800],\n", " [2016, 'Male', 'Noah', 19117],\n", " [2016, 'Female', 'Emma', 19496]]\n", "columns = ['year', 'sex', 'name', 'count']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q1:** Write a function\n", "\n", " convertDoL2LoL(D)\n", "\n", "that converts from a dictionary of lists representation in `D` to the equivalent list of lists representation, returning both the list of column names, as given by the keys of `D`, as well as a list of lists storing the data in the values of `D`. Note that, because we do not know the order of mappings in a dictionary, the order of fields in the column names, and the order of fields in the rows of the data set may not be apparent. But as long as all rows as well as the list of column names is *consistent*, the conversion is valid." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "eb8d51af09eaf5dbb133cdf299f99b4d", "grade": true, "grade_id": "cell-8e9794bb531ab1ca", "locked": false, "points": 2, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# Solution cell\n", "\n", "# YOUR CODE HERE\n", "raise NotImplementedError()\n", " \n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Testing cell\n", "\n", "convertDoL2LoL(topnamesDoL)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q (TB):** Write a function\n", "\n", " convertLoL2DoL(columns, data)\n", " \n", "that converts from a list of lists to a dictionary of lists `D`, which you return. Here, `columns` is a list of column names (which will become the keys in `D`), and `data` is a list of row lists. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "6e7e79c05f71da02a854d6ce22b29e50", "grade": true, "grade_id": "cell-80ac6dbe3eaeb4de", "locked": false, "points": 2, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# Solution cell\n", "\n", "# YOUR CODE HERE\n", "raise NotImplementedError()\n", " \n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Testing cell\n", "\n", "convertLoL2DoL(columns, topnamesLoL)" ] } ], "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 }