15-112 Fall 2013 Quiz 3

* Note that there were 2 very similar versions of this quiz.  Each is included here.

* 25 Minutes.  No calculators, no notes, no books, no computers.  * Show your work. Circle your answers.

1.    Quick Answers. [20 pts]  Be very brief.  A few well-chosen words are better than full sentences.

a.    Very briefly, give two distinct advantages of top-down design using lots of small helper functions.

b.    Very briefly, give one reason why magic numbers can lead to more bugs in your code.

c.    Very briefly, explain how excessive use of an autograder may decrease your learning in this course.

d.      Assuming the variable c holds a string of length one,  two of the following four expressions are equivalent to each other.  Circle both of them and briefly describe all the values of c for which those two equivalent expressions will return True.

c.upper().isupper()            (ord('A') <= ord(c) <= ord('a')+25)

(c in string.ascii_letters)    ('A' <= c <= 'Z')

2.    Code Tracing [20 pts]  Indicate what each will print:

def f(s):

    for x in xrange(1,4):

        spec = "%%0.%df" % x

        print spec % float(s)

f("12.45645")

 

def g(s):

    result = ""

    for i in xrange(len(s)):

        for j in xrange(i):

            if (s[j] > s[i]):

                result += s[j] + s[i]

    return result

print g("aebdc")
 

3.    Reasoning Over Code [10 pts]
Find arguments for the following function that makes it return True.

def h(s, t):

    assert((len(t) == 2) and ("0" not in t) and (t[1] > t[0]))

    count = 0

    for r in s.split(","):

        assert(r == t)

        count += 1

    return (count == int(t[0]) + int(t[1]))
 

4.    Free Response [50 pts]
largestNumber:
  Write the function largestNumber(text) that takes a string of text and returns the largest int value that occurs within that text, or None if no such value occurs.  You may assume that the only numbers in the text are non-negative integers and that numbers are always composed of consecutive digits (without commas, for example).  For example:
    largestNumber("I saw 3 dogs, 17 cats, and 14 cows!") returns 17 (the int value 17, not the string "17").
and
    largestNumber("One person ate two hot dogs!") returns None (the value None, not the string "None").
Hint: for code that does not quite work, you may get more partial credit if you include enough concise comments so we can see what you at least were trying to do.
 

5.  Bonus/Optional: [5 pts]  Find arguments for the following function that makes it return True.
def f(x, y=0):
    def g(x):
        total = x+x-x*x
        for y in xrange(x):
            for z in xrange(y): total += 4
        return total
    while (g(y) < x): y += 1
    return (x+y == 50)


 

15-112 Fall 2013 Quiz 3

* 25 Minutes.  No calculators, no notes, no books, no computers.  * Show your work. Circle your answers.

1.    Quick Answers. [20 pts]  Be very brief.  A few well-chosen words are better than full sentences.

a.    Assuming the variable c holds a string of length one,  two of the following four expressions are equivalent to each other.  Circle both of them and briefly describe all the values of c for which those two equivalent expressions will return True.

(c in string.ascii_letters)              ('A' <= c <= 'Z')
 
(ord('A') <= ord(c) <= ord('a')+25)       c.upper().isupper()
 

b.    Very briefly, give one reason why magic numbers can lead to more bugs in your code.
 

c.    Very briefly, give two distinct advantages of top-down design using lots of small helper functions.
 

d.    Very briefly, explain how excessive use of an autograder may decrease your learning in this course.
 

2.    Code Tracing [20 pts]  Indicate what each will print:

def f(s):

    for x in xrange(1,4):

        spec = "%%0.%df" % x

        print spec % float(s)

f("42.36827")

 

def g(s):

    result = ""

    for i in xrange(len(s)):

        for j in xrange(i):

            if (s[j] > s[i]):

                result += s[j] + s[i]

    return result

print g("bfced")
 

3.    Reasoning Over Code [10 pts]
Find arguments for the following function that makes it return True.

def h(s, t):

    assert((len(t) == 2) and ("0" not in t) and (t[0] > t[1]))

    count = 0

    for r in s.split(","):

        assert(r == t)

        count += 1

    return (count == int(t[0]) + int(t[1]))
 

4.    Free Response [50 pts]
largestNumber:
  Write the function largestNumber(text) that takes a string of text and returns the largest int value that occurs within that text, or None if no such value occurs.  You may assume that the only numbers in the text are non-negative integers and that numbers are always composed of consecutive digits (without commas, for example).  For example:
    largestNumber("I saw 3 dogs, 17 cats, and 14 cows!") returns 17 (the int value 17, not the string "17").
and
    largestNumber("One person ate two hot dogs!") returns None (the value None, not the string "None").
Hint: for code that does not quite work, you may get more partial credit if you include enough concise comments so we can see what you at least were trying to do.
 

5.  Bonus/Optional: [5 pts]  Find arguments for the following function that makes it return True.
def f(x, y=0):
    def g(x):
        total = x+x-x*x
        for y in xrange(x):
            for z in xrange(y): total += 4
        return total
    while (g(y) < x): y += 1
    return (x+y == 60)