Advertisement
dvdjaco

9.4

Feb 18th, 2012
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.72 KB | None | 0 0
  1. # #!/usr/bin/python
  2. #
  3. # By dvdjaco
  4. # Exercise 9.4
  5. #
  6.  
  7. f = raw_input('Enter a file name: ')
  8.  
  9. try:
  10.     fhand = open(f)
  11. except IOError, err :
  12.         print "Error opening file", f
  13.         print "Error was:", err
  14. d = dict()
  15.  
  16. # build a dictionary with all email addresses and count the number of messages
  17. # sent from each one
  18. for line in fhand:
  19.     words = line.split()
  20.     if len(words) == 0 or words[0] != 'From' or words[1].find('@') == -1: continue
  21.     d[words[1]] = d.get(words[1],0) + 1
  22.  
  23. # find who sent max messages
  24. maxmessages = None
  25. for sender in d:
  26.     if maxmessages is None or d[sender] > maxmessages:
  27.         maxmessages = d[sender]
  28.         maxsender = sender
  29.  
  30. print d
  31. print maxsender, "has sent", maxmessages, "messages"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement