Practice LP0
Here is the shell code. It runs out of the box.
from rt import run_test, run_test_float, close_enough
######################Problem 1###########################
def trim_sum(nums, limit):
"""nums is a numerical list, limit is a number
returns the sum of all elements in nums that are
<= limit"""
out = 0
return out
######################Problem 2###########################
def filter_sum(s):
"""prec: s is a string
postc: the total of all digits in the string is returned."""
out = 0
return out
######################Problem 3###########################
def sort_letters_backward(word):
"""prec: word is a lower-case string
postc: returns a string with the letters in word sorted in reverse
alphabetical order
"""
return ""
######################Problem 4###########################
def comb_nest(*x):
"""prec: x is a star argument; you pass in a comma separated
list of lists.
postc: returns a list containing the sum of each of
the nested lists inside of x.
"""
return []
######################Problem 5###########################
def paranoid_number(n):
"""prec: n is a nonnegative integer
postc: returns the nth paranoid number
paranoid_number(0) = 1
paranoid_number(1) = 3
paranoid_number(n+2) = 5*paranoid_number(n-1) - 6*paranoid_number(n)
"""
return 0
######################Problem 6###########################
def generate_anagrams(word):
"""prec: word is a string with distinct letters
postc: generates a list containing all anagrams of
word in alphabetical order. """
return [""]
def main():
## do your testing here.
print("*************** Problem 1 Tests **************")
run_test(trim_sum, 13, [[5, 12, 11, 2, 6], 10])
print("*************** Problem 2 Tests **************")
run_test(filter_sum, 10, ["c1o2w3s4moo"])
print("*************** Problem 3 Tests **************")
run_test(sort_letters_backward, "rmmhea", ["hammer"])
print("*************** Problem 4 Tests **************")
run_test(comb_nest, [6, 15, 15], [[1,2,3], [7, 8], [10,3,2]])
print("*************** Problem 4 Tests **************")
print("*************** Problem 5 Tests **************")
print(run_test(paranoid_number, 1, [0]))
print(run_test(paranoid_number, 3, [1]))
print("*************** Problem 6 Tests **************")
run_test(generate_anagrams, ['act', 'atc', 'cat', 'cta', 'tac', 'tca'], ["cat"])
main()
NEW VERSION of rt.py
Thanks, Chris Agrella!
# ###########################################################
# Authors: Morrison with revision by Chris Agrella
# Date created: 2021-08-30
# Date last modified: 2021-09-17
# FREE CODE FOR WRITING TESTS
# Usage for a non-float return value
# run_test(function_name, expected_output, [arg1, arg2, .... ])
# Usage for a float return value
# run_test_float(function_name, expected_output, [arg1, arg2, .... ])
# ############################################################
TOLERANCE = 1e-6
def close_enough(x, y):
return abs(x - y) < TOLERANCE
def run_test(function, expected, args):
# print(f"args = {args}")
if len(args) == 1:
# print("made it into the if statement")
args = args[0]
# print(f"args = {args}")
if function(args) == expected:
print(f"PASS for case {args} ({function(args)})")
else:
print(
f"FAIL because f({args}) != {expected}. Failed Output: {function(args)}"
)
else:
if function(*args) == expected:
print(f"PASS for case {args} ({function(*args)})")
else:
print(
f"FAIL because f({args}) != {expected}. Failed Output: {function(*args)}"
)
def run_test_float(function, expected, args):
if type(args) == list and len(args) == 1:
args = args[0]
if close_enough(function(args), expected):
print(f"PASS for case {args} ({function(args)})")
else:
print(
f"FAIL because f({args}) != {expected}. Failed Output: {function(args)}"
)
return
else:
if close_enough(function(*args), expected):
print(f"PASS for case {args} ({function(*args)})")
else:
print(
f"FAIL because f({args}) != {expected}. Failed Output: {function(*args)}"
)