LP0.py, 17 Mar 2022

Practice LP0

Directions In the navigation area, you can download a zip archive. When you unzip it, you will see a directory LP0 that contains two files.

When you open the file LP0.py, run it right away and you will see it has no error messages. You can download the files individually by right-clicking on the links in the navigation area and by selecting Save Link As...

Keep your work in the LP0 directory. Some people prefer to work on each functon alone in a file. You can do this in VS code, and just paste your finished product back into LP0.py.

When you are done, rezip the file and upload it to Canvas.

File Contents

LP0.py


from rt import *
######################   Problem 1    ############################
def count_words(s):
    """
        count_words("I eat cake") -> 3
        count_words("") -> 0
        count_words("To be or not to be, that is the question.") -> 10"""
    return 0
######################   Problem 2    ############################
def space_cadet(ch, s):
    """ ch is a character, and s is a string
return a string that repplaces every instance of ch
with a spoace.  This shuould be case SENSITIVE.
example:  space_cadet("a", "panorama") ->"p nor m "
"""
    return ""
######################   Problem 3    ############################
def same_letters(word1, word2):
    """
    return True if the two words have exactly the same 
set of letters (ignore repeats), case insensitive.
same_letters("scatter", "smatter") -> False
same_letters("fail", "FLAIL") -> True

"""
    return False
######################   Problem 4    ############################
def ordered_list(items):
    """items is a list of strings.  
    returns a string with an HTML ordered list.
    example:
ordered_list("cows", "horses", "pigs") ->
<ol>
    <li>cows</li>
    <li>horses</li>
    <li>pigs</li>
</ol>
You can use a triple-quoted string or whack-ns to achieve
the newline effect"""
    return ""
######################   Problem 5    ############################
def is_perfect(n):
    """
        n is a positive integer and n >= 2
        n is perfect if it is the sum of its proper divisors.
        6 has proper divisors 1,2,3 so is_perfect(6) -> True
        12 has proper divisors 1,2,3,4,6, which sum to 22,
        so is_perfect(12) -> False
        Hint: You might want to write a helper function 
        sum_of_proper_divisors(n)
    """
    return False
######################   Problem 6    ############################
def lurks_in(word, search_field):
    """
    word is a string of letters 
    search_field is a string
    returns True if the letters in word can be found in 
    order in the string search_field.  This is case-insensitive
    lurks_in("abcd, "abcessed") -> True
    lurks_in("foot, "flounder of trust") -> True
    lurks_in("dequeue", "defend the queen") -> False"""
    return False
def main():
    print("***************Test Problem 1************")
    s = "I eat cake"
    test = [s]
    expected = 3
    run_test(count_words, expected, test)
    s = ""
    test = [s]
    expected = 0
    run_test(count_words, expected, test)
    s = "To be or not to be, that is the question." 
    test = [s]
    expected = 10
    run_test(count_words, expected, test)
    print("***************Test Problem 2************")
    print("***************Test Problem 3************")
    print("***************Test Problem 4************")
    print(ordered_list(["cats", "dogs", "elephants", "ferrets"]))
    print("***************Test Problem 5************")
    print("***************Test Problem 6************")
main()

rt.py


# ###########################################################
#   Authors: Morrison with revision by Chris Agrella
#   Date created:  2021-08-30
#   Date last modified:  2021-09-17
#   rt Module
#   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)}"
            )