{ "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", "from contextlib import redirect_stdout\n", "\n", "datadir = \"./publicdata\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Instructions\n", "\n", "- Complete all the problems below by replacing the two-line sequence of # YOUR CODE HERE and `raise NotImplementedError()` with your solution code.\n", "- All functions that you define for homework **must have docstrings** or you will lose a point **per function**." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q1** Write a function\n", "\n", " myFileList(dir)\n", " \n", "that queries the specified directory, `dir`, for the files and folders contained therein and generates and returns a list with an element for each of the non-hidden **files** in the directory. This means that your list should **not** include any entries for hidden files (that start with a `.` character), nor should it include entries for directories (i.e. folders)." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "c5cc48f43ddc58d09911fd8a5cc76814", "grade": false, "grade_id": "cell-5a2690ad91df7a9f", "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": "36ed9b1c4f800d2d278ce00acf9d89ed", "grade": true, "grade_id": "cell-07f7d7e0c1519cdf", "locked": true, "points": 1, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "# Testing cell\n", "list1 = myFileList(\".\")\n", "assert len(list1) == 2\n", "assert \"listbasic.ipynb\" in list1\n", "assert \"filebasic.ipynb\" in list1" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "274123efa2df26ee9ff46bcce394a838", "grade": true, "grade_id": "cell-4379c9eebdfdaf7e", "locked": true, "points": 2, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "# Testing cell with hidden tougher test cases\n", "\n", "list2 = myFileList(\"publicdata\")\n", "assert \"tennyson.txt\" in list2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q2** Write a function\n", "\n", " tennysonWordCount()\n", "\n", "specific to the `tennyson.txt` file, located in the `publicdata` directory, that counts the number of words in the file, processing line by line. Words are defined as sequences of characters separated by spaces, tabs, and newlines. With this definition, you need not worry about apostrophes within a word, and a word, by this definition, would include any following punctuation, like a comma, period, semicolon, or exclamation mark." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "7850424dc30bf39cd6421812ca2bc0dc", "grade": false, "grade_id": "cell-8d7e4e29b52275ad", "locked": false, "schema_version": 3, "solution": true } }, "outputs": [], "source": [ "# Solution cell\n", "\n", "# YOUR CODE HERE\n", "raise NotImplementedError()\n", "print(\"Tennyson Word Count:\", tennysonWordCount())" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "021b9ab5d5703120c65b0f5084648de0", "grade": true, "grade_id": "cell-dac5326d4b855c3e", "locked": true, "points": 3, "schema_version": 3, "solution": false } }, "outputs": [], "source": [ "# Testing cell\n", "assert tennysonWordCount() == 20" ] } ], "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 }