CMU 15-112: Fundamentals of Programming and Computer Science
Extra Practice for Week 1 (Due never)



  1. distance(x1, y1, x2, y2)
    Write the function distance(x1, y1, x2, y2) that takes four int or float values x1, y1, x2, y2 that represent the two points (x1, y1) and (x2, y2), and returns the distance between those points as a float.

  2. circlesIntersect(x1, y1, r1, x2, y2, r2)
    Write the function circlesIntersect(x1, y1, r1, x2, y2, r2) that takes 6 numbers (ints or floats) -- x1, y1, r1, x2, y2, r2 -- that describe the circle centered at (x1,y1) with radius r1, and the circle centered at (x2,y2) with radius r2, and returns True if the two circles intersect and False otherwise.

  3. getInRange(x, bound1, bound2)
    Write the function getInRange(x, bound1, bound2) which takes 3 int or float values -- x, bound1, and bound2, where bound1 is not necessarily less than bound2. If x is between the two bounds, just return it unmodified. Otherwise, if x is less than the lower bound, return the lower bound, or if x is greater than the upper bound, return the upper bound. For example:
    • getInRange(1, 3, 5) returns 3 (the lower bound, since 1 lies to the left of the range [3,5])
    • getInRange(4, 3, 5) returns 4 (the original value, since 4 is in the range [3,5])
    • getInRange(6, 3, 5) returns 5 (the upper bound, since 6 lies to the right of the range [3,5])
    • getInRange(6, 5, 3) also returns 5 (the upper bound, since 6 lies to the right of the range [3,5])

  4. 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.

  5. fabricYards(inches)
    Fabric must be purchased in whole yards, so purchasing just 1 inch of fabric requires purchasing 1 entire yard. With this in mind, write the function fabricYards(inches) that takes the number of inches of fabric desired, and returns the smallest number of whole yards of fabric that must be purchased. Note: 36 inches equals 1 yard.

  6. fabricExcess(inches)
    Write the function fabricExcess(inches) that takes the number of inches of fabric desired and returns the minimum number of inches of excess fabric that must be purchased (as purchases must be in whole yards). Hint: you may want to use fabricYards, which you just wrote!

  7. isMultiple(m,n)
    Write the function isMultiple that takes two int values m and n and returns True if m is a multiple of n and False otherwise. Note that 0 is a multiple of every integer including itself.

  8. isLegalTriangle(s1, s2, s3)
    Write the function isLegalTriangle(s1, s2, s3) that takes three int or float values representing the lengths of the sides of a triangle, and returns True if such a triangle exists and False otherwise. Note from the triangle inequality that the sum of each two sides must be greater than the third side, and further note that all sides of a legal triangle must be positive. Hint: how can you determine the longest side, and how might that help?

  9. isRightTriangle(x1, y1, x2, y2, x3, y3)
    Write the function isRightTriangle(x1, y1, x2, y2, x3, y3) that takes 6 int or float values that represent the vertices (x1,y1), (x2,y2), and (x3,y3) of a triangle, and returns True if that is a right triangle and False otherwise. You may wish to write a helper function, distance(x1, y1, x2, y2), which you might call several times. Also, remember to use almostEqual (instead of ==) when comparing floats.

  10. isEvenPositiveInt(x)
    Write the function isEvenPositiveInt(x) that takes an arbitrary value x, return True if it is an integer, and it is positive, and it is even (all 3 must be True), or False otherwise. Do not crash if the value is not an integer. So, isEvenPositiveInt("yikes!") returns False (rather than crashing), and isEvenPositiveInt(123456) returns True.

  11. nthFibonacciNumber(n)
    Background: The Fibonacci numbers are defined by F(n) = F(n-1) + F(n-2). There are different conventions on whether 0 is a Fibonacci number, and whether counting starts at n=0 or at n=1. Here, we will assume that 0 is not a Fibonacci number, and that counting starts at n=0, so F(0)=F(1)=1, and F(2)=2. With this in mind, write the function nthFibonacciNumber(n) that takes a non-negative int n and returns the nth Fibonacci number. Some test cases are provided for you. You can use Binet's Fibonacci Number Formula which (amazingly) uses the golden ratio to compute this result, though you may have to make some small change to account for the assumptions noted above. Hint: remember not to use loops!

  12. isFactorish(n)
    Write the function isFactorish(n) that takes a value n that can be of any type, and returns True if n is a (possibly-negative) integer with exactly 3 unique digits (so no two digits are the same), where each of the digits is a factor of the number n itself. In all other cases, the function returns False (without crashing). For example:
    assert(isFactorish(412) == True) # 4, 1, and 2 are all factors of 412 assert(isFactorish(-412) == True) # Must work for negative numbers! assert(isFactorish(4128) == False) # 4128 has more than 3 digits assert(isFactorish(112) == False) # 112 has duplicate digits (two 1's) assert(isFactorish(420) == False) # 420 has a 0 (no 0's allowed) assert(isFactorish(42) == False) # 42 has a leading 0 (no 0's allowed) assert(isFactorish(412.0) == False) # 412.0 is not an int assert(isFactorish('nope!') == False) # don't crash on strings