15-110 Fall 2010 Quiz 3
10 Minutes

* No calculators, no notes, no books, no computers.
* Show your work. Correct answers without supporting calculations will not receive full credit.

1. (30 points) The function sum1toN(n) is supposed to return the sum of all the numbers from 1 to n (inclusive). For example, sum1toN(3) should return 6. What's wrong with each of the following versions? Note: There are no syntax errors.

A.
    def wrongSum1toN_A(n):
        mySum = 0
        x = 1
        while (x < n):
            mySum += x
            x += 1
        return mySum

B.
    def wrongSum1toN_B(n):

        mySum = 0
        x = 1
        while (x <= n):
            mySum += n
            x += 1
        return mySum

2. (20 points) How many times will “hey” be printed by each of these code fragments?

A.
    i = 10

    n = 10
    while (2 * i > n):
        print "hey"
        i -= 1

B.
    i = 3

    n = 10
    while (i > 2 and n > 10):
        print "hey"
        i += 1

3. (30 points) Using a while loop, write a function boundingPowerOfTwo(n) that takes a positive
integer n and returns the exponent of the smallest power of two that is greater or equal to n. For example,
boundingPowerOfTwo(1000) will return 10, because 2**9 = 512 < 1000 <= 2**10 = 1024. Do not
use any function from the math module.

4. (20 points) Write a function isWeekDay(day) that takes an integer number representing a day of the
week (Sunday = 1 through Saturday = 7) and returns True if it's Monday through Friday and False if
it's not.

5. Bonus/Optional (5 points). What does the function mystery(n, m) compute? Explain concisely (no
more than one sentence) in terms of what the function is supposed to accomplish, not mechanically line
by line.

    def mystery(n, m):

        while (n > 1):
            if (n % m != 0) :
                return False
            n /= m
        return True