Advertisement
Guest User

Untitled

a guest
May 9th, 2012
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.15 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. # P2PU - Python Programming - Chapter 5 - Datatypes
  4. # Chapter 10 - Tuples
  5. print 'Python for Informatics - Chapter 10: Tuples'
  6.  
  7. # Exercise 10.1
  8. print '\n# Exercise 10.1\n'
  9. fname =  raw_input('Enter file name: ')
  10. try:
  11.     ffile = open(fname)
  12. except:
  13.     print 'File', fname, 'cannot be opened!'
  14.     exit()
  15. mails = dict()
  16. for line in ffile:
  17.     words = line.split()
  18.     if len(words) < 2 or words[0] != 'From': continue
  19.     mails[words[1]] = mails.get(words[1], 0) + 1
  20. lst = list()
  21. for key, val in mails.items():
  22.    lst.append((val,key))
  23. lst.sort(reverse=True)
  24. print lst[0][1], ':', lst[0][0]
  25.  
  26. # Exercise 10.2
  27. print '\n# Exercise 10.2\n'
  28. fname =  raw_input('Enter file name: ')
  29. try:
  30.     ffile = open(fname)
  31. except:
  32.     print 'File', fname, 'cannot be opened!'
  33.     exit()
  34. hcount = dict()
  35. for line in ffile:
  36.     words = line.split()
  37.     if len(words) < 6 or words[0] != 'From': continue
  38.     h = words[5].split(':')
  39.     hcount[h[0]] = hcount.get(h[0], 0) + 1
  40. lst = list()
  41. for key, val in hcount.items():
  42.    lst.append((key,val))
  43. lst.sort()
  44. for key, val in lst:
  45.    print key, val
  46.  
  47. # Exercise 10.3
  48. import string
  49. print '\n# Exercise 10.3\n'
  50. def most_frequent(f):
  51.    #small change: function takes a file instead of a string
  52.    letters = dict()
  53.    for line in ffile:
  54.       # remove puntuation
  55.       line = line.translate(None, string.punctuation)
  56.       # remove spaces
  57.       line = line.translate(None, ' ')
  58.       # all lowercase
  59.       line = line.lower()
  60.       # remove numbers
  61.       line = line.translate(None, '0123456789')
  62.       # remove newlines
  63.       line = line.strip()
  64.       for letter in line:
  65.          letters[letter] = letters.get(letter, 0) + 1
  66.    lst = list()
  67.    for key, val in letters.items():
  68.       lst.append((val,key))
  69.    lst.sort(reverse=True)
  70.    return lst
  71.  
  72. fname =  raw_input('Enter file name: ')
  73. try:
  74.     ffile = open(fname)
  75. except:
  76.     print 'File', fname, 'cannot be opened!'
  77.     exit()
  78. res = most_frequent(ffile)
  79. for key, val in res[0:10]:
  80.    print key, val
  81.  
  82. # Top 10 letters from newspaper article in
  83. #   English: e t a n s o i r h l
  84. #   French:  e s a n i t l r u o
  85. #   Dutch:   e n t a i r d l o s
  86. #   German:  e n a r i s t h u l
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement