Python for Everybody
Chapter 9
Exercise 9.1
"""
Exercise 9.1: Write a program that reads the words in words.txt and stores
them as keys in a dictionary. Download a copy of the file from
https://www.py4e.com/code3/words.txt. It doesn't matter what the values are.
Then use the 'in' operator as a fast way to check whether a string is in the
dictionary.
Python for Everybody: Exploring Data Using Python 3
by Charles R. Severance
Solution by Jamison Lahman, May 31, 2017
"""
count = 0
dictionary_words = dict() # Initializes the dictionary
fhand = open('words.txt')
for line in fhand:
words = line.split()
for word in words:
count += 1
if word in dictionary_words:
continue # Discards duplicates
dictionary_words[word] = count # Value is first time word appears
if 'Python' in dictionary_words:
print('True')
else:
print('False')
Exercise 9.2
"""
Exercise 9.2: Write a program that categorizes each mail message by which day
of the week the commit was done. To do this, look for lines that start with
"From", then look for the third word and keep a running count of each of the
days of the week. At the end of the program, print out the contents of your
dictionary (order does not matter).
Sample Line: From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008
Sample Execution:
python dow.py
Enter a file name: mbox-short.txt
{'Fri': 20, 'Thu': 6, 'Sat': 1}
Python for Everybody: Exploring Data Using Python 3
by Charles R. Severance
Solution by Jamison Lahman, May 31, 2017
"""
dictionary_days = dict() # Initializes the dictionary
fname = input('Enter a file name: ')
try:
fhand = open(fname)
except FileNotFoundError:
print('File cannot be opened:', fname)
exit()
for line in fhand:
words = line.split()
if len(words) < 3 or words[0] != 'From':
continue
else:
if words[2] not in dictionary_days:
dictionary_days[words[2]] = 1 # First entry
else:
dictionary_days[words[2]] += 1 # Additional counts
print(dictionary_days)
Exercise 9.3
"""
Exercise 9.3: Write a program to read through a mail log, build a histogram
using a dictionary to count how many messages have come from each email
address, and print the dictionary.
Enter file name: mbox-short.txt
{'stephen.marquard@uct.ac.za': 2, 'louis@media.berkeley.edu': 3,
'zqian@umich.edu': 4, 'rjlowe@iupui.edu': 2, 'cwen@iupui.edu': 5,
'gsilver@umich.edu': 3, 'wagnermr@iupui.edu': 1,
'antranig@caret.cam.ac.uk': 1, 'gopal.ramasammycook@gmail.com': 1,
'david.horwitz@uct.ac.za': 4, 'ray@media.berkeley.edu': 1}
Python for Everybody: Exploring Data Using Python 3
by Charles R. Severance
Solution by Jamison Lahman, May 31, 2017
"""
dictionary_addresses = dict() # Initializes the dictionary
fname = input('Enter file name: ')
try:
fhand = open(fname)
except FileNotFoundError:
print('File cannot be opened:', fname)
exit()
for line in fhand:
words = line.split()
if len(words) < 2 or words[0] != 'From':
continue
else:
if words[1] not in dictionary_addresses:
dictionary_addresses[words[1]] = 1 # First entry
else:
dictionary_addresses[words[1]] += 1 # Additional counts
print(dictionary_addresses)
Exercise 9.4
"""
Exercise 9.4: Add ccode to the above program to figure out who has the most
mesasges in the file.
After all the data has been read and the dictionary has been created, look
through the dictionary using a maximum loop (see Section [maximumloop]) to
find who has the most messages and print how many messages the person has.
Enter a file name: mbox-short.txt
cwen@iupui.ed 5
Enter a file name: mbox.txt
zqian@umich.edu 195
Python for Everybody: Exploring Data Using Python 3
by Charles R. Severance
Solution by Jamison Lahman, May 31, 2017
"""
dictionary_addresses = dict() # Initialize variables
maximum = 0
maximum_address = ''
fname = input('Enter file name: ')
try:
fhand = open(fname)
except FileNotFoundError:
print('File cannot be opened:', fname)
quit()
for line in fhand:
words = line.split()
if len(words) < 2 or words[0] != 'From':
continue
if words[1] not in dictionary_addresses:
dictionary_addresses[words[1]] = 1 # First entry
else:
dictionary_addresses[words[1]] += 1 # Additional counts
for address in dictionary_addresses:
if dictionary_addresses[address] > maximum: # Checks if new maximum
# Update the maximum if needed
maximum = dictionary_addresses[address]
# Stors the address of maximum
maximum_address = address
print(maximum_address, maximum)
Exercise 9.5
"""
Exercise 9.5: This program records the domain name (instead of the address)
where the message was sent from instead of who the mail came from (i.e., the
whole email address). At the end of the program, print out the contents of
your dictionary.
python schoolcount.py
Enter a file name: mbox-short.txt
['media.berkeley.edu': 4, 'uct.ac.za': 6, 'umich.edu': 7, 'gmail.com': 1,
'caret.cam.ac.uk': 1, 'iupui.edu': 8}
Python for Everybody: Exploring Data Using Python 3
by Charles R. Severance
Solution by Jamison Lahman, May 31, 2017
"""
dictionary_domains = dict() # Initialize variables
fname = input('Enter file name: ')
try:
fhand = open(fname)
except FileNotFoundError:
print('File cannot be opened:', fname)
quit()
for line in fhand:
words = line.split()
if len(words) < 2 or words[0] != 'From':
continue
else:
atpos = words[1].find('@') # Position of '@'
domain = words[1][atpos+1:] # Store characters after '@'
if domain not in dictionary_domains:
dictionary_domains[domain] = 1 # First entry
else:
dictionary_domains[domain] += 1 # Additional counts
print(dictionary_domains)