Extra Practice for Week 1 (Due never)

• More problems will be added on Thursday.
• Note: We may solve some of these problems in recitation, so you might benefit from waiting until mid-week to do some of these.
• Please also note that we do not give out solutions to practice problems or quizzes! That's because you'll learn a lot more if you work closely with a TA (possibly through Piazza) if you're struggling to reach a solution. There's also rarely just one solution to any coding problem.
• Required files (download these to the same folder):
• Do not use string indexing, lists, list indexing, or recursion this week.
• Do not hardcode the test cases in your solutions.
• Hint: Look at the test functions in the starter file to get a better idea of what each question is asking.

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. Avoid using loops for the following problems!

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

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

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

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

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

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

9. 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?

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

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

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

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

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

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

16. numberOfPoolBalls(rows)
Pool balls are arranged in rows where the first row contains 1 pool ball and each row contains 1 more pool ball than the previous row. Thus, for example, 3 rows contain 6 total pool balls (1+2+3). With this in mind, write the function numberOfPoolBalls(rows) that takes a non-negative int value, the number of rows, and returns another int value, the number of pool balls in that number of full rows. For example, numberOfPoolBalls(3) returns 6. We will not limit our analysis to a "rack" of 15 balls. Rather, our pool table can contain an unlimited number of rows. Hint: you may want to briefly read about Triangular Numbers. Also, remember not to use loops!

17. numberOfPoolBallRows(balls)
This problem is the inverse of the previous problem. Write the function numberOfPoolBallRows(balls) that takes a non-negative int number of pool balls, and returns the smallest int number of rows required for the given number of pool balls. Thus, numberOfPoolBallRows(6) returns 3. Note that if any balls must be in a row, then you count that row, and so numberOfPoolBallRows(7) returns 4 (since the 4th row must have a single ball in it). Hint: you may want to briefly read about Triangular Numbers, and also think about how this problem relates to the numberOfPoolBalls problem above.

18. rectanglesOverlap(left1, top1, width1, height1, left2, top2, width2, height2)
A rectangle can be described by its left, top, width, and height. This function takes two rectangles described this way, and returns True if the rectangles overlap at all (even if just at a point), and False otherwise. Note: here we will represent coordinates the way they are usually represented in computer graphics, where (0,0) is at the left-top corner of the screen, and while the x-coordinate goes up while you head right, the y-coordinate goes up while you head down (so we say that "up is down").