15-112 Spring 2013 Practice Quiz 3

* No calculators, no notes, no books, no computers.
SHOW YOUR WORK, CIRCLE YOUR ANSWERS.

  1. Quick Answers.  Be very brief.

    1. Write a line of code that fails precisely because strings are immutable.

    2. Explain the difference between using a single-quoted (") string and a triple-quoted (""") string.

    3. For int values n where (255 >= n >= 0), is it always true that ord(chr(n)) == n?

    4. To convert a string s to an int, you could do eval(s) or int(s). Which one is better and why?

    5. For a string s, what is the difference between s.lower() and s.islower()?

    6. For a string s, what does s.isalpha() compute?

    7. What does ("abc%de%7.2fg" % (12, 34.567)) evaluate to?

    8. What is a magic number?  Why are they ill-advised?

    9. Very briefly, what are each of Polya's Four Steps, specifically as they relate to programming?

    10. What are some good hints to help you "write your solution so it is amenable to translating into code"?
       

  2. Code Tracing

    Indicate what each will print:

    import string
    def f(x, s):
        while (len(s[x:-x]) > 0):
            s = s[x:-x:max(1,len(s)/5)]
            x += 1
            print x, s
    print f(1, string.ascii_letters)
    def g(s, t):
        u = ""
        for i in xrange(len(s)):
            for c in t:
                if (ord(s[i]) == 1+ord(c)):
                    print i, s[i], c
                    u += s[i]
        return u
    print g("abcde", "cat")
  3. Reasoning Over Code

    Find arguments for the following functions that make them return True.

    def f(x, y, z):
        assert((y - x) % z == 0)
        s = ""
        for n in xrange(x, y, z):
            offset = (n - ord('a')) % 26
            s += chr(ord('A') + offset)
        return (s == "VAF")
    
    def g(s):
        t = ""
        while (len(s) > 0):
            t += s[0]
            s = s[1:][::-1]
        return (t == string.digits)
  4. Free Response

    Here you would be expected to write any of the functions from hw3, or from practice-thru-week3, or any variant of those, or anything similarly challenging.  For example:

    sameChars
    Write the function sameChars(s1, s2) that takes two strings and returns True if the two strings are composed of the same characters (though perhaps in different numbers and in different orders) -- that is, if every character that is in the first string, is in the second, and vice versa -- and False otherwise. This test is case-sensitive, so "ABC" and "abc" do not contain the same characters. The function returns False if either parameter is not a string, but returns True if both strings are empty (why?).

    hasBalancedParentheses
    Write the function hasBalancedParentheses, which takes a string and returns True if that code has balanced parentheses and False otherwise (ignoring all non-parentheses in the string).  We say that parentheses are balanced if each right parenthesis closes (matches) an open (unmatched) left parenthesis, and no left parentheses are left unclosed (unmatched) at the end of the text.  So, for example, "( ( ( ) ( ) ) ( ) )" is balanced, but "( ) )" is not balanced, and "( ) ) (" is also not balanced.

    inverseF
    Consider the function f(x) = 3x - 2x.  Write the function inverseF(y), that takes a positive floating-point value y, and finds and returns the value x such that f(x) == y.  As this is approximate, your answer needs to be within 0.001 of the actual answer.  For example, f(2) = 32 - 22 = 9 - 4 = 5.  Thus, since f(2)==5, inverseF(5)==2.  How to do this?  Using bisection, though you may have to slightly adapt it.

    Hint:  in the homework, we used bisection to find zeros.  We can adjust this problem easily to search for a zero.  How?  Well, say:
         y = 3x - 2x
    Then we also have:
         3x - 2x - y = 0
    Hopefully that's enough to get you started.  Good luck!