CMU 15-112: Fundamentals of Programming and Computer Science
Week1 Practice (Due never)
- These problems will help you prepare for hw1 and for quiz1.
- To start:
- Create a folder named 'week1'
- download both wk1_practice.py and cs112_f16_wk1.py to that folder
- Edit wk1_practice.py using pyzo
- Do not use strings, loops, lists, or recursion this week.
- Do not hardcode the test cases in your solutions.
Wed Recitation
- 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. - 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 other than itself. Also, you should make constructive use of the isFactor function you just wrote above. - 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. - 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? - 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. - 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.
Thu Lecture
- 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. - 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. - 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. - 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. - nearestBusStop(street)
Write the function nearestBusStop(street) that takes a non-negative int street number, and returns the nearest bus stop to the given street, where buses stop every 8th street, including street 0, and ties go to the lower street, so the nearest bus stop to 12th street is 8th street, and the nearest bus stop to 13 street is 16th street.
TA-led Small-Group Session
- 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. For this problem and the next, you should research Triangular Numbers. - 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).