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


Read these instructions first!
  1. isPerfectIntCube [25 pts]
  2. isGray [25 pts]
  3. jealousDonutEaters [50 pts]
  4. Bonus/Optional:  bonusOverlappingArea [5 pts]

  1. isPerfectIntCube [25 pts]
    Write the function isPerfectIntCube(x), which takes an arbitrary value x (which may or may not be an int) and returns True if x is an int value and it is a perfect cube (that is, it is the cube of another int value). For example, 27 is a perfect cube, because 27 equals 3 cubed.  Note that 27.0 is not an int value, and so isPerfectIntCube(27.0) would return False.
     
  2. isGray [25 pts]
    Write the function isGray(rgba) that takes a 32-bit rgba value (where each of the red, green, blue, and alpha parts are 8 bits), and returns True if it represents a gray color and False otherwise.  A color is gray if all the red, green, and blue portions are equal, and the alpha portion is non-zero.  Note that both white and black are gray.  In case it helps you understand the problem, here is a sample test function:
    def testIsGray():
        print "Testing isGray()...",
        assert(isGray(0xEEEEEEEE) == True)
        assert(isGray(0xEEEEEE00) == False) # no alpha
        assert(isGray(0xFFFFFFFF) == True)  # white is gray
        assert(isGray(0x000000FF) == True)  # black is gray
        assert(isGray(0x00000000) == False) # no alpha
        assert(isGray(0x99EEEEEE) == False)
        assert(isGray(0xEE99EEEE) == False)
        assert(isGray(0xEEEE99EE) == False)
        assert(isGray(0xEEEEEE99) == True)
        print "Passed!"
  3. jealousDonutEaters [50 pts]
    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. Bonus/Optional:  bonusOverlappingArea [5 pts]
    Write the function bonusOverlappingArea(x1, y1, w1, h1, x2, y2, w2, h2) that takes 8 numeric values specifying two rectangles (where each rectangle is specified by its left-top point and its width and height), and returns the overlapping area covered by both rectangles.  If the rectangles do not overlap, your function should return 0.  In case it helps you understand the problem, here is a sample test function:
    def testBonusOverlappingArea():
        print "Testing bonusOverlappingArea()...",
        assert(bonusOverlappingArea(0, 0, 100, 100, 0, 0, 50,  50) == 2500)
        assert(bonusOverlappingArea(0, 0, 100, 100, -50, -50, 100,  100) == 2500)
        assert(bonusOverlappingArea(0, 0, 100, 100, -50, -50, 50, 50) == 0)
        print "Passed!"

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