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

• These problems will help you prepare for hw1 and quiz1. They are optional and you are encouraged to collaborate when working on them.

• You may also wish to see extra-practice1-ct-and-roc.html.

• To start:
1. Create a folder named 'week1'
3. Edit extra_practice1.py using IDLE
• Do not use string indexing, loops, 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. 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.

2. fabricExcess(inches)
Write the function fabricExcess(inches) that takes the number of inches of fabric desired and returns the 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!

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

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

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

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

7. triangleArea(s1, s2, s3)
Write the function triangleArea(s1, s2, s3) that takes 3 floats and returns the area of the triangle that has those lengths of its side. If no such triangle exists, return 0. Hint: you will probably wish to use Heron's Formula.

8. triangleAreaByCoordinates(x1, y1, x2, y2, x3, y3)
Write the function triangleAreaByCoordinates(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. Hint: you should make constructive use of the triangleArea function you just wrote above.

9. lineIntersection(m1, b1, m2, b2)
Write the function lineIntersection(m1, b1, m2, b2) that takes four int or float values representing the 2 lines:
y = m1*x + b1
y = m2*x + b2
This function 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.

10. threeLinesArea(m1, b1, m2, b2, m3, b3)
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
First find where each pair of lines intersects, then return 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, use 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, and a third to find the area of a triangle given its side lengths.

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