Advertisement
britneybeatey

hw4

Jul 25th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.55 KB | None | 0 0
  1. # SAMS 2018, Programming Sections A and B
  2. #########################################
  3. # Full name: Britney Beatey
  4. # Andrew ID: bbeatey
  5. #########################################
  6.  
  7. # DUE DATE: Sunday July 29th, 5pm.
  8. # SUBMIT THIS FILE TO AUTOLAB. LATE SUBMISSIONS WILL NOT BE ACCEPTED.
  9.  
  10. # For the functions below, erase the "return None" line and write the
  11. # appropriate piece of code instead.
  12.  
  13. # IMPORTANT NOTE:
  14. # You are not allowed to import any modules or use recursion.
  15.  
  16. # Takes a list, l, and rotates all the elements in l one position to the left,
  17. # placing the first element in the last position. For example, if the list
  18. # before the call to rotateLeft is 1, 4, 5, 6, it is 4, 5, 6, 1 after the call.
  19. def rotateLeft(n):
  20. if len(n) == 0:
  21. return None
  22. if len(n) == 1:
  23. return None
  24. first = n[0]
  25. for i in range(len(n)-1):
  26. n[i] = n[i+1]
  27. n[len(n)-1] = first
  28. return None
  29.  
  30. # Takes a list, l, and rotates all the elements in l one position to the right,
  31. # placing the last element in position 0. For example, if the list before the
  32. # call to rotateRight is 1, 4, 5, 6, it is 6, 1, 4, 5 after the call.
  33. def rotateRight(n):
  34. if len(n) == 0:
  35. return None
  36. if len(n) == 1:
  37. return None
  38. last = n[-1]
  39. for i in range((len(n)-1),-1,-1):
  40. n[i] = n[i-1]
  41. n[0] = last
  42. return None
  43.  
  44.  
  45. # Takes a list, l and reverses the elements stored in the list in place. For example,
  46. # if the list before the call to reverse is 1, 4, 5, 6, it will be 6, 5, 4, 1 after
  47. # the call. Obviously, you are NOT allowed to use the built-in Python list.reverse()
  48. # method to solve this question!
  49. def reverse(n):
  50. if len(n) == 0:
  51. return None
  52. if len(n) == 1:
  53. return None
  54. first = 0
  55. last = (len(n)-1)
  56. while first < last:
  57. n[first],n[last] = n[last],n[first]
  58. first = first + 1
  59. last = last-1
  60. return None
  61.  
  62.  
  63. # Unlike the previous three functions which were destructive (i.e., they modified the
  64. # original list), this function takes a list and returns a new list with any duplicate
  65. # values removed (i.e., the new list will contain all the unique values in the original
  66. # list). The original list must not be modified in any way.
  67. def copyNoDups(n):
  68. return []
  69.  
  70.  
  71. # Takes a list, l, and returns the index of the first occurrence of target in l.
  72. # Returns -1 if target does not occur in l
  73. def firstIndex(n, target):
  74. return 42
  75.  
  76.  
  77. # Takes a list, l, and returns the index of the last occurrence of target in l.
  78. # Returns -1 if target does not occur in l
  79. def lastIndex(n, target):
  80. return 42
  81.  
  82.  
  83. # Takes a list of integers and counts the number of multiples of 2 and 3 that occur
  84. # in the list and returns whichever count is higher.
  85. def maxMultiples23(ints):
  86. return 42
  87.  
  88.  
  89. # Takes a list of integers and keeps track of the sum of the values as you traverse
  90. # from the first value to the last. But instead of returning the sum (how boring),
  91. # you will return the number of times the sum returns to zero after becoming non-zero.
  92. # For example, if [1,-1,0,-2,3,-3,2] were the parameter, the sum would start at 0,
  93. # and then take on the values 1, 0, 0, -2, 1, -2, 0; so the function would return 2
  94. def countCycles(ints):
  95. return 42
  96.  
  97.  
  98.  
  99. ######################################################################
  100. # ignore_rest: The autograder will ignore all code below here
  101. ######################################################################
  102.  
  103. def testRotateLeft():
  104. print("Testing rotateLeft...", end="")
  105. a = ['a']
  106. rotateLeft(a)
  107. assert(a == ['a'])
  108. a = ['a','b']
  109. rotateLeft(a)
  110. assert(a == ['b','a'])
  111. a = ['a','b','c']
  112. rotateLeft(a)
  113. assert(a == ['b','c','a'])
  114. a = [1, 4, 5, 6]
  115. rotateLeft(a)
  116. assert(a == [4, 5, 6, 1])
  117. print("Passed. (Add more tests to be more sure!)")
  118.  
  119. def testRotateRight():
  120. print("Testing rotateRight...", end="")
  121. a = ['a']
  122. rotateRight(a)
  123. assert(a == ['a'])
  124. a = ['a','b']
  125. rotateRight(a)
  126. assert(a == ['b','a'])
  127. a = ['a','b','c']
  128. rotateRight(a)
  129. assert(a == ['c','a', 'b'])
  130. a = [1, 4, 5, 6]
  131. rotateRight(a)
  132. assert(a == [6, 1, 4, 5])
  133. print("Passed. (Add more tests to be more sure!)")
  134.  
  135. def testReverse():
  136. print("Testing reverse..", end="")
  137. a = ['a']
  138. reverse(a)
  139. assert(a == ['a'])
  140. a = ['a','b']
  141. reverse(a)
  142. assert(a == ['b','a'])
  143. a = ['a','b','c']
  144. reverse(a)
  145. assert(a == ['c','b', 'a'])
  146. print("Passed. (Add more tests to be more sure!)")
  147.  
  148. def testCopyNoDups():
  149. print("Testing copyNoDups...", end="")
  150. a = ['a']
  151. assert(copyNoDups(a) == ['a'] and a == ['a'])
  152. a = ['a','a','a']
  153. assert(copyNoDups(a) == ['a'] and a == ['a','a','a'])
  154. a = ['a','b','b']
  155. assert(copyNoDups(a) == ['a','b'] and a == ['a','b','b'])
  156. a = [1,2,3,4,2,2,1,2,5]
  157. assert(copyNoDups(a) == [1,2,3,4,5] and a == [1,2,3,4,2,2,1,2,5])
  158. print("Passed. (Add more tests to be more sure!)")
  159.  
  160. def testFirstIndex():
  161. print("Testing firstIndex...", end="")
  162. assert(firstIndex([1,2,3],2) == 1)
  163. assert(firstIndex([1,2,2],2) == 1)
  164. assert(firstIndex([1,2,8,9,2],2) == 1)
  165. assert(firstIndex([1,2,3],4) == -1)
  166. print("Passed. (Add more tests to be more sure!)")
  167.  
  168. def testLastIndex():
  169. print("Testing lastIndex...", end="")
  170. assert(lastIndex([1,2,3],2) == 1)
  171. assert(lastIndex([1,2,2],2) == 2)
  172. assert(lastIndex([1,2,8,9,2],2) == 4)
  173. assert(lastIndex([1,2,3],4) == -1)
  174. print("Passed. (Add more tests to be more sure!)")
  175.  
  176. def testMaxMultiples23():
  177. print("Testing maxMultiples23..", end="")
  178. assert(maxMultiples23([2,4,3,6,8]) == 4)
  179. assert(maxMultiples23([2,4,3,9]) == 2)
  180. assert(maxMultiples23([1,7,9,11,17,19]) == 1)
  181. assert(maxMultiples23([-1,-3]) == 1)
  182. assert(maxMultiples23([]) == 0)
  183. print("Passed. (Add more tests to be more sure!)")
  184.  
  185. def testCountCycles():
  186. print("Testing countCycles..", end="")
  187. assert(countCycles([1,-1,0,-2,3,-3,2]) == 2)
  188. assert(countCycles([1,-1,-2,3,-3,2]) == 2)
  189. assert(countCycles([]) == 0)
  190. assert(countCycles(list(range(10))) == 0)
  191. assert(countCycles(list(range(5)) + list(range(-4,1))) == 1)
  192. assert(countCycles([1,-1,0,5,-3,-2,0,2]) == 2)
  193. assert(countCycles([1,-1,-2,2,3,-3]) == 3)
  194. print("Passed. (Add more tests to be more sure!)")
  195.  
  196. def testAll():
  197. testRotateLeft()
  198. testRotateRight()
  199. testReverse()
  200. testCopyNoDups()
  201. testFirstIndex()
  202. testLastIndex()
  203. testMaxMultiples23()
  204. testCountCycles()
  205.  
  206. testAll()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement