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".
import os
import os.path
import io
import sys
from contextlib import redirect_stdout
datadir = "./publicdata"
raise NotImplementedError()
with your solution code.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).
# Solution cell
# YOUR CODE HERE
raise NotImplementedError()
# Testing cell
list1 = myFileList(".")
assert len(list1) == 2
assert "listbasic.ipynb" in list1
assert "filebasic.ipynb" in list1
# 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.
# Solution cell
# YOUR CODE HERE
raise NotImplementedError()
print("Tennyson Word Count:", tennysonWordCount())
# Testing cell
assert tennysonWordCount() == 20