proffreda

lab04.py

Sep 15th, 2016
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. ## Lists, Dictionaries ##
  2.  
  3. #########
  4. # Lists #
  5. #########
  6.  
  7. # Q5
  8. def reverse_iter(lst):
  9. """Returns the reverse of the given list.
  10.  
  11. >>> reverse_iter([1, 2, 3, 4])
  12. [4, 3, 2, 1]
  13. """
  14. "*** YOUR CODE HERE ***"
  15.  
  16.  
  17.  
  18. # Q6
  19. def reverse_recursive(lst):
  20. """Returns the reverse of the given list.
  21.  
  22. >>> reverse_recursive([1, 2, 3, 4])
  23. [4, 3, 2, 1]
  24. """
  25. "*** YOUR CODE HERE ***"
  26.  
  27.  
  28. # Q7
  29. def map(fn, seq):
  30. """Applies fn onto each element in seq and returns a list.
  31.  
  32. >>> map(lambda x: x*x, [1, 2, 3])
  33. [1, 4, 9]
  34. """
  35. "*** YOUR CODE HERE ***"
  36.  
  37. def filter(pred, seq):
  38. """Keeps elements in seq only if they satisfy pred.
  39.  
  40. >>> filter(lambda x: x % 2 == 0, [1, 2, 3, 4])
  41. [2, 4]
  42. """
  43. "*** YOUR CODE HERE ***"
  44.  
  45. def reduce(combiner, seq):
  46. """Combines elements in seq using combiner.
  47.  
  48. >>> reduce(lambda x, y: x + y, [1, 2, 3, 4])
  49. 10
  50. >>> reduce(lambda x, y: x * y, [1, 2, 3, 4])
  51. 24
  52. >>> reduce(lambda x, y: x * y, [4])
  53. 4
  54. """
  55. "*** YOUR CODE HERE ***"
  56.  
  57.  
  58. ################
  59. # Dictionaries #
  60. ################
  61.  
  62. # Q10
  63. def build_successors_table(tokens):
  64. """Return a dictionary: keys are words; values are lists of
  65. successors.
  66.  
  67. >>> text = ['We', 'came', 'to', 'investigate', ',', 'catch', 'bad', 'guys', 'and', 'to', 'eat', 'pie', '.']
  68. >>> table = build_successors_table(text)
  69. >>> sorted(table)
  70. [',', '.', 'We', 'and', 'bad', 'came', 'catch', 'eat', 'guys', 'investigate', 'pie', 'to']
  71. >>> table['to']
  72. ['investigate', 'eat']
  73. >>> table['pie']
  74. ['.']
  75. >>> table['.']
  76. ['We']
  77. """
  78. table = {}
  79. prev = '.'
  80. for word in tokens:
  81. if prev not in table:
  82. "*** YOUR CODE HERE ***"
  83. "*** YOUR CODE HERE ***"
  84. prev = word
  85. return table
  86.  
  87. # Q11
  88. def construct_tweet(word, table):
  89. """Prints a random sentence starting with word, sampling from
  90. table.
  91. """
  92. import random
  93. result = ' '
  94. while word not in ['.', '!', '?']:
  95. "*** YOUR CODE HERE ***"
  96. return result + word
  97.  
  98. # Warning: do NOT try to print the return result of this function
  99. def shakespeare_tokens(path='shakespeare.txt', url='http://composingprograms.com/shakespeare.txt'):
  100. """Return the words of Shakespeare's plays as a list."""
  101. import os
  102. from urllib.request import urlopen
  103. if os.path.exists(path):
  104. return open('shakespeare.txt', encoding='ascii').read().split()
  105. else:
  106. shakespeare = urlopen(url)
  107. return shakespeare.read().decode(encoding='ascii').split()
  108.  
  109. def trump_tokens(path='trumptweets.txt', url='http://pastebin.com/raw/nWvFKcH7'):
  110. """Return the words found in tweets of Trump as a list."""
  111. import os
  112. from urllib.request import urlopen
  113. if os.path.exists(path):
  114. return open('trumptweets.txt', encoding='ascii').read().split()
  115. else:
  116. trump = urlopen(url)
  117. return trump.read().decode(encoding='ascii').split()
  118.  
  119. # Uncomment the following lines
  120. # shakestokens = shakespeare_tokens()
  121. # shakestable = build_successors_table(shakestokens)
  122. # trumptokens = trump_tokens()
  123. # trumptable = build_successors_table(trumptokens)
  124.  
  125. def random_tweet(table):
  126. import random
  127. return construct_tweet(random.choice(table['.']), table)
Advertisement
Add Comment
Please, Sign In to add comment