LP0

Version C

DirectionsDownload the shell file on the left. Once class begins, I will display it on this page as well.

It is a smart idea to test your code as you go but don't get too hung up on it. You can copy functions to another file so you can run them by themselves and minimize output. However, allow a couple of minutes to assemble the solutions back into the shell file. To submit, you will upload this file to Canvas.

It is smart strategy to get the first two questions first. They are "basic competency" questions and are pretty easy. The middle two deonstrate good competence. The fifth one has some tricky elements, and the sixth is quite challenging.

Most students solve 3-5 of the problems.

The file will be accessible five minutes prior to the start of class so you can start work immediately.

Allowable and Non-Allowable Items


from rt import run_test, run_test_float, close_enough

# ##################### Problem 1 #########################
def describe_string(s):
    """prec:  s is a string
    postc:  returns "LONG" if the string has 10 or
    more characters and "SHORT" otherwise."""
    return "COW"
# ##################### Problem 2 #########################
def add_evens(nums):
    """prec: nums is a list of integers.  
    postc:  returns the sum of the EVEN numbers in numss."""
    return 0
# ##################### Problem 3 #########################
def opposite_letter(ch):
    """prec:  ch is a one-character string containing a letter.
    postc: returns the "opposite" letter in the alphabet
    in lower case:
    A -> z
    a -> z
    B -> y
    b -> y
    C -> x 
    c -> x 
    etc."""
    return "Q"
# ##################### Problem 4 #########################
def zipper_strings(a, b):
    """prec: a, b are strings.
    post:  returns a string that, starting with a,
    alternates characters of a and b.  When one list is
    exhausted, the rest of the other is tacked onto the end."""
    out = ""
    return out
# ##################### Problem 5 #########################
def maximum_product(lists):
    """prec: lists is a nonempty list of numerical lists.
    Note that one of the numerical lists could be empty and 
    that the product of an empty list is 1.
    postc:  returns THE LIST with the largest product.
    In the event of a tie, it returns the first such list."""
    return []
# ##################### Problem 6 #########################
def nuggets(limit, nugget_list):
    """prec: limit is an nonnegative integer
    postc: nugget_list is a list of nonnegative integers
    returns a list from nugget_list that has the
    highest total without exceeding the limit. """
    return []
print("************* Tests for Problem 1 **************")
run_test(describe_string, "LONG", ["antidisestablishmentarianism"])
print("************* Tests for Problem 2 **************")
run_test(add_evens, 30, [[1,4,7,9,10,16]])
print("************* Tests for Problem 3 **************")
run_test(opposite_letter, "z", ["a"])
print("************* Tests for Problem 4 **************")
run_test(zipper_strings, "cdaotggie", ["cat", "doggie"])
print("************* Tests for Problem 5 **************")
run_test(maximum_product, [6,6,6], [[[1,2], [5,6,7], [3, 0], [6,6,6]]])
print("************* Tests for Problem 6 **************")
run_test(nuggets, [4, 16], [20, [1, 2, 4, 8, 16]])