CMU 15-112 Spring 2018: Fundamentals of Programming and Computer Science
Homework 1 (Due Saturday 20-Jan, at 8pm)




  1. Lab Problems [10pts]
    Attend your scheduled lab on Friday. While there, complete the basic problem and make a real attempt at the advanced problem. One of the TAs will record your participation by hand.

  2. Reasoning over Code [15pts]
    Given the function roc1 (shown below), find a value that will make roc1 return True. You should then modify the function roc1Answer() to return that value so that the autograder will accept your work.

    def roc1(x): a = x % 42 b = x // 42 return a == b and x > 450

    Note: yes, you could try every possible value in this function until you find one that works. That won't help you learn, though. Instead, try working through the problem on paper, to see if you can figure out what a correct answer should look like without going through all the possibilities.

  3. getTheCents(n) [25pts]
    Write the function getTheCents(n) which takes a value n (which represents a payment in US dollars) as input and returns the number of cents in the payment. If n is an int, the function should return 0, as it has 0 cents; otherwise, if it isn't a float, it should return None, because a non-number payment make no cents (ha!). If the payment has partial cents (for example, 3.959), it should be rounded up to the nearest cent.

  4. playGuessingGame() [25pts]
    Write the function playGuessingGame() which runs a short guessing game with the user. The program should ask the user two Yes-or-No questions, then guess what the user is thinking based on their answers. You must use the specific prompts shown in the example exchanges below to pass the test cases (where the bolded words are user input), so you should copy the prompts directly into your code to avoid typos.

    Let's play a guessing game! Think of a type of pet. Does it have fur?Yes Can you teach it to play fetch?Yes It's a dog!
    Let's play a guessing game! Think of a type of pet. Does it have fur?Yes Can you teach it to play fetch?No It's a cat!
    Let's play a guessing game! Think of a type of pet. Does it have fur?No Can it swim?Yes It's a fish!
    Let's play a guessing game! Think of a type of pet. Does it have fur?No Can it swim?No It's a bird!

  5. isRightTriangle(x1, y1, x2, y2, x3, y3) [25pts]
    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 must also write a helper function, distance(x1, y1, x2, y2), which should be used by isRightTriangle. This function takes four int or float values representing two points and returns the distance between those points. Remember to use almostEqual (instead of ==) when comparing floats!

  6. Bonus/optional: bonusFindIntRootsOfCubic(a,b,c,d) [3 pts]
    Write the function bonusFindIntRootsOfCubic(a,b,c,d) that takes the int or float coefficients a, b, c, d of a cubic equation of this form:
         y = ax3 + bx2 + cx + d
    You are guaranteed the function has 3 real roots, and in fact that the roots are all integers.  Your function should return these 3 roots in increasing order.  How does a function return multiple values?  Like so:
        return root1, root2, root3
    To get started, you'll want to read about Cardano's cubic formula here (great stuff!).  Then, from that page, use this formula:
     
    x   =   {q + [q2 + (r-p2)3]1/2}1/3   +   {q - [q2 + (r-p2)3]1/2}1/3   +   p

    where:

    p = -b/(3a),   q = p3 + (bc-3ad)/(6a2),   r = c/(3a)

    This isn't quite as simple as it seems, because your solution for x will not only be approximate (and not exactly an int, so you'll have to do something about that), but it may not even be real!  Though the solution is real, the intermediate steps may include some complex values, and in these cases the solution will include a (possibly-negligibly-small) imaginary value.  So you'll have to convert from complex to real (try c.real if c is complex), and then convert from real to int.

    Great, now you have one root.  What about the others?  Well, we can divide the one root out and that will leave us with a quadratic equation, which of course is easily solved.  A brief, clear explanation of this step is provided here.  Don't forget to convert these to int values, too!

    So now you have all three int roots.  Great job!  All that's left is to sort them.  Now, if this were later in the course, you could put them in a list and call a built-in function that will sort for you.  But it's not, so you can't.  Instead, figure out how to sort these values using the limited built-in functions and arithmetic available this week.  Then just return these 3 values and you're done.