Computer Science 15-112, Spring 2012
Class Notes:  Writing Functions Practice (Problem-Solving)


  1. eggCartons
  2. militaryTimeToStandardTime

Writing Functions Practice (Problem-Solving)

  1. eggCartons

    # eggCartons
    # Write a program that reads in a non-negative number of eggs,
    # and prints the number of egg cartons required to hold that many eggs
    # (given that each egg carton holds one dozen eggs, and you cannot buy
    # fractional egg cartons).  Be sure your program works for multiples of 12,
    # including 0.

    def eggCartons(eggs):
        # You write this!
       
    def testEggCartons():
        print "Testing eggCartons()...",
        assert(eggCartons(0) == 0)
        assert(eggCartons(1) == 1)
        assert(eggCartons(12) == 1)
        assert(eggCartons(13) == 2)
        assert(eggCartons(24) == 2)
        assert(eggCartons(25) == 3)
        print "Passed all tests!"

    def runEggCartons():
        eggs = int(raw_input("Enter number of eggs:"))
        cartons = eggCartons(eggs)
        print eggs, "eggs require", cartons, "egg cartons"
       
    testEggCartons() # run the robotic test function
    runEggCartons()  # let the user use the function


    Sample Solution:

    # Sample Solution is in white text (select to view)
    ##########################################
    def eggCartons(eggs):
        return (eggs + 11) / 12
    ##########################################


  2. militaryTimeToStandardTime

    # militaryTimeToStandardTime
    # Write a program that reads in an integer between 0 and 23,
    # representing the hour in military time, and prints the same
    # hour in standard time.  For example, 17 is 5 o'clock.

    def militaryTimeToStandardTime(militaryTime):
        # You write this!

    def testMilitaryTimeToStandardTime():
        print "Testing militaryTimeToStandardTime()...",
        assert(militaryTimeToStandardTime(0) == 12)
        assert(militaryTimeToStandardTime(1) == 1)
        assert(militaryTimeToStandardTime(11) == 11)
        assert(militaryTimeToStandardTime(12) == 12)
        assert(militaryTimeToStandardTime(13) == 1)
        assert(militaryTimeToStandardTime(23) == 11)
        print "Passed all tests!"

    def runMilitaryTimeToStandardTime():
        militaryTime = int(raw_input("Enter the military time (0-23):"))
        standardTime = militaryTimeToStandardTime(militaryTime)
        print "Military time:", militaryTime
        print "Standard time:", standardTime
      
    testMilitaryTimeToStandardTime() # run the robotic test function
    runMilitaryTimeToStandardTime()  # let the user use the function


    Sample Solution:

    # Sample Solution is in white text (select to view)
    ##########################################
    def militaryTimeToStandardTime(militaryTime):
        return 1 + (militaryTime-1) % 12
    ##########################################


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