Advertisement
britneybeatey

programming hw

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