Advertisement
oldmagi

chapter 8

Aug 22nd, 2012
740
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.02 KB | None | 0 0
  1. #chapter 8
  2. #Exercise 8.1 Write a function called chop that takes a list and modifies it, removing the first and last elements, and returns None.
  3. #Then write a function called middle that takes a list and returns a new list that contains all but the first and last elements.
  4.  
  5. def chop(_t):
  6.     del _t[0]
  7.     return _t.remove(t[(len(t)-1)])
  8.    
  9.  
  10. def middle(_t2):
  11.     n_l=_t2[0:4]
  12.     return n_l
  13.  
  14. t=['a','b','c','4','5']
  15. print(chop(t))
  16. print(middle(t))
  17. ----------------------------------------------------------------------------------------------
  18.  
  19. #Exercise 8.2 Figure out which line of the above program is still not properly guarded.See if you can construct a text file which causes the program to fail and then modify the program so that the line is properly guarded and test it to make sure it handles your new text file.
  20.  
  21.  
  22. #the guardian code---- if len(words)==0: continue ---- fails if it finds a new empty line so we have to change it to '< 3' instead of '=='. you may have a doubt why not '< 1' or '< 2',since we are going to print(words[2])--> only third value in the list, we are not going to consider the list with two values. Thats why we are using '< 3'
  23.  
  24.  
  25. #mbox.txt contains
  26. #From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008
  27. #From Fri Jan 5 09:14:16 2008
  28. fhand = open('mbox-short1.txt')
  29. m='Mon Tue Wed Thu Fri Sat Sun'
  30. count=0
  31. for line in fhand:
  32.     words=line.split()
  33.     for i in range(len(words)):
  34.         if words[i] in m[:]:
  35.             print(words[i])
  36. ----------------------------------------------------------------------------------------------
  37.  
  38. #Exercise 8.3 Rewrite the guardian code in the above example without two if statements. Instead use a compound logical expression using the and logical operator with a single if statement.
  39.  
  40. #mbox-short.txt
  41. ##From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008
  42.  
  43. fhand = open('mbox-short.txt')
  44. for line in fhand:
  45.     words = line.split()
  46.     if len(words)<3 or words[0] != 'From' :continue
  47.     print(words[2])
  48. ----------------------------------------------------------------------------------------------
  49.  
  50. #Exercise 8.4 Download a copy of the file from www.py4inf.com/code/romeo.txt
  51. #Write a program to open the file romeo.txt and read it line by line. For each line, split the line into a list of words using the split function.
  52.  
  53. a=input('Enter file:')
  54. b=open(a)
  55. wordli=[]
  56. for line in b:
  57.     words=line.split()
  58.     for word in words:
  59.         if word not in wordli:
  60.            wordli.append(word)
  61. wordli.sort()
  62. print(wordli)
  63. ----------------------------------------------------------------------------------------------
  64.  
  65. #Exercise 8.5 Write a program to read through the mail box data and when you find line that starts with “From”, you will split the line into words using the split function. We are interested in who sent the message which is the second word on the From line.
  66. #From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008
  67. #You will parse the From line and print out the second word for each From line and then you will also count the number of From (not From:) lines and print out a count at the end.
  68.  
  69. b=input('Enter a filename:')
  70. a=open(b)
  71. count=0
  72. for line in a:
  73.     if not line.startswith('From'):continue
  74.     words=line.split()
  75.     print(words[1])
  76.     count+=1
  77. c='There were %d lines in the file with From as the first word' % count
  78. print(c)
  79. ----------------------------------------------------------------------------------------------
  80.  
  81. #Exercise 8.6 Rewrite the program that prompts the user for a list of numbers and prints out the maximum and minimum of the numbers at the end when the user enters “done”. Write the program to store the numbers the user enters in a list and use the max() and min() fuctions to compute the maximum and minimum numbers after the loop completes.
  82.  
  83. a=list()
  84. while True:
  85.     b=input("Enter a number:")
  86.     if b=='done':break
  87.     try:
  88.         c=float(b)
  89.         a.append(c)
  90.     except:
  91.         print("invalid input")
  92. print('Maximum:',format(max(a),'.12g'))
  93. print('Minimum:',format(min(a),'.12g'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement