-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchapter8_exercise.py
More file actions
84 lines (74 loc) · 2.43 KB
/
chapter8_exercise.py
File metadata and controls
84 lines (74 loc) · 2.43 KB
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
'''
Exercise 8.4
Open the file romeo.txt and read it line by line.
For each line, split the line into a list of words using the split() method.
The program should build a list of words.
For each word on each line check to see if the word is already in the list and if not append it to the list.
When the program completes, sort and print the resulting words in alphabetical order.
'''
print("<Exercise 8.4>")
fname = input("Enter file name: ") #romeo.txt
fhandle = open(fname)
newList = list()
for line in fhandle :
line2 = line.rstrip()
wordsList = line2.split()
for word in wordsList :
if word in newList :
continue
else :
newList.append(word)
newList.sort()
print(newList)
print()
'''
Exercise 8.5
Open the file mbox-short2.txt and read it line by line.
When you find a line that starts with 'From ' like the following line:
From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008
You will parse the From line using split() and print out the second word in the line
(i.e. the entire address of the person who sent the message).
Then print out a count at the end.
Hint: make sure not to include the lines that start with 'From:'.
'''
print("<Exercise 8.5>")
fname = input("Enter file name: ")
if len(fname) < 1 : fname = "mbox-short2.txt"
fh = open(fname)
lst = list()
count = 0
for line in fh:
wordsInLine = line.rstrip().split()
if len(wordsInLine) < 3 or wordsInLine[0] != 'From':
continue
print(wordsInLine[1])
count = count + 1
print("There were", count, "lines in the file with From as the first word")
'''
Exercise 8.6:
Rewrite the program that prompts the user for a list of numbers and
prints out the maximum and minimum of the numbers at the end when the user enters “done”.
Write the program to store the numbers the user enters in a list
and use the max() and min() functions to compute the maximum and minimum numbers after the loop completes.
Enter a number: 6
Enter a number: 2
Enter a number: 9
Enter a number: 3
Enter a number: 5
Enter a number: done
Maximum: 9.0
Minimum: 2.0
'''
print("<Exercise 8.6>")
numberList = list()
while True:
num = input("Enter a number: ")
if(num == 'done'):
break
try:
number = float(num)
numberList.append(num)
except:
print('Invalid Value')
print('Maximum:', max(numberList))
print('Maximum:', min(numberList))