Advertisement
Guest User

Untitled

a guest
May 9th, 2012
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.78 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. # P2PU - Python Programming - Chapter 5 - Datatypes
  4. # Chapter 8 - Lists
  5. print 'Python for Informatics - Chapter 8: Lists'
  6.  
  7. # Exercise 8.1
  8. print '\n# Exercise 8.1\n'
  9. def chop(t):
  10.     del t[0]
  11.     del t[len(t)-1]
  12.     return None
  13. def middle(t):
  14.     return t[1:len(t)-1]
  15. letters = ['a', 'b', 'c', 'd', 'e', 'f']
  16. print 'list before function chop:', letters
  17. chop(letters)
  18. print 'list after function chop :', letters
  19. print
  20. letters = ['a', 'b', 'c', 'd', 'e', 'f']
  21. print 'original list before function middle:', letters
  22. nletters = middle(letters)
  23. print 'original list after function middle :', letters
  24. print 'new list after function middle      :', nletters
  25.  
  26.  
  27. # Exercise 8.2
  28. inp = raw_input('Press <enter> to continue')
  29. print '\n# Exercise 8.2\n'
  30. fname =  'mbox.txt'
  31. try:
  32.     ffile = open(fname)
  33. except:
  34.     print 'File', fname, 'cannot be opened!'
  35.     exit()
  36. for line in ffile:
  37.     words = line.split()
  38.     # if it's an empty line continue
  39.     # if there are less then 3 words continue
  40.     if len(words) < 3: continue
  41.     if words[0] != 'From': continue
  42.     print words[2]
  43.  
  44. # Exercise 8.3
  45. inp = raw_input('Press <enter> to continue')
  46. print '\n# Exercise 8.3\n'
  47. fname =  'mbox.txt'
  48. try:
  49.     ffile = open(fname)
  50. except:
  51.     print 'File', fname, 'cannot be opened!'
  52.     exit()
  53. for line in ffile:
  54.     words = line.split()
  55.     # assignment says use and but looks easier with or
  56.     if len(words) < 3 or words[0] != 'From': continue
  57.     print words[2]
  58.  
  59. # Exercise 8.4
  60. inp = raw_input('Press <enter> to continue')
  61. print '\n# Exercise 8.4\n'
  62. fname =  'romeo.txt'
  63. wordlist = list()
  64. try:
  65.     ffile = open(fname)
  66. except:
  67.     print 'File', fname, 'cannot be opened!'
  68.     exit()
  69. for line in ffile:
  70.     words = line.split()
  71.     for word in words:
  72.         if word in wordlist: continue
  73.         wordlist.append(word)
  74. wordlist.sort()
  75. print wordlist
  76.  
  77. # Exercise 8.5
  78. inp = raw_input('Press <enter> to continue')
  79. print '\n# Exercise 8.5\n'
  80. fname =  'mbox-short.txt'
  81. count = 0
  82. try:
  83.     ffile = open(fname)
  84. except:
  85.     print 'File', fname, 'cannot be opened!'
  86.     exit()
  87. for line in ffile:
  88.     words = line.split()
  89.     # assignment says use and but looks easier with or
  90.     if len(words) < 3 or words[0] != 'From': continue
  91.     print words[1]
  92.     count += 1
  93. print 'There were %d lines in the file with From as the first word.' % count
  94.  
  95. # Exercise 8.6
  96. inp = raw_input('Press <enter> to continue')
  97. print '\n# Exercise 8.6\n'
  98. numlist = list()
  99. while True:
  100.   number = raw_input('Enter a number: ')
  101.   if number == 'done' : break;
  102.   try:
  103.      numlist.append(float(number))
  104.   except:
  105.      print "Error, please enter a number, or 'done' to finish"
  106. if len(numlist) == 0:
  107.     # Preventing ValueError when list is empty
  108.     print 'No minimum and maximum values available for an empty list.'
  109. else:
  110.    print 'Maximum =', max(numlist)
  111.    print 'Minimum =', min(numlist)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement