15-112 Spring 2013 Homework 1
Due Monday, 21-Jan, at 9pm

Read these instructions first!

Hint #1:  You may find the round function helpful at times.  For example, round(2.4) returns 2.0, and round(2.5) returns 3.0.

Hint #2:  For one of the problems in the autograded portion, the following may be very helpful:

def ceiling(n):
    # We'll require that n be positive since this only works in that case.
    assert(n >= 0)
    # This uses "boolean arithmetic", which is not recommended, but
    # we are using it here as a workaround for not having conditionals yet
    return int(n) + (n % 1 > 0)


  1. Manually-Graded Portion:  Reasoning Over Code [25 pts; 4 pts each, 1 free pt!]
    Submit this in person to your CA, as per the directions above.  For each of the following functions, find values of the arguments that will make the function return True.  Show your work.  Hint: reason your way through this. Consider only the possible inputs. For example, if you are given that x+y==16 and x>y>0, then we only need consider (15,1), (14,2), ..., (9,7). That's only 7 (x,y) points in total, one of which must be correct! Also, CIRCLE YOUR ANSWERS!  And SHOW YOUR WORK!!!  When grading, your CA will ask you to explain some of your answers!
    def f1(x, y):
        # This problem was solved in recitation!
        assert(type(x) == type(y) == int)
        z = 10+x*y
        # note: almostEqual is as defined in the class notes
        return (100>x>y>0) and (x-y<10) and almostEqual(z%(z-5),z**0.5)
    
    def f2(x):
        # This problem was also solved in recitation!
        return ((type(x) != type(int(x))) and
                ((x % 1)**2 == int(x % 1)) and
                (100 > x**2 > 8*x > 0))
    
    def f3(x, y):
        return (100>x>y>0) and (x+y == 10) and (y/x == x/y - 1)
    
    def f4(x, y):
        return ((100>x>y>0) and (x+x**y == 100))
    
    def f5(x, y):
        return ((100>x>y>0) and
                (x+y == 16) and
                (round(float(x/y)) != round(float(x)/y)))
    
    def f6(x):
        assert ((type(x) == int) and
                (1000 > x > 0))
        y = x%10*100 + x/10%10*10 + x/100
        return ((x == y) and
                (int(x**0.5) == 11) and
                (x / 125) > (x / 135))
  2. Autograded Portion:  Problem-Solving (Writing functions) [75 pts; 7.5 pts each]
    Start by downloading this file:  hw1.py.  Edit that file and submit that edited file to Autolab.
    Submit this via Autolab (you will practice this in recitation this week)
    Remember:  No loops, no conditionals, no strings, no imports, no recursion.
    Be sure to write reasonable test functions for every function!
     
    1. kthDigit(x, k)
      Given two integers, x and k, return the kth digit of x, counting from the right.  So:
           kthDigit(789, 0) returns 9
           kthDigit(789, 1) returns 8
           kthDigit(789, 2) returns 7
           kthDigit(789, 3) returns 0
      Negative numbers should work, too, so:
           kthDigit(-789, 0) returns 9

       
    2. numberOfPoolBalls(rows)
      Pool balls are arranged in rows where the first row contains 1 pool ball and each row contains 1 more pool ball than the previous row.  Thus, if there are 3 rows, then we'd have 6 total pool balls (1+2+3).  Write a function that takes an int value, the number of rows, and returns another int value, the number of pool balls in that number of rows.  For example, numberOfPoolBalls(3) returns 6.  Do not use loops.
       
    3. numberOfPoolBallRows(balls)
      This problem is the inverse of the previous problem.  In this case, return the number of rows required for the given number of pool balls.  Thus, numberOfPoolBallRows(6) returns 3.   Note that if any balls must be in a row, then you count that row, and so numberOfPoolBallRows(7) returns 4 (since the 4th row must have a single ball in it).
       
    4. isEvenPositiveInt(x)
      Given an arbitrary value x, return True if it is an integer, and it is positive, and it is even (all 3 must be True), or False otherwise.  Do not crash if the value is not an integer.  So, isEvenPositiveInt("yikes!") returns False (rather than crashing), and isEvenPositiveInt(123456) returns True.
       
    5. isPerfectCube(x)
      Given an integer value x, returns True if it is a perfect cube and False otherwise.  That is, return True if there is another integer y such that x = y3.  Thus, isPerfectCube(27) returns True, but isPerfectCube(16) returns False.
       
    6. isTriangular(x)
      A number is triangular if it equals 1+2+3+...+N for some integer N.  Given an integer value x, return True if it is triangular, and False otherwise.  Note that this relates to the pool ball problems above.
       
    7. fabricYards(inches)
      Fabric must be purchased in whole yards.  Write a function that takes a non-negative number of inches of fabric desired, and returns the smallest number of whole yards of fabric that must be purchased.  Thus, fabricYards(1) is 1 (you need a full yard if you buy one inch) and fabricYards(36) is also 1, but fabricYards(37) is 2.
       
    8. fabricExcess(inches)
      Write a function that takes a non-negative number of inches of fabric desired and returns the number of inches of excess fabric that must be purchased (as purchases must be in whole yards).  Thus, since you need a whole yard when you buy 1 inch, fabricExcess(1) is 35.  Similarly, fabricExcess(36) is 0, and fabricExcess(37) is 35.
      Hint: there are (at least) two good ways to write this.  One way involves a simple expression using one of the math operators we have learned.  The other way uses fabricYards (which you just wrote!).  To restate:  one way uses div (/) and the other way uses mod (%).   You should understand both ways!!!
       
    9. nearestBusStop(street)
      Write a function that takes a non-negative street number, and returns the nearest bus stop to the given street, where buses stop every 8th street, including street 0, and ties go to the lower street, so the nearest bus stop to 12th street is 8th street, and the nearest bus stop to 13 street is 16th street.
       
    10. areCollinear(x1, y1, x2, y2, x3, y3)
      Write a function that takes six integer values representing three points (x1,y1), (x2,y2), and (x3,y3), and returns True if those points all lie on a single line, and False otherwise.  Don't crash when checking for a vertical line!

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