Computer Science 15-110, Fall 2010
Class Notes:  Recitation 4


  1. Quiz 3
  2. Practice Tracing
  3. Practice Mystery Methods
  4. Practice Problems using for and while Loops
  5. Practice Problems using Graphics Helper Functions

Recitation 4

  1. Quiz 3
    Quiz 3 will be administered at the start of recitation.

  2. Practice Tracing

    a)
    # trace this code by hand, then run it to confirm your prediction
    y = 5
    for x in range(5,10):
        print "in loop:", x, y
        if (x % y > 2):
            y -= 1
        else:
            y += 2
    print "after loop:", x, y

    b)
    # trace this code by hand, then run it to confirm your prediction
    for x in range(4):
        print "x =", x, ": ",
        for y in range(4-x):
            print y,
        print

  3. Practice Mystery Methods

    a)
    def mysteryA(n):
        # assume n is a non-negative integer
        r = 1
        for x in range(1, n+1):
            r *= x
        return r

    b)
    def mysteryB(s):
        # assume s is a string
        r = ""
        for ch in s:
            if ((ch >= 'A') and (ch <= 'Z')):
                r += chr(ord(ch) - ord("A") + ord("a"))
            else:
                r += ch
        return r


  4. Practice Problems Using for and while Loops

    Background:  read the first paragraph from the Wikipedia page on happy numbers.  After some thought, we see that no matter what number we start with, when we keep replacing the number by the sum of the squares of its digits, we'll always either arrive at 4 (unhappy) or at 1 (happy).  With that in mind, we want to write the function nthHappyNumber(n).  However, to write that function, we'll first need to write isHappyNumber(n) (right?).  And to right that function, we'll first need to write sumOfSquaresOfDigits(n).  And that's top-down design!  Here we go....
     
    1. sumOfSquaresOfDigits(n)
      Write the function sumOfSquaresOfDigits(n) which takes a non-negative integer and returns the sum of the squares of its digits.  Here are some test assertions for you:
      assert(sumOfSquaresOfDigits(5) == 25)   # 5**2 = 25
      assert(sumOfSquaresOfDigits(12) == 5)   # 1**2 + 2**2 = 1+4 = 5
      assert(sumOfSquaresOfDigits(234) == 29) # 2**2 + 3**2 + 4**2 = 4 + 9 + 16 = 29
      print "Passed all tests!"


    2. isHappyNumber(n)
      Write the function isHappyNumber(n) which takes a possibly-negative integer and returns True if it is happy and False otherwise.  Note that all numbers less than 1 are not happy.  Here are some test assertions for you:
      assert(isHappyNumber(-7) == False)
      assert(isHappyNumber(1) == True)
      assert(isHappyNumber(2) == False)
      assert(isHappyNumber(97) == True)
      assert(isHappyNumber(98) == False)
      assert(isHappyNumber(404) == True)
      assert(isHappyNumber(405) == False)
      print "Passed all tests!"


    3. nthHappyNumber(n)
      Write the function nthHappyNumber(n) which takes a non-negative integer and returns the nth happy number (where the 0th happy number is 1).  Here are some test assertions for you:
      assert(nthHappyNumber(0) == 1)
      assert(nthHappyNumber(1) == 7)
      assert(nthHappyNumber(2) == 10)
      assert(nthHappyNumber(3) == 13)
      assert(nthHappyNumber(4) == 19)
      assert(nthHappyNumber(5) == 23)
      assert(nthHappyNumber(6) == 28)
      assert(nthHappyNumber(7) == 31)
      print "Passed all tests!"

  5. Practice Problems using Graphics Helper Functions
    1. Flag of Kuwait

      Starting from the class notes on Graphics Helper Functions, replace the calls to drawBelgianFlag with calls to drawFlagOfKuwait, which you will create here....  So, write the graphics helper function drawFlagOfKuwait(canvas, left, top, right, bottom), which draws the Kuwaiti flag in the given region of the given canvas.  When completed, you should see a large Kuwaiti flag, a smaller one below, and a 3x3 grid of them on the right.

      Fun hint:  it's ok grade-wise if you just use the colors "green" and "red" here, but it won't be very satisfying.  You can specify any color you wish using RGB values.  The RGB values in the Kuwaiti flag above are (35, 158, 70) for green and (190, 0, 39) for red.  Now we have to convert these into hexadecimal (base 16), and represent it as a string starting with the "#" sign.  Here is the result:
          kuwaitGreen = "#239E46"  # rgb = 35, 158, 70
          kuwaitRed   = "#BE0027"  # rgb = 190, 0, 39
      Now we can use these colors just like we use "green", "red", etc.  So you can say:
          canvas.create_rectangle(10, 20, 30, 40, fill=kuwaitGreen)
      Excellent!!!

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