CMU 15-112: Fundamentals of Programming and Computer Science
Week 1: Conditionals



Code Tracing
What will this code print? Figure it out by hand, then run the code to confirm. Then slightly edit the code and try again.

Free Response (Problem-Solving) - Edits from Previous Practice

  1. triangleArea(s1, s2, s3) (Non-restricted)
    Note: You can edit your code from the previous Practice document 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.

  2. triangleAreaByCoordinates(x1, y1, x2, y2, x3, y3) (Non-restricted)
    Note: You can edit your code from the previous Practice document 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.

  3. lineIntersection(m1, b1, m2, b2) (Non-restricted)
    Note: You can edit your code from the previous Practice document 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.

  4. nthFibonacciNumber(n) (Non-restricted)
    Note: You can edit your code from the previous Practice document 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.

Free Response (Problem-Solving)

  1. pascalsTriangleValue(row, col)
    Write the function pascalsTriangleValue(row, col) that takes int values row and col, and returns the value in the given row and column of Pascal's Triangle where the triangle starts at row 0, and each row starts at column 0. If either row or col are not legal values, return None, instead of crashing. Hint: math.factorial may be helpful!

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

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

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

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

  6. rectanglesOverlap(left1, top1, width1, height1, left2, top2, width2, height2)
    Background: we will define the integer-square-root of a non-negative integer N to be the largest integer M such that M**2 <= N. So the integer-square-root of 99 is 9, becuase 9**2 is 81 which is <= 99, but 10**2 is 100 which is not <= 99. With this in mind, write the function integerSquareRoot(v) that takes an arbitrary value v (possibly not an integer), and if v is a non-negative integer, returns the integer-square-root of v. Otherwise, the function returns None.

  7. alternatesEvenOdd(n)
    Write the function alternatesEvenOdd(n) that takes a positive int n that has exactly 3 digits (ignoring leading 0's), so 100 <= n <= 999, and returns True if n alternates between even and odd digits, and False otherwise. Another way to say this is that there are no two consecutive even digits and no two consecutive odd digits.

  8. isSmallTidy(n)
    We will say that a value n is a small tidy (coined term) if it is an integer number with precisely three digits and its digits are either strictly increasing or strictly decreasing in order. For example, 123, 321, and-941 are small tidy numbers because they have three digits and their digits are in strictly increasing order (123) or strictly decreasing order (321 and-941). With this in mind, and without using strings or loops, write the function isSmallTidy(n) that takes a value n, which may or may not be an integer, and returns True if n is a small tidy number, and False otherwise. Do not crash if n is not an integer! Do not use strings or loops here.

  9. isSmall42ish(n)
    We will say that a value n is a small 42ish (coined term) if it is an integer number with precisely four digits and has at least one pair of consecutive digits that form the number 42. For example, 4212, 5042, and-4242 are small 42ish numbers because they are integers, have four digits, and 42 appears inside each of these numbers. With this in mind, and without using strings or loops, write the function isSmall42ish(n) that takes a value n, which may or may not be an integer, and returns True if n is a small 42ish number, and False otherwise. Do not crash if n is not an integer! Do not use strings or loops here.