-
Notifications
You must be signed in to change notification settings - Fork 0
/
Week 9-10 Exercises
54 lines (50 loc) · 2.19 KB
/
Week 9-10 Exercises
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
9.4 Write a program to read through the mbox-short.txt and figure out who has the sent the greatest number of mail messages.
The program looks for 'From ' lines and takes the second word of those lines as the person who sent the mail.
The program creates a Python dictionary that maps the sender's mail address to a count of the number of times they appear
in the file. After the dictionary is produced, the program reads through the dictionary using a maximum loop to find the
most prolific committer.
name = raw_input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)
maxsend = dict()
for line in handle:
if not line.startswith("From:"): continue
line=line.rstrip()
fields = line.split(':')
sndr = fields[1]
sndr = sndr.strip()
if sndr not in maxsend:
maxsend[sndr] = 1
else:
maxsend[sndr] = maxsend[sndr] + 1
maxmailuser = "Shirish"
maxmails = -100
for maxsndr in maxsend:
if maxsend[maxsndr] > maxmails:
maxmailuser = maxsndr
maxmails = maxsend[maxsndr]
else: continue
print maxmailuser,maxsend[maxmailuser]
10.2 Write a program to read through the mbox-short.txt and figure out the distribution by hour of the day for each of the messages. You can pull the hour out from the 'From ' line by finding the time and then splitting the string a second time using a colon.
From [email protected] Sat Jan 5 09:14:16 2008. Once you have accumulated the counts for each hour, print out the counts, sorted by hour as shown below. Note that the autograder does not have support for the sorted() function.
name = raw_input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)
hrcnt = dict()
for line in handle:
if not line.startswith("From"): continue
fields = line.split()
if len(fields) == 7:
tmstr=fields[5]
hrstr=tmstr.split(":")
hr=hrstr[0]
if hr not in hrcnt:
hrcnt[hr] = 1
else:
hrcnt[hr] = hrcnt[hr] + 1
lst = list()
for key,val in hrcnt.items():
lst.append( (key,val) )
lst.sort()
for key,val in lst:
print key,val