# CMU 15-112 Spring 2016 Quiz 1 Practice (Due never)

• Edit how you wish (add imports, helper functions, etc), except:
• Do not use strings, loops, lists, or recursion this week.
• Do not edit the test code.
• Do not hardcode the test cases in your solutions.
• Quiz1 will include a subset of the problems here.

1. Code Tracing
See here.

2. isFactor(f, n)
Write the function isFactor(f, n) that takes two int values f and n, and returns True if f is a factor of n, and False otherwise. Note that every integer is a factor of 0.
def isFactor(f, n): return False # replace with your solution def testIsFactor(): print("Testing isFactor()...", end="") assert(isFactor(2, 2)) assert(isFactor(2, 4)) assert(not isFactor(2, 5)) assert(not isFactor(0, 6)) assert(isFactor(6, 0)) assert(isFactor(0, 0)) assert(isFactor(-2, 4)) print("Passed!") testIsFactor()

3. kthDigit(n, k)
Write the function kthDigit(n, k) that takes a possibly-negative int n and a non-negative int k, and returns the kth digit of n, starting from 0, counting from the right. So kthDigit(789, 0) returns 9, kthDigit(789, 2) returns 7, kthDigit(789, 3) returns 0, and kthDigit(-789, 0) returns 9.
def kthDigit(n, k): return 42 # replace with your solution def testKthDigit(): print("Testing kthDigit()...", end="") assert(kthDigit(0,0) == 0) assert(kthDigit(789, 0) == 9) assert(kthDigit(789, 1) == 8) assert(kthDigit(789, 2) == 7) assert(kthDigit(789, 3) == 0) assert(kthDigit(-1234, 3) == 1) assert(kthDigit(-3, 1) == 0) print("Passed!") testKthDigit()

4. isAlmostInteger(n)
Write the function isAlmostInteger(n) that takes an int or float n, and returns True if it is within 0.0001 of an integer value, and False otherwise. This test should be inclusive, so 5.0001 is almost an integer.
def isAlmostInteger(n): return False # replace with your solution def testIsAlmostInteger(): print("Testing isAlmostInteger()...", end="") assert(isAlmostInteger(5) == True) assert(isAlmostInteger(5.0001) == True) assert(isAlmostInteger(4.9999) == True) assert(isAlmostInteger(5.00011) == False) assert(isAlmostInteger(4.99989) == False) assert(isAlmostInteger(-4.9999) == True) assert(isAlmostInteger(-5.00011) == False) print("Passed.") testIsAlmostInteger()

5. isPerfectSquare(n)
Write the function isPerfectSquare(n) that takes a possibly-negative int n, and returns True if it is a perfect square (that is, if there exists an integer m such that m**2 == n), and False otherwise.
def isPerfectSquare(n): return False # replace with your solution def testIsPerfectSquare(): print("Testing isPerfectSquare...", end="") assert(isPerfectSquare(16) == True) assert(isPerfectSquare(32) == False) assert(isPerfectSquare(0) == True) assert(isPerfectSquare(15) == False) assert(isPerfectSquare(-16) == False) print("Passed!") testIsPerfectSquare()

6. fabricExcess(fabricInches)
Fabric must be purchased in whole yards. With this in mind, write the function fabricExcess(fabricInches) that takes a non-negative int or float representing the number of inches of fabric desired and returns as an int or float the number of inches of excess fabric that must be purchased (as purchases must be in whole yards, where a yard is 36 inches). Thus, since you need a whole yard when you buy 1 inch, fabricExcess(1) is 35. Similarly, fabricExcess(36) is 0, and fabricExcess(35.5) is 0.5.
import math def fabricExcess(fabricInches): return 42 # replace with your solution def almostEqual(d1, d2): epsilon = 0.00001 return abs(d1-d2) <= epsilon def testFabricExcess(): print("Testing fabricExcess()...", end="") assert(fabricExcess(0) == 0) assert(fabricExcess(1) == 35) assert(fabricExcess(35) == 1) assert(fabricExcess(36) == 0) assert(fabricExcess(37) == 35) # use almostEqual when comparing floats assert(almostEqual(fabricExcess(35.5), 0.5)) assert(almostEqual(fabricExcess(36.5), 35.5)) print("Passed.") testFabricExcess()

7. triangleArea(x1, y1, x2, y2, x3, y3)
Write the function triangleArea(x1, y1, x2, y2, x3, y3) that takes 6 int or float values that represent the three points (x1,y1), (x2,y2), and (x3,y3), and returns as a float the area of the triangle formed by those three points. You may ignore the case where the three points do not form a triangle. Also, you may wish to use Heron's Formula, which states that for a triangle with sides a, b, and c, then the square of the area is equal to the product s(s-a)(s-b)(s-c), where s is the semi-perimeter (half of the perimeter). A function to compute the distance between two points is provided for you.
def distance(x1, y1, x2, y2): return ((x2-x1)**2 + (y2-y1)**2)**0.5 def triangleArea(x1, y1, x2, y2, x3, y3): return 42 # replace with your solution def almostEqual(d1, d2): epsilon = 0.00001 return abs(d1-d2) <= epsilon def testTriangleArea(): print("Testing triangleArea...", end="") assert(almostEqual(triangleArea(0, 0, 0, 2, 2, 0), 2.0)) assert(almostEqual(triangleArea(0, 0, 4, 0, 2, 6), 12.0)) assert(almostEqual(triangleArea(0, 0, -4, 0, -2, -6), 12.0)) print("Passed!") testTriangleArea()