15-112 Fall 2012 Quiz 3 (Autograded)
45 minutes


Read these instructions first!
  1. fasterIsPrime [25 pts]
  2. nthWithTwoNines [25 pts]
  3. jealoutDonutEaters [25 pts]
  4. drawCirclesWithinRectangle [25 pts]
  5. Bonus/Optional:  drawBonusImage [5 pts]

  1. fasterIsPrime [25 pts]
    Write the function fasterIsPrime(n).  You must verify that n is an int value, and return False otherwise.  And you must use the faster approach we covered in the notes (you can't just check all the factors up to n).
     
  2. nthWithTwoNines [25 pts]
    Write the function nthWithTwoNines(n) that takes a non-negative int n and returns the nth positive integer that contains exactly two nines.  Thus, 99, 199, and 919 are such numbers, but 999 is not.
     
  3. jealousDonutEaters [25 pts]
    Note:  This is the same as on quiz2, only now you can use loops and conditionals if it helps.
    Say that 5 people order 1 dozen donuts.  First, everyone gets 2 donuts, leaving 2 extra.  So then 2 of them each get an extra donut, and 3 of them are now jealous since they did not get an extra donut.  With this in mind, write the function jealousDonutEaters(p, d), that takes two non-negative integers p and d, and assuming that p people are eating d dozen donuts, returns the number of people that will be jealous because they got one fewer than some others.  Important:  once someone has eaten at least 4 donuts, they will not be jealous even if someone else eats more donuts.  In case it helps you understand the problem, here is a sample test function:
    def testJealousDonutEaters():
        print "Testing jealousDonutEaters()...",
        assert(jealousDonutEaters( 5, 1) == 3) # 2 get 3, 3 get 2, 3 jealous
        assert(jealousDonutEaters( 4, 1) == 0) # 3 each, none jealous
        assert(jealousDonutEaters(13, 3) == 3) # 10 get 3, 3 get 2
        assert(jealousDonutEaters( 5, 2) == 0) # all get at least 4, none jealous
        assert(jealousDonutEaters( 0, 2) == 0) # nobody to get jealous!
        assert(jealousDonutEaters( 2, 0) == 0) # no donuts to be jealous of!
        print "Passed!"
  4. drawCirclesWithinRectangle[25 pts]
    Working below the ignore_rest line, write the function drawCirclesWithinRectangle(canvas, left, top, right, bottom) that takes a rectangular area and draws circles around it like so:

    The circles should be of radius 5, and each left should be 20 pixels from the next left, and each top 20 pixels from the next top, and you may assume the rectangle's width and height will be sized so this pattern fits just inside with no extra pixels.  The left-top circle should be blue, and they should alternate red-blue-red-blue from there.
     
  5. Bonus/Optional:  drawBonusImage [5 pts]
    Working below the ignore_rest line, write the function drawBonusImage(canvas, left, top, sideLength) that takes a square area and draws the following image inside it:

    This can be done exclusively with ovals.

carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem