Advertisement
britneybeatey

programming hw new

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