Advertisement
Guest User

Untitled

a guest
Jul 10th, 2012
2,584
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.57 KB | None | 0 0
  1. '''
  2. Exercise 9.1 Write a function that reads the words in words.txt and stores them as keys in a
  3. 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.
  4. '''
  5. f1=open('words.txt')
  6. d1=dict()
  7. i=0
  8. for line in f1:
  9.     words=line.split()
  10.     for i in range(len(words)):
  11.         d1[words[i]]=i
  12. print d1
  13. print 'abc' in d1
  14.  
  15. '''
  16. Exercise 9.2 Dictionaries have a method called get that takes a key and a default value. If
  17. the key appears in the dictionary, get returns the corresponding value; otherwise it returns the
  18. default value.
  19. Use get to write histogram more concisely. You should be able to eliminate the if statement.
  20. '''
  21. def histogram(s):
  22.     d = dict()
  23.     for c in s:
  24.         d[c]=d.get(c,0)+1
  25.     return d
  26. print histogram('abbcccdddd')
  27.  
  28. '''
  29. Exercise 9.3 Write a program that categorizes each mail message by which day of the week
  30. the commit was done. To do this look for lines which start with “From”, then look for the third
  31. word and then keep a running count of each of the days of the week. At the end of the program
  32. print out the contents of your dictionary (order does not matter).
  33. '''
  34. f1=open('mail.txt')
  35. d1=dict()
  36. for line in f1:
  37.     words=line.split()
  38.     if words[0]=='From':
  39.         d1[words[2]]=d1.get(words[2],0)+1
  40.     else:
  41.         continue
  42. print d1
  43.  
  44. '''
  45. 9.4 Write a program to read through a mail log, and figure out who had the most
  46. messages in the file. The program looks for “From” lines and takes the second parameter on
  47. those lines as the person who sent the mail.
  48. The program creates a Python dictionary that maps the sender’s address to the total number of
  49. messages for that person.
  50. After all the data has been read the program looks through the dictionary using a maximum loop
  51. (see Section 5.7.2) to find who has the most messages and how many messages the person has.
  52. '''
  53. f1=open('mail.txt')
  54. d1=dict()
  55. for line in f1:
  56.     words=line.split()
  57.     if words[0]=='From':
  58.         d1[words[1]]=d1.get(words[1],0)+1
  59.     else:
  60.         continue
  61. print d1
  62.  
  63. '''
  64. Exercise 9.5 This program records the domain name (instead of the address) where the message
  65. was sent from instead of who the mail came from (i.e. the whole e-mail address). At the end of
  66. the program print out the contents of your dictionary.
  67. '''
  68. f1=open('mail.txt')
  69. d1=dict()
  70. for line in f1:
  71.     words=line.split()
  72.     if words[0]=='From':
  73.         a=line.find('@')
  74.         b=line.find(' ',a);
  75.         c=line[a+1:b]
  76.         d1[c]=d1.get(c,0)+1
  77.     else:
  78.         continue
  79. print d1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement