CS 21: Algorithmic Problem Solving

 

HW #6: Warmup Questions

  1. Write a function which returns the number of times that it takes for a pair of (randomly) rolled dice to sum to 7.
  2. Modify the previous question so that the function now takes a parameter n which represents the sum you are trying to roll (which, in the previous question was always 7). Add a check at the start of the function to be sure that the user has provided a value between 2 and 36. This check will prevent an infinite loop if the user enters an out-of-range number.
  3. Write a function ulam that takes a parameter n and returns n/2 if n is even, and 3n+1 if n is odd.
  4. Write a function called hailstone that takes a parameter n and repeatedly calls n = ulam(n) until n equals 1.
  5. Create a list of 10 numbers and ask the user to select one of those 10 values using an input statement. Using a combination of while, if and try/except statements, continue to ask the user for a value until the enter a valid value. When they do, print out the appropriate item from the list.
  6. Here is the start of a function called safeOrd:
    
    def safeOrd(char):
         val = ord(char)
         return val
    
    Unfortunately, this function will crash whenever the user passes values that are inappropriate for the ord function. Using try and except, modify this function so that if char causes an error, the function returns 0; otherwise, it returns val.