This code can be downloaded on the left.
from rt import *
################# Problem 1 ####################"
def transform(x, f):
"""precondition: x is a list, f is a function whose domain
contains all of the list elements
postcondition: returns a new list [f(x[0]), f(x[1]),.... f(x[n])]
"""
return []
################# Problem 2 ####################"
def chop_top(x, limit):
"""precondition: x is a numerical list, limit is a number
postcondition: returns the sum of all elements in x that are
<= limit"""
return 0
################# Problem 3 ####################"
def chop_bottom(x, limit):
"""precondition: x is a numerical list, limit is a number
postcondition: returns the sum of all elements in x that are
>= limit"""
return 0
################# Problem 4 ####################"
def is_in_order(x):
"""precondition: x is a homogenous list of sortable
items (for which < and friendds are defined)
postcondition: checks to see if the items in x are in
ascending order"""
return False
################# Problem 5 ####################"
def get_even(x):
"""precondition: x is a list of integers.
postcondition: returns the sum of the even elements of x
"""
return 0
################# Problem 6 ####################"
def zippy(s, t):
"""precondition: s and t are strings.
postcondition: zips s and t together, staring with
s. Example: zippy("cat", "elephant") -> "cealtephant"
"""
return ""
################# Problem 7 ####################"
def sigma(n, f):
"""precondition: n is a nonnegative integer, f is a function
defined for all nonnegative integers that returns
a number
postcondition: returns f(0) + f(1) + ... + f(n)
"""
return 0
################# Problem 8 ####################"
def fib(n):
"""precondition, n is an integer, n >= 0.
postcondition: returns nth Fibonacci number.
Hint: use a,b = b,a
"""
return 0
print("**************** Problem 1 tests **************")
## supply test args
f = lambda x:x*x
x = list(range(10))
## wrap args in a list, even if there is only one.
test = [x, f]
## supply an expected value
expected = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
## run the test
run_test(transform, expected, test)
print("**************** Problem 2 tests **************")
x = [0, 12, 4, 51, 3, 9, 40]
limit = 20
test = [x, limit]
expected = 28
print("**************** Problem 3 tests **************")
x = [0, 12, 4, 51, 3, 9, 40]
limit = 20
test = [x, limit]
expected = 91
run_test(chop_bottom, expected, test)
print("**************** Problem 4 tests **************")
x = [1,5,7,9,11,13,21,25]
expected = True
test = [x]
run_test(is_in_order, expected, test)
print("**************** Problem 5 tests **************")
x = [2,5,7,10,24,11]
test = [x]
expected = 36
print("**************** Problem 6 tests **************")
s = "cat"
t = "elephant"
test = [s,t]
expected = "cealtephant"
run_test(zippy, expected, test)
print("**************** Problem 7 tests **************")
n = 10
f = lambda x:x*x
expected = 385
test = [n, f]
run_test(sigma, expected, test)
print("**************** Problem 8 tests **************")
n = 50
expected =12586269025
test = [n]
run_test(fib, expected, test)
print(fib(50))