CMU 15-112 Summer 2020: Fundamentals of Programming and Computer Science
Homework 1 (Due Tue 19-May, at 5pm)



Important note about spiciness levels!
Note: this homework has 2 levels: Medium and Spicy.
50 points of hw1 come from collab1. 20 points of hw1 come from the CT and ROC practice.
This leaves 30 points to gain from either getKthDigit or colorBlender. Choose the problem at the level that is most appropriate for you.


  1. CT & ROC Practice [20pts - Required]
    Download day1-ct-rc-runner.zip, unzip it, and run go.py
    Log in with the username and password you received from us.
    For full credit, you must complete a CT problem in under 5 minutes and an ROC problem in under 5 minutes.
    For every minute it takes you to complete the problem over 5 minutes you will lose 20% of the score for that problem.
    Only your best score is counted.
    You have unlimited attempts to complete each type of problem in the time limit.
    Important: To receive any credit, you must quickly upload a picture of your scrap work and your submission confirmation to this form after getting a correct answer.
    Your picture should look something like this. Notice that clearly visible in this image are:
    1. Your scrap work and answer
    2. The CT RESULTS table including the problem statement, correct answer, and your answer

  2. getKthDigit(n, k) [30pts - Medium]
    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

  3. colorBlender(rgb1, rgb2, midpoints, n) [30pts - Spicy]
    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):

    color0: rgb(220, 20, 60) color1: rgb(212, 78, 95) color2: rgb(205, 136, 131) color3: rgb(197, 194, 166) color4: 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

    Hints: RGB values must be ints, not floats. Also, remember to use roundHalfUp(n) instead of round(n) when calculating midpoint colors.