Advertisement
oldmagi

Chapter 9

Aug 25th, 2012
705
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.91 KB | None | 0 0
  1. #Exercise 9.1 Write a function that reads the words in words.txt and stores them as keys in a dictionary. It doesn’t matter what the values are. Then you can use the in operator as a fast way to check whether a string is in the dictionary.
  2.  
  3. def dict_9_1(e):
  4.     a=open('words.txt')
  5.     i=1
  6.     for line in a:
  7.         b=line.split()
  8.         for c in b:
  9.             e[c]=i
  10.             i=i+1
  11.        
  12. dict_=dict()
  13. dict_9_1(dict_)
  14. h=input('Check whether the word is in the dictionary:')
  15. print(h in dict_)
  16. ----------------------------------------------------------------------------------------------
  17.  
  18. #Exercise 9.2 Dictionaries have a method called get that takes a key and a default value. If the key appears in the dictionary, get returns the corresponding value; otherwise it returns the default value. For example:
  19. #>>> h = histogram('a')
  20. #>>> print h
  21. #{'a': 1}
  22. #>>> h.get('a', 0)
  23. #1
  24. #>>> h.get('b', 0)
  25. #0
  26. #Use get to write histogram more concisely. You should be able to eliminate the if statement.
  27.  
  28. def histogram(s):
  29.     d=dict()
  30.     i=1
  31.     for c in s:
  32.         d[c]=d.get(c,0)+1
  33.     return d  
  34.        
  35.  
  36. h=histogram('enternode')
  37. print(h)
  38. ----------------------------------------------------------------------------------------------
  39.  
  40. #Exercise 9.3 Write a program that categorizes each mail message by which day of the week the commit was done. To do this look for lines which start with “From”, then look for the third word and then keep a running count of each of the days of the week. At the end of the program print out the contents of your dictionary (order does not matter).
  41.  
  42. def mail_categ(b):
  43.     c=open(b)
  44.     di=dict()
  45.     for line in c:
  46.         words=line.split()
  47.         if words[0]!='From':continue
  48.         #print(words[2])
  49.         e=words[2]
  50.         di[e]=di.get(e,0)+1
  51.     print(di)
  52.  
  53. while True:
  54.     d=input('Enter a file name:')
  55.     if d=='mbox-short.txt':
  56.         mail_categ(d)
  57.         break
  58.     else:
  59.         print(" Enter correct file name:")    
  60. ----------------------------------------------------------------------------------------------
  61.  
  62. #Exercise 9.4 Write a program to read through a mail log, and figure out who had the most messages in the file. The program looks for “From” lines and takes the second parameter on those lines as the person who sent the mail.
  63.  
  64. #The program creates a Python dictionary that maps the sender’s address to the total number of messages for that person.
  65.  
  66. #After all the data has been read the program looks through the dictionary using a maximum loop (see Section 5.7.2) to find who has the most messages and how many messages the person has.
  67.  
  68. def sent_mail(c):
  69.     d=dict()
  70.     j=0
  71.     for line in c:
  72.         words=line.split()
  73.         if words[0]!='From':continue
  74.         e=words[1]
  75.         d[e]=d.get(e,0)+1
  76.         i=d[e]
  77.         if i>j and i!=j:
  78.             a=e
  79.             j=i
  80.             large=j  
  81.     print(a,large)
  82.    
  83.  
  84. while True:
  85.     a=input('Enter a file name:')
  86.     if a=='mbox-short.txt':
  87.         b=open(a)
  88.         sent_mail(b)
  89.         break
  90.     else:
  91.         print('Incorrect file name')
  92.        
  93.  
  94. ----------------------------------------------------------------------------------------------
  95.  
  96. #Exercise 9.5 This program records the domain name (instead of the address) where the message was sent from instead of who the mail came from (i.e. the whole e-mail address). At the end of the program print out the contents of your dictionary.
  97.  
  98. def mail(c):
  99.     dic_dom=dict()
  100.     for line in c:
  101.         words=line.split()
  102.         if words[0]!='From':continue
  103.         word=str(words[1])
  104.         w1=word.split('@')
  105.         #print(w1)
  106.         w2=w1[1]
  107.         dic_dom[w2]=dic_dom.get(w2,0)+1
  108.     print(dic_dom)
  109.    
  110.        
  111. while True:
  112.     a=input('Enter a file name:')
  113.     if a=='mbox-short.txt':
  114.         b=open(a)
  115.         mail(b)
  116.         break
  117.     else:
  118.         print('Incorrect file name')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement