15-112 Spring 2013 Practice Quiz 1

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

  1. Quick Answers.

    For positive int values x, y, and z:

    1. For any fixed value of y, what is the largest value of (x%y) where (x < y)?  where (x > y)?  where (x == y)?

    2. Is it true that (x % y % y) always equals (x % y)?  Why (or why not)?

    3. Under what conditions is this true:   x/y*y < x

    4. Give an expression equivalent to (x % y) that does not use the % operator.
       

  2. Code Tracing

    Indicate what each will print:

    def f(x):
        print "f", x
        return 2*x
    
    def g(x,y,z):
        print "g", x, y, z
        y += f(x+f(z))
        print "g", x, y, z
    
    print g(2, f(2), 6)
    ############################################
    
    def h(x,y):
        print x/y, y/x, (x/y or y/x or 42)
    h(5, 23)
  3. Reasoning Over Code

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

    def f(x,y):
        return ((type(x) == type(y) == int) and
                (100 > x > y > 0) and
                (x + y > 50) and
                (x == y+y*y**y))
    ############################################
    
    def g(x):
        a = int(x)
        b = int(round(10*(x%1)))
        print a,b
        return ((type(x) == type(x*1.0)) and
                (a != b) and
                (a == 4*b) and
                (10 >= a+b > 5))
  4. Free Response

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

    fourDigits(x,y)
    Write a function that takes two values which may or may not be numbers, and returns True if the two values are both integers, both between 0 and 99 (inclusive), and such that no digit occurs more than once in the two numbers (including leading zeros).  Your function may not crash even on non-integer inputs.