Denison CS181/DA210 Homework

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).

Make sure you fill in any place that says YOUR CODE HERE or "YOUR ANSWER HERE".


In [ ]:
import os
import os.path
import io
import sys
from contextlib import redirect_stdout

datadir = "./publicdata"

Instructions

  • Complete all the problems below by replacing the two-line sequence of # YOUR CODE HERE and raise NotImplementedError() with your solution code.
  • All functions that you define for homework must have docstrings or you will lose a point per function.

Q1 Write a function

myFileList(dir)

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).

In [ ]:
# Solution cell

# YOUR CODE HERE
raise NotImplementedError()
In [ ]:
# Testing cell
list1 = myFileList(".")
assert len(list1) == 2
assert "listbasic.ipynb" in list1
assert "filebasic.ipynb" in list1
In [ ]:
# Testing cell with hidden tougher test cases

list2 = myFileList("publicdata")
assert "tennyson.txt" in list2

Q2 Write a function

tennysonWordCount()

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.

In [ ]:
# Solution cell

# YOUR CODE HERE
raise NotImplementedError()
print("Tennyson Word Count:", tennysonWordCount())
In [ ]:
# Testing cell
assert tennysonWordCount() == 20