proffreda

hw04.py

Nov 9th, 2016
549
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. #Homework 04 - CS2021
  2.  
  3. def every_other(s):
  4. """Mutates a linked list so that all the odd-indiced elements are removed
  5. (using 0-based indexing).
  6.  
  7. >>> s = Link(1, Link(2, Link(3, Link(4))))
  8. >>> every_other(s)
  9. >>> s
  10. Link(1, Link(3))
  11. >>> odd_length = Link(5, Link(3, Link(1)))
  12. >>> every_other(odd_length)
  13. >>> odd_length
  14. Link(5, Link(1))
  15. >>> singleton = Link(4)
  16. >>> every_other(singleton)
  17. >>> singleton
  18. Link(4)
  19. """
  20. "*** YOUR CODE HERE ***"
  21.  
  22.  
  23. def has_cycle(s):
  24. """Return whether Link s contains a cycle.
  25.  
  26. >>> s = Link(1, Link(2, Link(3)))
  27. >>> s.rest.rest.rest = s
  28. >>> has_cycle(s)
  29. True
  30. >>> t = Link(1, Link(2, Link(3)))
  31. >>> has_cycle(t)
  32. False
  33. """
  34. "*** YOUR CODE HERE ***"
  35.  
  36.  
  37. def has_cycle_constant(s):
  38. """Return whether Link s contains a cycle.
  39.  
  40. >>> s = Link(1, Link(2, Link(3)))
  41. >>> s.rest.rest.rest = s
  42. >>> has_cycle_constant(s)
  43. True
  44. >>> t = Link(1, Link(2, Link(3)))
  45. >>> has_cycle_constant(t)
  46. False
  47. """
  48. "*** YOUR CODE HERE ***"
  49.  
  50. ##############################
  51. # Linked List implementation #
  52. ##############################
  53.  
  54. class Link:
  55.  
  56. empty = ()
  57.  
  58. def __init__(self, first, rest=empty):
  59. assert rest is Link.empty or isinstance(rest, Link)
  60. self.first = first
  61. self.rest = rest
  62.  
  63. def __len__(self):
  64. return 1 + len(self.rest)
  65.  
  66. def __repr__(self):
  67. if self.rest is not Link.empty:
  68. rest_str = ', ' + repr(self.rest)
  69. else:
  70. rest_str = ''
  71. return 'Link({0}{1})'.format(repr(self.first), rest_str)
  72.  
  73. def __contains__(self, value):
  74. "*** YOUR CODE HERE ***"
  75.  
  76. def __iadd__(self, other):
  77. "*** YOUR CODE HERE ***"
  78.  
  79.  
  80.  
  81. class ScaleIterator:
  82. """An iterator the scales elements of the iterable s by a number k.
  83.  
  84. >>> s = ScaleIterator([1, 5, 2], 5)
  85. >>> list(s)
  86. [5, 25, 10]
  87.  
  88. >>> m = ScaleIterator(naturals(), 2)
  89. >>> [next(m) for _ in range(5)]
  90. [2, 4, 6, 8, 10]
  91. """
  92. def __init__(self, s, k):
  93. "*** YOUR CODE HERE ***"
  94.  
  95. def __iter__(self):
  96. return self
  97.  
  98. def __next__(self):
  99. "*** YOUR CODE HERE ***"
  100.  
  101.  
  102.  
  103. def scale(s, k):
  104. """Yield elements of the iterable s scaled by a number k.
  105.  
  106. >>> s = scale([1, 5, 2], 5)
  107. >>> type(s)
  108. <class 'generator'>
  109. >>> list(s)
  110. [5, 25, 10]
  111.  
  112. >>> m = scale(naturals(), 2)
  113. >>> [next(m) for _ in range(5)]
  114. [2, 4, 6, 8, 10]
  115. """
  116. "*** YOUR CODE HERE ***"
  117.  
  118.  
  119. def merge(s0, s1):
  120. """Yield the elements of strictly increasing iterables s0 and s1, removing
  121. repeats. Assume that s0 and s1 have no repeats. You can also assume that s0
  122. and s1 represent infinite sequences.
  123.  
  124. >>> twos = scale(naturals(), 2)
  125. >>> threes = scale(naturals(), 3)
  126. >>> m = merge(twos, threes)
  127. >>> type(m)
  128. <class 'generator'>
  129. >>> [next(m) for _ in range(10)]
  130. [2, 3, 4, 6, 8, 9, 10, 12, 14, 15]
  131. """
  132. i0, i1 = iter(s0), iter(s1)
  133. e0, e1 = next(i0), next(i1)
  134. "*** YOUR CODE HERE ***"
  135.  
  136.  
  137.  
  138. def make_s():
  139. """A generator function that yields all positive integers with only factors
  140. 2, 3, and 5.
  141.  
  142. >>> s = make_s()
  143. >>> type(s)
  144. <class 'generator'>
  145. >>> [next(s) for _ in range(20)]
  146. [1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24, 25, 27, 30, 32, 36]
  147. """
  148. "*** YOUR CODE HERE ***"
  149.  
  150.  
  151. def naturals():
  152. """A generator function that yields the infinite sequence of natural
  153. numbers, starting at 1.
  154.  
  155. >>> m = naturals()
  156. >>> type(m)
  157. <class 'generator'>
  158. >>> [next(m) for _ in range(10)]
  159. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  160. """
  161. i = 1
  162. while True:
  163. yield i
  164. i += 1
  165.  
  166.  
  167. def _test():
  168. import doctest
  169. doctest.testmod()
  170.  
  171. if __name__ == "__main__":
  172. _test()
Advertisement
Add Comment
Please, Sign In to add comment