-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathxtra_p3.py
105 lines (73 loc) · 2.77 KB
/
xtra_p3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
"""
Optional bonus. See course site for details.
>>> len(longwordset1)
415
>>> len(longwordset2)
197
>>> len(longwords)
13
"""
import doctest
# import from local util_datafun_logger.py
from util_datafun_logger import setup_logger
# Call the setup_logger function to create a logger and get the log file name
logger, logname = setup_logger(__file__)
def compare_two_plays():
''' This function compares two plays by Shakespeare.'''
logger.info("Calling compare_two_plays()")
# read from file and get a list of words
with open("text_hamlet.txt", "r") as f1:
text = f1.read()
wordlist1 = text.split() # split on whitespace
logger.info(f"List of words from play 1: {wordlist1}")
# read from file and get a list of words
with open("text_juliuscaesar.txt", "r") as f2:
text = f2.read()
wordlist2 = text.split() # split on whitespace
logger.info(f"List of words from play 2: {wordlist2}")
# Done with files - let the files close and the work begin
# Remove duplicates by creating two sorted sets
# hint: use sorted() to sort the list
# hint: use set() to remove duplicates
# name them wordset1 and wordset2
wordset1 = set() # TODO fix this line
wordset2 = set() # TODO fix this line
# initialize a variable maxlen = 10
maxlen = 1 # TODO fix this line
# use a list comprension to get a list of words longer than 10
# for word in wordset1
# That is:
# in a list (e.g. square brackets)
# say "[Give me word (for each word in wordset1)
# if len(word) is greater than maxlen]"
# then convert the list to a set to we can take the intersection
# hint: use set()
# name them longwordset1 and longwordset2
longwordset1 = set() # TODO: fix this line
longwordset2 = set() # TODO: fix this line
# find the intersection of the two sets
# that is, the words in both longwordset1 1 & longwordset2
# name this variable longwords
longwords = longwordset1 & longwordset2
# print the length of the sets and the list
print(len(longwordset1))
print(len(longwordset2))
print(len(longwords))
print()
print(f"{sorted(longwords) = }")
print()
# check your work
print("TESTING...if nothing prints before the testing is done, you passed!")
doctest.testmod()
print("TESTING DONE")
def show_log():
"""Read log file and print it to the terminal"""
with open(logname, "r") as file_wrapper:
print(file_wrapper.read())
# -------------------------------------------------------------
# Call some functions and execute code!
if __name__ == "__main__":
logger.info("Calling functions from main block")
compare_two_plays()
logger.info("Complete the code to compare two plays.")
show_log()