This is your first function writing exercise. It includes a facility for easily testing your functions.
# Author: Morrison
# Date: 2021-08-30
# Program easyFunctions.py
# FREE CODE FOR WRITING TESTS
# Usage is demonstrated on examples below
################# DO NOT CHANGE THIS CODE ###################
def close_enough(x,y):
return abs(x - y) < 1e6
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}")
else:
print(f"FAIL because f({args}) != {expected}")
else:
if function(*args) == expected:
print(f"PASS for case {args}")
else:
print(f"FAIL because f({args}) != {expected}")
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}")
else:
print(f"FAIL because f({args}) != {expected}")
return
else:
if close_enough(function(*args), expected):
print(f"PASS for case {args}")
else:
print(f"FAIL because f({args}) != {expected}")
############### END OF CODE NOT TO CHANGE ###########################
## sample problem with tests that returns a floating-point value.
def f2c(tempF):
"""precondition: tempF is a float or an int
postc: returns the equivalent temperature on the celsius scale."""
print(tempF)
return (tempF - 32)*5/9
test = [-40] #put arguments in a list
expected = -40 #set this to expected output
run_test_float(f2c, expected, test) #run test
test = [32]
expected = 0
run_test_float(f2c, expected, test)
test = [212]
expected = 100
run_test_float(f2c, expected, test)
## sample problem with tests that returns a non-float object
def get_first_name(name):
"""prec: name is given in the form last, first, and possibly a middle name in
a single string
postc: returns first name"""
chunks = name.split()
return chunks[1]
test = ["Roberts, Todd"]
expected = "Todd"
run_test(get_first_name, expected, test)
test = ["Morrison, John M."]
expected = "John"
run_test(get_first_name, expected, test)
## Problem 0
def c2f(tempC):
"""precondition: tempC is a float or an int
postc: returns temperature in degrees farenheit
"""
return 0
print("*************** Problem 0 Tests ***************")
# Problem 1
def positive_part(x):
"""precondition: x is a float or an int
postc: returns x if x is nonnegative and 0 otherwise.
"""
return 0
print("*************** Problem 1 Tests ***************")
## Problem 2
def truncate_string(s, n):
"""prec: s is a string, n is a nonnegative integer
post: returns s if s has length at most end; otherwise,
it truncates s to its first n characters."""
return ""
print("*************** Problem 2 Tests ***************")
test = ["catamaran", 5]
expected = "catam"
run_test(truncate_string, expected, test)
## Problem 3
def xor(b1, b2):
"""prec b1 and b2 are booleans
post: returns True if EXACTLY ONE of b1 or b2 is true.
"""
return False
test = [True, True]
expected = False
run_test(xor, expected, test)
print("*************** Problem 3 Tests ***************")
##Problem 4
def chop_at(s, ch):
"""s is a string, ch is a one-character string
if ch is not present in s, just return s
if it is find the last instance of ch in s and truncate
the string there (including ch)"""
return s
print("*************** Problem 4 Tests ***************")
test = ["Todd Roberts", "e"]
expected = "ToddRob"
run_test(chop_at, expected, test)
## Problem 5
def sort_string(s):
"""prec: s is a string of alphabetical characters
post: returns s lower-cased and with its characters in alphabetical order.
"""
return s
test = ["CowABUnga"]
expected = "aabcgnouw"
run_test(sort_string, expected, test)
## Problem 6
def are_anagrams(s1, s2):
"""prec: s1 and s2 are strings
postc: return True if s1 is an anagram of s2, case INSENSITIVE."""
return False
test = ["golf", "flog"]
expected = True
run_test(are_anagrams, expected, test)
test = ["butter", "rebut"]
expected = False
run_test(are_anagrams, expected, test)
Here are some tips to keep you sane. First, use run_test
for functions that do not return a floating-point value. Use run_test_float
for functons that return a floating-point value.
How to Use the Test functions DO NOT MODIFY the three functions at the top. These make testing your functions easy.
- Create a variable named
test
and assign to it a list of arguments being passed in. Use a list even if there is only one argument. - Find the corrrect return value for those arguments; assign that
value to the variable
expected
. - If you DO NOT return a float, do this
run_test(your_function, expected, test)
- If you DO return a float, do this
run_test_float(your_function, expected, test)
Create Test Cases! Try and do things that might break your code. Be fearless. But be fair; do not violate the given preconditions.