Extra Practice for Week 1 (Due never)



  1. Previous quizzes
  2. distance(x1, y1, x2, y2)
  3. circlesIntersect(x1, y1, r1, x2, y2, r2)
  4. getInRange(x, bound1, bound2)
  5. isFactor(f, n)
  6. isFactorish(n)
  7. isMultiple(m,n)
  8. isLegalTriangle(s1, s2, s3)
  9. isRightTriangle(x1, y1, x2, y2, x3, y3)
  10. eggCartons(eggs)
  11. isEvenPositiveInt(x)
  12. nthFibonacciNumber(n)
  13. isPerfectSquare(n)
  14. nearestOdd(n)
  15. getKthDigit(n, k)
  16. setKthDigit(n, k, d)

  1. Previous Quizzes
    Here are some recent quizzes that may be helpful in studying for quiz1: You can find many more quizzes by following the previous-semester links near the top of the syllabus. Note that content varies by semester, so prior quizzes may not exactly match what we are covering this semester. If you are unsure of which parts match, ask a TA for help.

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

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

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

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

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

  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. eggCartons(eggs)
    Write the function eggCartons(eggs) that takes a non-negative integer number of eggs, and returns the smallest integer number of cartons required to hold that many eggs, where a carton may hold up to 12 eggs.

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

  12. 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!

  13. isPerfectSquare(n)
    Write the function isPerfectSquare(n) that takes a possibly-non-int value, and returns True if it is an int that is a perfect square (that is, if there exists an integer m such that m**2 == n), and False otherwise. Do not crash on non-ints nor on negative ints.

  14. nearestOdd(n)
    Write the function nearestOdd(n) that takes an int or float n, and returns as an int value the nearest odd number to n. In the case of a tie, return the smaller odd value. Note that the result must be an int, so nearestOdd(13.0) is the int 13, and not the float 13.0.

    Hint: Remember that the built-in round function works in surprising ways. Instead of round(n), you should use roundHalfUp(n) from this week's notes. That said, there are good ways to solve this problem without using rounding at all, if you prefer.

  15. getKthDigit(n, k)
    Write the function getKthDigit(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:
    getKthDigit(789, 0) == 9
    getKthDigit(789, 1) == 8
    getKthDigit(789, 2) == 7
    getKthDigit(789, 3) == 0
    getKthDigit(-789, 0) == 9

  16. setKthDigit(n, k, d)
    Write the function setKthDigit(n, k, d) that takes three integers -- n, k, and d -- where n is a possibly-negative int, k is a non-negative int, and d is a non-negative single digit (between 0 and 9 inclusive). This function returns the number n with the kth digit replaced with d. Counting starts at 0 and goes right-to-left, so the 0th digit is the rightmost digit. For example:
    setKthDigit(468, 0, 1) == 461
    setKthDigit(468, 1, 1) == 418
    setKthDigit(468, 2, 1) == 168
    setKthDigit(468, 3, 1) == 1468