Advertisement
oldmagi

Chapter 10

Aug 30th, 2012
565
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.53 KB | None | 0 0
  1. #Exercise 10.1 Revise a previous program as follows: Read and parse the “From” lines and pull out the addresses from the line. Count the number of messages from each person using a dictionary.
  2. #After all the data has been read print the person with the most commits by creating a list of (count, email) tuples from the dictionary and then sorting the list in reverse order and print out the person who has the most commits.
  3.  
  4. while True:
  5.     a=input('Enter a file name')
  6.     if a=='mbox-short.txt':
  7.         b=open('mbox-short.txt')
  8.         l=list()
  9.         d=dict()
  10.         for line in b:
  11.             words=line.split()
  12.             if words[0]!='From':continue
  13.             c=words[1]
  14.             d[c]=d.get(c,0)+1
  15.         for k,v in d.items():
  16.             l.append((v,k))
  17.         l.sort(reverse=True)
  18.         print(max(l))
  19.         break
  20.     else:
  21.         print("Invalid input")
  22.        
  23. ------------------------------------------------------------------------------------------------
  24. #Exercise 10.2 This program counts the distribution of the hour of the day for each of the messages. You can pull the hour from the “From” line by finding the time string and then splitting that string into parts using the colon character. Once you have accumulated the counts for each hour, print out the counts, one per line, sorted by hour as shown below.
  25.  
  26. while True:
  27.     fin=input('Enter a file name:')    
  28.     if fin=='mbox-short.txt':
  29.         a=open('mbox-short.txt')
  30.         d=dict()
  31.         o=list()
  32.         for line in a:
  33.             words=line.split()
  34.             if words[0]!='From':continue
  35.             c=(words[5].split(':'))
  36.             i=c[0]
  37.             d[i]=d.get(i,0)+1
  38.         for a,b in d.items():
  39.             o.append((a,b))
  40.         o.sort()
  41.         for a,b in o:
  42.             print(a,b)
  43.         break
  44.     else:
  45.         print("Enter correct file name")
  46.  
  47. ------------------------------------------------------------------------------------------------
  48. #Exercise 10.3 Write a function called most_frequent that takes a string and prints the letters in decreasing order of frequency. Find text samples from several different languages and see how letter frequency varies between languages. Compare your results with the tables at wikipedia.org/wiki/Letter_frequencies.
  49.  
  50. def most_frequent(st):
  51.     d=dict()
  52.     l=list()
  53.     for s in st:
  54.         d[s]=d.get(s,0)+1
  55.     for key,val in d.items():
  56.         l.append((val,key))
  57.     l.sort(reverse=True)
  58.     for key,val in l:
  59.         print(key,val)
  60.  
  61. most_frequent("catalyst")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement