Albion College

CS 171

Introduction to Computer Science I & Lab

Fall 2021

Lab 6: Files, lists, Dictionaries

Goals and Objectives

The main goal this lab is for you to get a more practice with Pythons's File reading, lists, and dictionaries.

Task

You will write several functions that perform some simple computations using lists.

Background

Lists and dictionaries are especially useful in processing files. Many programs to store a collection of related items in files.

Introductions

Introduce yourself to your lab partner(s). Do you have plans for fall break?

Download the lab

Download the source file Lab06.py. It contains the stubs of Python functions. Open it in the online Python environment.

Download the source file declaration.txt. It contains the text of the Declaration of Independence, one of the founding documents of the USA.

Getting Started

In this lab, you will practice writing functions that process strings. You should start off with s1 driving and s2 navigating. Complete the functions in Lab06.py. You should change driver/navigator roles after each method is completed.

Seek help as soon as you are experiencing difficulty with this assignment.
Do not wait until the deadline to seek help!

# Lab 6 # YOUR NAMES HERE def warmup(): s1 = "one two three four five" # split s1 into a list of five words # YOUR CODE HERE list1 = None print(list1) s2 = "one:two:three:four:five" # split s2 into a list of five words # YOUR CODE HERE list2 = None print(list2) s3 = "one, two, three, four, five" # split s3 into a list of five words # YOUR CODE HERE list3 = None print(list3) # trim any spaces from the start or end of each word in s3 # YOUR CODE HERE list4 = None print(list4) d1 = dict() d1['a'] = 1 d1['b'] = 3 d1['c'] = 3 d1['d'] = 2 print(d1) # create a list containing just the keys of d1 # use the keys() function # YOUR CODE HERE list5 = None print(list5) # ['a', 'b', 'c', 'd'] return None # create a list containing just the values of d1 # use the values() function # YOUR CODE HERE list6 = None print(list6) # [1, 3, 3, 5] return None # read the indicated text file and put all the words # in a single list. # remove punctuation and whitespace def readFile(fileName): return list() # return a dictionary counting words in the list wordList def counts(wordList): return dict() # return a the word that occurs most often in the # given dictionary of word:count items def mostFrequent(d): return '' # return a the average number of times words occur # total number of words / total unique words def average(d): return 0 # return a list of words that occur more than average # Use above functions def mostUsed(wordList): return list() def main(): print("warmup") warmup() print("readFile") words = readFile('declaration.txt') print(len(words)) print("wordCounts") wc = counts(words) print(len(wc)) print("mostFrequent") print(mostFrequent(wc)) print("average") print(average(wc)) print("mostUsed") common = mostUsed(words) print(common) print(len(common)) main()

Have Fun!

Final Steps

When done, email you lab partner and instructor your lab (.py file).