Advertisement
britneybeatey

programming A

Jul 3rd, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.65 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 8th, 5pm
  8. # SUBMIT THIS FILE TO AUTOLAB. LATE SUBMISSIONS WILL NOT BE ACCEPTED.
  9.  
  10. # For the functions below, erase the "return 42" line and write the
  11. # appropriate piece of code instead.
  12.  
  13. # You will need the following function to compare floats
  14. def almostEqual(d1, d2):
  15. epsilon = 10**-8
  16. return (abs(d2 - d1) < epsilon)
  17.  
  18.  
  19. #### PROBLEM 1 ####
  20.  
  21. # Given an integer n, return True if n is even and return False otherwise.
  22. # Hint: use the % operator.
  23. def isEven(n):
  24. m=n%2
  25. if (m==0)
  26. return ("True")
  27. else ()
  28. return ("False")
  29.  
  30. #### PROBLEM 2 ####
  31.  
  32. # Given an integer n, return the ones-digit of n,
  33. # i.e. the first digit of n from the right.
  34. def onesDigit(n):
  35. return (abs(n)%10)
  36.  
  37. # Given an integer n, return the tens-digit of n,
  38. # i.e. the 2nd digit of n from the right.
  39. def tensDigit(n):
  40. return (abs(n)%100)
  41.  
  42. # Given an integer n and k, return the (k+1)'th digit of n from the right.
  43. # So k = 0 refers to the ones-digit, k = 1 refers to the tens-digit, etc.
  44. # You can assume k is non-negative.
  45. def getKthDigit(n, k):
  46. return (n%
  47.  
  48.  
  49. # Given integers n, k and d, replace the kthDigit of n with d.
  50. # You can assume k is non-negative, and d is an integer between 0 and 9.
  51. # NOTE: It may help to do problems 3 and 4 and then come back to this one!
  52. def setKthDigit(n, k, d):
  53. return 42
  54.  
  55.  
  56. #### PROBLEM 3 ####
  57.  
  58. # Given a float n, round it down to the nearest integer.
  59. # You are not allowed to use anything from the math module.
  60. def floor(n):
  61. return 42
  62.  
  63.  
  64. #### PROBLEM 4 ####
  65.  
  66. # Given a non-negative int street, return the nearest bus stop to the given
  67. # street, assuming buses stop every 8th street, including street 0, with ties
  68. # going to the lower street. Thus, the nearest bus stop to 12th street is
  69. # 8th street, and the nearest bus stop to 13th street is 16th street.
  70. def nearestBusStop(street):
  71. return 42
  72.  
  73.  
  74. #### PROBLEM 5 ####
  75.  
  76. # Write the function threeLinesArea(m1, b1, m2, b2, m3, b3) that takes
  77. # six int or float values representing the 3 lines:
  78. # y = m1*x + b1
  79. # y = m2*x + b2
  80. # y = m3*x + b3
  81. # finds where each pair of lines intersects, then returns the area of the
  82. # triangle formed by connecting these three points of intersection.
  83. # If no such triangle exists (if any two of the lines are parallel), return 0.
  84.  
  85. # To do this, you must write three helper functions: one to find where two
  86. # lines intersect (which you will call three times), a second to find the distance
  87. # between two points (see above), and a third to find the area of a triangle given
  88. # its side lengths (which you will call once). You may write other helper functions
  89. # if you think they would be useful, but you must at least write these three exactly
  90. # as described below, and then you must use them appropriately in your solution.
  91. # Of course, you should write and test the helper functions first before writing
  92. # this function (that's why they are helper functions - to break down this function
  93. # into smaller, more manageable (and testable) chunks.
  94. def threeLinesArea(m1, b1, m2, b2, m3, b3):
  95. return 42
  96.  
  97. # This function takes four int or float values representing two lines and returns
  98. # the x value of the point of intersection of the two lines. If the lines are
  99. # parallel, or identical, the function should return None.
  100. def lineIntersection(m1, b1, m2, b2):
  101. return 42
  102.  
  103. # returns the distance between 2 points using the formula:
  104. # distance equals the square root of the sum of the squares of the
  105. # difference between the two x coordinates and the two y coordinates
  106. def distance(x1, y1, x2, y2):
  107. return 42
  108.  
  109. # This function takes three int or float values representing side lengths of a
  110. # triangle, and returns the area of that triangle. To do this, you may wish to
  111. # use Heron's Formula (time to use Google).
  112. def triangleArea(s1, s2, s3):
  113. return 42
  114.  
  115.  
  116. # If you have written the functions correctly, you should not get any errors
  117. # when you run this file, i.e., you should pass all the tests below.
  118.  
  119.  
  120. ######################################################################
  121. # ignore_rest: The autograder will ignore all code below here
  122. ######################################################################
  123.  
  124. import math
  125.  
  126. def testIsEven():
  127. print("Testing isEven()...", end="")
  128. assert(isEven(0))
  129. assert(not isEven(1))
  130. assert(isEven(2))
  131. assert(not isEven(3))
  132. assert(isEven(4))
  133. assert(not isEven(-1))
  134. assert(isEven(-2))
  135. assert(not isEven(-3))
  136. assert(isEven(-4))
  137. assert(isEven(123456))
  138. print("Passed.")
  139.  
  140. def testOnesDigit():
  141. print("Testing onesDigit()...", end="")
  142. assert(onesDigit(0) == 0)
  143. assert(onesDigit(789) == 9)
  144. assert(onesDigit(7) == 7)
  145. assert(onesDigit(-1234) == 4)
  146. assert(onesDigit(-3) == 3)
  147. print("Passed.")
  148.  
  149. def testTensDigit():
  150. print("Testing tensDigit()...", end="")
  151. assert(tensDigit(0) == 0)
  152. assert(tensDigit(1) == 0)
  153. assert(tensDigit(10) == 1)
  154. assert(tensDigit(21) == 2)
  155. assert(tensDigit(-1234) == 3)
  156. assert(tensDigit(-3) == 0)
  157. assert(tensDigit(-10) == 1)
  158. print("Passed.")
  159.  
  160. def testGetKthDigit():
  161. print("Testing getKthDigit()...", end="")
  162. assert(getKthDigit(0,0) == 0)
  163. assert(getKthDigit(789, 0) == 9)
  164. assert(getKthDigit(789, 1) == 8)
  165. assert(getKthDigit(789, 2) == 7)
  166. assert(getKthDigit(789, 3) == 0)
  167. assert(getKthDigit(-1234, 3) == 1)
  168. assert(getKthDigit(-3, 1) == 0)
  169. print("Passed.")
  170.  
  171. def testSetKthDigit():
  172. print("Testing setKthDigit()...", end="")
  173. assert(setKthDigit(468, 0, 1) == 461)
  174. assert(setKthDigit(468, 1, 1) == 418)
  175. assert(setKthDigit(468, 2, 1) == 168)
  176. assert(setKthDigit(468, 3, 1) == 1468)
  177. assert(setKthDigit(777, 2, 7) == 777)
  178. assert(setKthDigit(-468, 1, 5) == -458)
  179. print("Passed.")
  180.  
  181. def testFloor():
  182. print("Testing floor()...", end="")
  183. assert(floor(0) == math.floor(0))
  184. assert(floor(1) == math.floor(1))
  185. assert(floor(-1) == math.floor(-1))
  186. assert(floor(1.1) == math.floor(1.1))
  187. assert(floor(1.5) == math.floor(1.5))
  188. assert(floor(1.9) == math.floor(1.9))
  189. assert(floor(-1.1) == math.floor(-1.1))
  190. assert(floor(-1.5) == math.floor(-1.5))
  191. assert(floor(-1.9) == math.floor(-1.9))
  192. assert(floor(0.1) == math.floor(0.1))
  193. assert(floor(0.5) == math.floor(0.5))
  194. assert(floor(0.9) == math.floor(0.9))
  195. assert(floor(-0.1) == math.floor(-0.1))
  196. assert(floor(-0.5) == math.floor(-0.5))
  197. assert(floor(-0.9) == math.floor(-0.9))
  198. print("Passed.")
  199.  
  200. def testNearestBusStop():
  201. print("Testing nearestBusStop()...", end="")
  202. assert(nearestBusStop(0) == 0)
  203. assert(nearestBusStop(4) == 0)
  204. assert(nearestBusStop(5) == 8)
  205. assert(nearestBusStop(12) == 8)
  206. assert(nearestBusStop(13) == 16)
  207. assert(nearestBusStop(20) == 16)
  208. assert(nearestBusStop(21) == 24)
  209. print("Passed.")
  210.  
  211. def testLineIntersection():
  212. print("Testing lineIntersection()...", end="")
  213. assert(lineIntersection(2.5, 3, 2.5, 11) == None)
  214. assert(lineIntersection(25, 3, 25, 11) == None)
  215. # y=3x-5 and y=x+5 intersect at (5,10)
  216. assert(almostEqual(lineIntersection(3,-5,1,5), 5))
  217. # y=10x and y=-4x+35 intersect at (2.5,25)
  218. assert(almostEqual(lineIntersection(10,0,-4,35), 2.5))
  219. print("Passed.")
  220.  
  221. def testDistance():
  222. print("Testing distance()...", end="")
  223. assert(almostEqual(distance(0, 0, 1, 1), 2**0.5))
  224. assert(almostEqual(distance(3, 3, -3, -3), 6*2**0.5))
  225. assert(almostEqual(distance(20, 20, 23, 24), 5))
  226. print("Passed.")
  227.  
  228. def testTriangleArea():
  229. print("Testing triangleArea()...", end="")
  230. assert(almostEqual(triangleArea(3,4,5), 6))
  231. assert(almostEqual(triangleArea(2**0.5, 1, 1), 0.5))
  232. assert(almostEqual(triangleArea(2**0.5, 2**0.5, 2), 1))
  233. print("Passed.")
  234.  
  235. def testThreeLinesArea():
  236. print("Testing threeLinesArea()...", end="")
  237. assert(almostEqual(threeLinesArea(1, 2, 3, 4, 5, 6), 0))
  238. assert(almostEqual(threeLinesArea(0, 7, 1, 0, -1, 2), 36))
  239. assert(almostEqual(threeLinesArea(0, 3, -.5, -5, 1, 3), 42.66666666666))
  240. assert(almostEqual(threeLinesArea(1, -5, 0, -2, 2, 2), 25))
  241. assert(almostEqual(threeLinesArea(0, -9.75, -6, 2.25, 1, -4.75), 21))
  242. print("Passed.")
  243.  
  244.  
  245. def testAll():
  246. testIsEven()
  247. testOnesDigit()
  248. testTensDigit()
  249. testGetKthDigit()
  250. testSetKthDigit()
  251. testFloor()
  252. testNearestBusStop()
  253. testLineIntersection()
  254. testDistance()
  255. testTriangleArea()
  256. testThreeLinesArea()
  257.  
  258.  
  259. testAll()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement