{ "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", "import io\n", "import sys\n", "\n", "datadir = \"publicdata\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q1** Write a function\n", "\n", " makeDict(L)\n", " \n", "that creates a dictionary whose keys are the items of `L` and whose values are the corresponding *indices* of `L`. You may assume `L` is a list without any duplicate items." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "7bcbc0e9d91167e91810d36c07e77edc", "grade": false, "grade_id": "cell-1b48e481c8c4c6cc", "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": "45028805a71e76940ea7fb76719f053c", "grade": true, "grade_id": "cell-7a90d00c4694cb19", "locked": true, "points": 2, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "# Testing cell\n", "\n", "L1 = [5, 4, 3, 2, 1]\n", "expect1 = {5: 0, 4: 1, 3: 2, 2: 3, 1:4}\n", "assert makeDict(L1) == expect1\n", "L2 = \"Piotr Piper Picked a Pack of Pickled Peppers\".split()\n", "expect2 = {'Piotr': 0, 'Piper': 1, 'Picked': 2, \"a\": 3, \"Pack\":4, \"of\": 5, \"Pickled\": 6, \"Peppers\": 7}\n", "assert makeDict(L2) == expect2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q2** Write a function\n", "\n", " filmography(L)\n", "\n", "whose parameter `L` is a list of tuples of the form `(actor, movie)`, and creates and returns a dictionary where the keys are actor names and the values are lists of movies that actor has been in. For example, given \n", "\n", " L = [(‘DiCaprio’,‘Revenant’),\n", " (‘Cumberbatch’,‘Hobbit’),\n", " (‘DiCaprio’,‘Gatsby’),\n", " (‘DiCaprio’,‘Django’)]\n", "\n", "then you should return \n", "\n", " {‘DiCaprio’:[‘Revenant’,‘Gatsby’,‘Django’],\n", " ‘Cumberbatch’:[‘Hobbit’]}" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "c4c20a48af5f5bf907601c503662434c", "grade": false, "grade_id": "cell-6b2af6f816026d9f", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# YOUR CODE HERE\n", "raise NotImplementedError()\n", "\n", "L = [('DiCaprio','Revenant'),\n", " ('Cumberbatch','Hobbit'),\n", " ('DiCaprio','Gatsby'),\n", " ('DiCaprio','Django')]\n", "D = filmography(L)\n", "print(D)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "351e3fbca745e78446875a00a19c795c", "grade": true, "grade_id": "cell-687b64ee889a4671", "locked": true, "points": 2, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "L = [('DiCaprio','Revenant'),\n", " ('Cumberbatch','Hobbit'),\n", " ('DiCaprio','Gatsby'),\n", " ('DiCaprio','Django')]\n", "D = filmography(L)\n", "assert len(D) == 2\n", "assert 'DiCaprio' in D.keys()\n", "assert 'Django' in D['DiCaprio']\n", "assert 'Cumberbatch' in D.keys()\n", "assert D['Cumberbatch'] == ['Hobbit']" ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "markdown", "checksum": "7851726aa83bad6ee8275f51d4c99638", "grade": false, "grade_id": "cell-31710d7246cdc088", "locked": true, "schema_version": 3, "solution": false } }, "source": [ "**Q1** Write a function\n", "\n", " readNamesCounts(path)\n", "\n", "that reads from the file at location `path` and returns a dictionary with the information from the file. The format of the file is a *CSV file*. The first line is a comma separated set of string names for the columns contained in the file. In this case `year,name,count`. Subsequent lines have, on each line, comma separated values for a data mapping, where the first value gives the independent variable value of the year, and the second and third values on the line give the string name and the integer count.\n", "\n", "The returned dictionary should map from years to two-tuples consisting of a name and an integer count. Make sure your year keys are integers. Assign to variable `female` the dictionary obtained from `topfemale.csv` and assign to variable `male` the dictionary obtained from `topmale.csv`. Both files may be found in the `datadir` directory." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "e18aae5acd5857fdb30e0af8739b6adc", "grade": false, "grade_id": "cell-6ff7c2a5d023bb5c", "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": "a1593acd649489e54bc9c8b9bddfebd9", "grade": true, "grade_id": "cell-f15ce7f7239dcd48", "locked": true, "points": 2, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "# Testing cell\n", "\n", "femalepath = os.path.join(datadir, \"topfemale.csv\")\n", "females = readNamesCounts(femalepath)\n", "assert len(females) == 139\n", "assert 1880 in females\n", "assert 2018 in females\n", "assert females[1880] == ('Mary', 7065)\n", "assert females[2018] == ('Emma', 18688)\n", "\n", "malepath = os.path.join(datadir, \"topmale.csv\")\n", "males = readNamesCounts(malepath)\n", "assert len(females) == 139\n", "assert 1880 in females\n", "assert 2018 in females\n", "assert males[1880] == ('John', 9655)\n", "assert males[2018] == ('Liam', 19837)" ] } ], "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 }