Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # SAMS 2018, Programming Sections A and B
- #########################################
- # Full name: Britney Beatey
- # Andrew ID: bbeatey
- #########################################
- # DUE DATE: Sunday July 8th, 5pm
- # SUBMIT THIS FILE TO AUTOLAB. LATE SUBMISSIONS WILL NOT BE ACCEPTED.
- # For the functions below, erase the "return 42" line and write the
- # appropriate piece of code instead.
- # You will need the following function to compare floats
- def almostEqual(d1, d2):
- epsilon = 10**-8
- return (abs(d2 - d1) < epsilon)
- #### PROBLEM 1 ####
- # Given an integer n, return True if n is even and return False otherwise.
- # Hint: use the % operator.
- def isEven(n):
- m=n%2
- if (m==0)
- return ("True")
- else ()
- return ("False")
- #### PROBLEM 2 ####
- # Given an integer n, return the ones-digit of n,
- # i.e. the first digit of n from the right.
- def onesDigit(n):
- return (abs(n)%10)
- # Given an integer n, return the tens-digit of n,
- # i.e. the 2nd digit of n from the right.
- def tensDigit(n):
- return (abs(n)%100)
- # Given an integer n and k, return the (k+1)'th digit of n from the right.
- # So k = 0 refers to the ones-digit, k = 1 refers to the tens-digit, etc.
- # You can assume k is non-negative.
- def getKthDigit(n, k):
- return (n%
- # Given integers n, k and d, replace the kthDigit of n with d.
- # You can assume k is non-negative, and d is an integer between 0 and 9.
- # NOTE: It may help to do problems 3 and 4 and then come back to this one!
- def setKthDigit(n, k, d):
- return 42
- #### PROBLEM 3 ####
- # Given a float n, round it down to the nearest integer.
- # You are not allowed to use anything from the math module.
- def floor(n):
- return 42
- #### PROBLEM 4 ####
- # Given a non-negative int street, return the nearest bus stop to the given
- # street, assuming buses stop every 8th street, including street 0, with ties
- # going to the lower street. Thus, the nearest bus stop to 12th street is
- # 8th street, and the nearest bus stop to 13th street is 16th street.
- def nearestBusStop(street):
- return 42
- #### PROBLEM 5 ####
- # Write the function threeLinesArea(m1, b1, m2, b2, m3, b3) that takes
- # six int or float values representing the 3 lines:
- # y = m1*x + b1
- # y = m2*x + b2
- # y = m3*x + b3
- # finds where each pair of lines intersects, then returns the area of the
- # triangle formed by connecting these three points of intersection.
- # If no such triangle exists (if any two of the lines are parallel), return 0.
- # To do this, you must write three helper functions: one to find where two
- # lines intersect (which you will call three times), a second to find the distance
- # between two points (see above), and a third to find the area of a triangle given
- # its side lengths (which you will call once). You may write other helper functions
- # if you think they would be useful, but you must at least write these three exactly
- # as described below, and then you must use them appropriately in your solution.
- # Of course, you should write and test the helper functions first before writing
- # this function (that's why they are helper functions - to break down this function
- # into smaller, more manageable (and testable) chunks.
- def threeLinesArea(m1, b1, m2, b2, m3, b3):
- return 42
- # This function takes four int or float values representing two lines and returns
- # the x value of the point of intersection of the two lines. If the lines are
- # parallel, or identical, the function should return None.
- def lineIntersection(m1, b1, m2, b2):
- return 42
- # returns the distance between 2 points using the formula:
- # distance equals the square root of the sum of the squares of the
- # difference between the two x coordinates and the two y coordinates
- def distance(x1, y1, x2, y2):
- return 42
- # This function takes three int or float values representing side lengths of a
- # triangle, and returns the area of that triangle. To do this, you may wish to
- # use Heron's Formula (time to use Google).
- def triangleArea(s1, s2, s3):
- return 42
- # If you have written the functions correctly, you should not get any errors
- # when you run this file, i.e., you should pass all the tests below.
- ######################################################################
- # ignore_rest: The autograder will ignore all code below here
- ######################################################################
- import math
- def testIsEven():
- print("Testing isEven()...", end="")
- assert(isEven(0))
- assert(not isEven(1))
- assert(isEven(2))
- assert(not isEven(3))
- assert(isEven(4))
- assert(not isEven(-1))
- assert(isEven(-2))
- assert(not isEven(-3))
- assert(isEven(-4))
- assert(isEven(123456))
- print("Passed.")
- def testOnesDigit():
- print("Testing onesDigit()...", end="")
- assert(onesDigit(0) == 0)
- assert(onesDigit(789) == 9)
- assert(onesDigit(7) == 7)
- assert(onesDigit(-1234) == 4)
- assert(onesDigit(-3) == 3)
- print("Passed.")
- def testTensDigit():
- print("Testing tensDigit()...", end="")
- assert(tensDigit(0) == 0)
- assert(tensDigit(1) == 0)
- assert(tensDigit(10) == 1)
- assert(tensDigit(21) == 2)
- assert(tensDigit(-1234) == 3)
- assert(tensDigit(-3) == 0)
- assert(tensDigit(-10) == 1)
- print("Passed.")
- def testGetKthDigit():
- print("Testing getKthDigit()...", end="")
- assert(getKthDigit(0,0) == 0)
- assert(getKthDigit(789, 0) == 9)
- assert(getKthDigit(789, 1) == 8)
- assert(getKthDigit(789, 2) == 7)
- assert(getKthDigit(789, 3) == 0)
- assert(getKthDigit(-1234, 3) == 1)
- assert(getKthDigit(-3, 1) == 0)
- print("Passed.")
- def testSetKthDigit():
- print("Testing setKthDigit()...", end="")
- assert(setKthDigit(468, 0, 1) == 461)
- assert(setKthDigit(468, 1, 1) == 418)
- assert(setKthDigit(468, 2, 1) == 168)
- assert(setKthDigit(468, 3, 1) == 1468)
- assert(setKthDigit(777, 2, 7) == 777)
- assert(setKthDigit(-468, 1, 5) == -458)
- print("Passed.")
- def testFloor():
- print("Testing floor()...", end="")
- assert(floor(0) == math.floor(0))
- assert(floor(1) == math.floor(1))
- assert(floor(-1) == math.floor(-1))
- assert(floor(1.1) == math.floor(1.1))
- assert(floor(1.5) == math.floor(1.5))
- assert(floor(1.9) == math.floor(1.9))
- assert(floor(-1.1) == math.floor(-1.1))
- assert(floor(-1.5) == math.floor(-1.5))
- assert(floor(-1.9) == math.floor(-1.9))
- assert(floor(0.1) == math.floor(0.1))
- assert(floor(0.5) == math.floor(0.5))
- assert(floor(0.9) == math.floor(0.9))
- assert(floor(-0.1) == math.floor(-0.1))
- assert(floor(-0.5) == math.floor(-0.5))
- assert(floor(-0.9) == math.floor(-0.9))
- print("Passed.")
- def testNearestBusStop():
- print("Testing nearestBusStop()...", end="")
- assert(nearestBusStop(0) == 0)
- assert(nearestBusStop(4) == 0)
- assert(nearestBusStop(5) == 8)
- assert(nearestBusStop(12) == 8)
- assert(nearestBusStop(13) == 16)
- assert(nearestBusStop(20) == 16)
- assert(nearestBusStop(21) == 24)
- print("Passed.")
- def testLineIntersection():
- print("Testing lineIntersection()...", end="")
- assert(lineIntersection(2.5, 3, 2.5, 11) == None)
- assert(lineIntersection(25, 3, 25, 11) == None)
- # y=3x-5 and y=x+5 intersect at (5,10)
- assert(almostEqual(lineIntersection(3,-5,1,5), 5))
- # y=10x and y=-4x+35 intersect at (2.5,25)
- assert(almostEqual(lineIntersection(10,0,-4,35), 2.5))
- print("Passed.")
- def testDistance():
- print("Testing distance()...", end="")
- assert(almostEqual(distance(0, 0, 1, 1), 2**0.5))
- assert(almostEqual(distance(3, 3, -3, -3), 6*2**0.5))
- assert(almostEqual(distance(20, 20, 23, 24), 5))
- print("Passed.")
- def testTriangleArea():
- print("Testing triangleArea()...", end="")
- assert(almostEqual(triangleArea(3,4,5), 6))
- assert(almostEqual(triangleArea(2**0.5, 1, 1), 0.5))
- assert(almostEqual(triangleArea(2**0.5, 2**0.5, 2), 1))
- print("Passed.")
- def testThreeLinesArea():
- print("Testing threeLinesArea()...", end="")
- assert(almostEqual(threeLinesArea(1, 2, 3, 4, 5, 6), 0))
- assert(almostEqual(threeLinesArea(0, 7, 1, 0, -1, 2), 36))
- assert(almostEqual(threeLinesArea(0, 3, -.5, -5, 1, 3), 42.66666666666))
- assert(almostEqual(threeLinesArea(1, -5, 0, -2, 2, 2), 25))
- assert(almostEqual(threeLinesArea(0, -9.75, -6, 2.25, 1, -4.75), 21))
- print("Passed.")
- def testAll():
- testIsEven()
- testOnesDigit()
- testTensDigit()
- testGetKthDigit()
- testSetKthDigit()
- testFloor()
- testNearestBusStop()
- testLineIntersection()
- testDistance()
- testTriangleArea()
- testThreeLinesArea()
- testAll()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement