Introduction to Computer Programming: Lab 2

Read Evaluate Print Loops (REPLs)



  1. Complete digit_to_letter(d) so that it returns a single-character string representing d (assumed to be a number >= 0 and < 10) ranging from 'A' for 0 through 'J' for 9. If d is not a single digit value then it should return '!'. Examples:

    >>> digit_to_letter(0)
    >>> digit_to_letter(9)
    >>> digit_to_letter(37)
  2. Fix encode(n) so that it correctly returns a string representing n where each digit is an uppercase letter between A (0) and J (9). Call digit_to_letter. Examples:

    >>> encode(0)
    >>> encode(914395)
  3. Complete random_string(n) so that it returns a random lowercase string of n letters (between a and j). Call random.randrange and digit_to_letter.

  4. Do some research on leap years. You know that 2000, 2004, 2008, and 2012 are all leap years and none of the years in between are. But did you know that 1900 is not a leap year? Write two equivalent functions that return True if the year is a leap year and False otherwise.

    1. Complete is_leap_multiway(year) so it uses a multiway-conditional statement (if/elif/else) and does not use logical operators (and, or). Examples:

      >>> is_leap_multiway(2015)
      >>> is_leap_multiway(2016)
      >>> is_leap_multiway(1900)
      >>> is_leap_multiway(2000)
    2. Complete is_leap(year) so it consists of a single return statement. It should make use of the logical operators (and, or, not). Examples:

      >>> is_leap(2015)
      >>> is_leap(2016)
      >>> is_leap(1900)
      >>> is_leap(2000)

  5. This exercise imagines a world with only the three primary colors (red, yellow and blue) and an encoding between those colors and the numbers 0, 1 and 2:

        0   'red'
        1   'yellow'
        2   'blue'
    1. Complete color_to_code(color) so it returns the numeric coding for the color assuming it is the string 'red', 'yellow' or 'blue'. Otherwise it returns None. Examples:

      >>> color_to_code('red')
      >>> color_to_code('yellow')
      >>> color_to_code('blue')
      >>> color_to_code('white') is None
    2. Complete code_to_color(code) so it returns the color string corresponding to the numerical code. If the code is not 0, 1 or 2 it returns 'black'. Examples:

      >>> code_to_color(0)
      >>> code_to_color(1)
      >>> code_to_color(2)
      >>> code_to_color(7)
    3. Complete next_color(color) so it returns the next color in the color sequence red, yellow, blue. The next color after blue should be red again. You may assume that color has the value 'red', 'yellow' or 'blue'. Examples:

      >>> next_color('red')
      >>> next_color('yellow')
      >>> next_color('blue')

      Hint: you can accomplish this with a single return statement using composition, the two previous functions, and the remainder operator (%).

  6. Fix sum_digits(n) so it that is correctly returns the sum of the digits of n. Examples:

        >>> sum_digits(0)
        >>> sum_digits(8)
        >>> sum_digits(55)    # 5 + 5
        >>> sum_digits(205)   # 2 + 0 + 5
  7. The sum of the digits of a number can be used as a way of verifying that a number received via electronic communication has been transmitted correctly. For example, if we wish to send the number 213, we could send 2136 using the last digit as a checksum (or check digit) that the recipient can use to verify that the first three digits were received correctly.

    We can use the sum_digits function to compute this checksum, except that we want the checksum to be only one digit long. For example, for the number 815, the sum of the digits is 14, but the one-digit checksum is 5 since it is the sum of the digits 1 and 4.

    Complete compute_check_digit(n) so it returns a one-digit checksum corresponding to that number. (I recommend making your function repeatedly call sum_digits until the result is a single digit.) Examples:

       >>> compute_check_digit(0)
       >>> compute_check_digit(55)   # 5 + 5 = 10  -->  1 + 0 = 1
       >>> compute_check_digit(999999999999)   # 9 * 12 = 108 ...
       >>> 99999999999999999999991   # 199 --> 19 --> 10 --> 1
  8. Complete verify_code(n) so that it returns True if the last digit of a number is the check digit of the preceeding digits in n. If not, or if n has fewer than 2 digits, returns False. You can assume n is positive. Call compute_check_digit. Examples:

    >>> verify_code(22)
    >>> verify_code(551)
    >>> verify_code(999999999999999999999911)
    >>> verify_code(9)
    >>> verify_code(1237)

  9. Complete tally_random(n) so that it returns the number of times that n random numbers generated between 0 (inclusive) and 10 (exclusive) are either 0 or 9. Make use of random.randrange. If working correctly, for larger values of n, the returned value should be close to 20% of n.

  10. Complete string_kind() so that running it produces a REPL that matches the behavior of the supplied lab2x.string_kind(). Make use of isdigit, islower, and isupper.

  11. Try using the function human_guesses(). It is a very simple guessing game and not well implemented at that. Modify that game so that the computer "thinks" of a different number each time (hint: use random.randrange). Add code so that when the player finally guesses the correct value, the total number of guesses is reported. Add code so that if the player guesses 0 it tells the computer to quit the game rather than to keep playing. Here are two possible transcripts of a correctly functioning game:

    >>> human_guesses()
    enter your first guess: 9
    enter your next guess: 6
    enter your next guess: 3
    enter your next guess: 7
    you got it in 4 guesses
    >>> human_guesses()
    enter your first guess: 5
    enter your next guess: 0
    you quit
  12. Try using the function computer_guesses(). Again, it plays a very simple guessing game and, again, it is not fully functional as given. This time around, the computer tries to guess a number that the human is thinking of. Modify the game so that it works. Add code so that the computer can tell if the human user cheats (i.e., if the computer tries all the numbers between 1 and 10 and none are the answer it can suspect the human is not playing by the rules). Here are two possible transcripts of a correctly functioning game:

    >>> computer_guesses()
    is it 1? n
    is it 2? n
    is it 3? n
    is it 4? y
    i got it!
    >>> computer_guesses()
    is it 1? n
    is it 2? n
    is it 3? n
    is it 4? n
    is it 5? n
    is it 6? n
    is it 7? n
    is it 8? n
    is it 9? n
    is it 10? n
    you cheated!