CMU 15-112 Spring 2017: Fundamentals of Programming and Computer Science
Lab 1 (Due Thursday 31-Aug, at 10pm)


This lab has 2 required forms -- one to make groups for this week and one to peer-review each others' groupwork (in terms of being great groupmates, not in terms of getting right answers).

Reminder: do not work on these problems alone -- only work on them together with your lab partners!

  1. nearestOdd(n)
    Write the function nearestOdd(n) that takes an int or float n, and returns as an int value the nearest odd number to n. In the case of a tie, return the smaller odd value.

  2. rectanglesOverlap(x1, y1, w1, h1, x2, y2, w2, h2)
    A rectangle can be described by its left, top, width, and height. This function takes two rectangles described this way, and returns True if the rectangles overlap at all (even if just at a point), and False otherwise. Note: here we will represent coordinates the way they are usually represented in computer graphics, where (0,0) is at the left-top corner of the screen, and while the x-coordinate goes up while you head right, the y-coordinate goes up while you head down. Yes, up is down! This is quite common in computer graphics, and is how Tkinter and Brython in particular both work. Check out the examples in the test code we provided to see this in action. Up is down. Weird, but true.

  3. isPerfectSquare(n)
    Write the function isPerfectSquare(n) that takes a possibly-non-int value, and returns True if it is an int that is a perfect square (that is, if there exists an integer m such that m**2 == n), and False otherwise. Do not crash on non-ints nor on negative ints.

  4. getKthDigit(n, k)
    Write the function getKthDigit(n, k) that takes a possibly-negative int n and a non-negative int k, and returns the kth digit of n, starting from 0, counting from the right. So:
       getKthDigit(789, 0) returns 9
       getKthDigit(789, 2) returns 7
       getKthDigit(789, 3) returns 0
       getKthDigit(-789, 0) returns 9

  5. setKthDigit(n, k, d)
    Write the function setKthDigit(n, k, d) that takes three integers -- n, k, and d -- where n is a possibly-negative int, k is a non-negative int, and d is a non-negative single digit (between 0 and 9 inclusive), and returns the number n but with the kth digit replaced with d. Counting starts at 0 and goes right-to-left, so the 0th digit is the rightmost digit. For example:
       setKthDigit(468, 0, 1) returns 461
       setKthDigit(468, 1, 1) returns 418
       setKthDigit(468, 2, 1) returns 168
       setKthDigit(468, 3, 1) returns 1468

  6. colorBlender(rgb1, rgb2, midpoints, n)
    This problem implements a color blender, inspired by this tool. In particular, we will use it with integer RGB values (it also does hex values and RGB% values, but we will not use those modes). Note that RGB values contain 3 integers, each between 0 and 255, representing the amount of red, green, and blue respectively in the given color, where 255 is "entirely on" and 0 is "entirely off".

    For example, consider this case. Here, we are combining crimson (rgb(220, 20, 60)) and mint (rgb(189, 252, 201)), using 3 midpoints, to produce this palette (using our own numbering convention for the colors, starting from 0, as the tool does not number them):
    • color #0: rgb(220,20,60)
    • color #1: rgb(212,78,95)
    • color #2: rgb(205,136,131)
    • color #3: rgb(197,194,166)
    • color #4: rgb(189,252,201)
    There are 5 colors in the palette because the first color is crimson, the last color is mint, and the middle 3 colors are equally spaced between them.

    So we could ask: if we start with crimson and go to mint, with 3 midpoints, what is color #1? The answer then would be rgb(212,78,95).

    One last step: we need to represent these rgb values as a single integer. To do that, we'll use the first 3 digits for red, the next 3 for green, the last 3 for blue, all in base 10 (decimal, as you are accustomed to). Hence, we'll represent crimson as the integer 220020060, and mint as the integer 189252201.

    With all that in mind, write the function colorBlender(rgb1, rgb2, midpoints, n), which takes two integers representing colors encoded as just described, a non-negative integer number of midpoints, and a non-negative integer n, and returns the nth color in the palette that the tool creates between those two colors with that many midpoints. If n is out of range (too small or too large), return None.

    For example, following the case above:
      colorBlender(220020060, 189252201, 3, 1) returns 212078095
    You can find some other test cases in the test function provided for you in lab1.py.