Introduction to Computer Science: The Way of the Program — Homework 13

Due by 11:59pm Tuesday, December 12

Reading


Programming Exercises

  1. Finish all of your recursive function definitions from Lab 13.


EXTRA CREDIT PROBLEMS (OPTIONAL)

  1. Write a recursive function ruler(n) that prints out a "vertical ruler" pattern of dashes, where n is an integer > 0. The middle row of the pattern should be a row of n dashes, with a row of n-1 dashes in the middle of the top and bottom halves of the ruler. The ruler should continue to be subdivided until the smallest section is labeled by a single dash. Some examples are shown below:

    >>> ruler(1)
    -
    
    >>> ruler(2)
    -
    --
    -
    
    >>> ruler(3)
    -
    --
    -
    ---
    -
    --
    -
    
    >>> ruler(4)
    -
    --
    -
    ---
    -
    --
    -
    ----
    -
    --
    -
    ---
    -
    --
    -
    
  2. In blackjack, the face cards Jack, Queen, and King are worth 10 points each, and an Ace can be worth either 1 or 11, depending on circumstances. The other non-face cards are each worth their rank (2 through 10). Write a program called worth(cards) that takes as input a list of cards representing a hand, and returns a list of all possible values for the hand, with Aces counted as either 1 or 11. Each card is represented either as a string "Jack", "Queen", "King", "Ace", or as a number from 2 to 10. The list of values returned should not contain duplicates, and the order of the values does not matter. Some examples are shown below. Notice that 0 is the only possible value for an empty hand. Hint: use a recursive approach similar to the programs subsets and binaryStrings.

    >>> worth([8, "King"])
    [18]
    >>> worth(["Jack", "Ace"])
    [11, 21]
    >>> worth(["Ace", "Ace", "Ace"])
    [3, 13, 23, 33]
    >>> worth(["Ace", "King", 5, 2, "Ace", "Ace"])
    [20, 30, 40, 50]
    >>> worth([])
    [0]
    
    

Turning in Your Homework