medium_functions.py

Download both files at the left and put them in the same directory (smart idea: make a directory for this assignment) Save time by right-clicking on the links and using Save Link As.

You will import the testing apparatus into your code.

Here is my test module


############################################################
#   Author: Morrison
#   Date created:  2021-08-30
#   Date last modified:  2021-08-30
#   run_test Module
#   FREE CODE FOR WRITING TESTS
#   Usage is demonstrated on examples below
################# DO NOT CHANGE THIS CODE ###################
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}")
        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}")

Here is the shell code. It runs out of the box.


#################################################
#  Author:
#  Date:
#  medium_functions.py
#################################################
from run_tests import run_test, run_test_float, close_enough
###################FREE CODE######################################
def is_leap(year):
    """prec:  year is a modern year
postc: returns True if the year leaps.
"""
    out = False
    if year %4 == 0:
        out = not out
        if year % 100 == 0:
            out = not out
            if year % 400 == 0:
                out = not out
    return out
##############END FREE CODE######################################


###################Problem 1################
def meanie(theList):
    """Precondition: theList is a non-empty list of numbers
Postcondition: return the mean of the numbers."""
    return 0
###################Problem 2################
# 1 is January, 2 is February, etc.
def day_in_year(year, month, day):
    """prec:  year/month/day is a valid date
    postc: returns the ordinal position of the day in the year
    (Feb 15 is the 46th day of year 2000).
    Hint:  The built-in function sum is your friend. Learn about it."""
    lengths = [31, 28 + is_leap(year), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    return 0
###################Problem 3################
def days_left_in_year(year, month, day):
    """prec:  year/month/day is a valid date
    postc: returns the number of days left in the year
    (Feb 15 is the 44th day of year 2000)."""
    return 0
###################Problem 4################
def days_to_graduation(year, month, day):
    """prec:  year/month/ is a valid date before graduation
    in 2020 or 2021.  It's 29 May 2021.
    postc: returns the number of days until graduation 
    If the year is illegal, or if a date after 29 May 2021 is entered,
    return "ILLEGAL INPUT"  """
    return 0
###################Problem 5################
def dhms(secs):
    """prec:  secs is a nonnegative integer
    postc: return a STRING of the form d:hh:mm:ss, where d is the number
    of days, h is the number of hours, m is the number     of minutes, and
    s is the number of seconds, h < 24, 0 <= m, s, < 60.  Give each of
    h, m, s a two character width, padding with zeroes as needed.  """
    return ""
###################Problem 6################
def water_closet(theString):
    """precondition: thesString is a string.
postcondition: returns a tuple (c, w, l) where c is the number of
characters in theString, w is the number of words, and l is the number of
lines in the string"""
    return (0, 0, 0)
###################Problem 7################
def math_case(x):
    """precondition: x is a number
postcondition: If x > 4, f(x) = x - 4, if x < -5, f(x) = x + 5,
otherwise, f(x) = 0."""
    if x > 4:
        return x - 4
    if x < -5:
        return x + 5
    return 0

def main():
    print("###################Problem 1################")
    test = [[1,2,3,4,5,6]]
    expected = 3.5
    run_test_float(meanie, expected, test)
    test = [[-3, -2, 0, 0, 6, 5]]
    expected = 1.0
    run_test_float(meanie, expected, test)
    print("###################Problem 2################")
    print("###################Problem 3################")
    print("###################Problem 4################")
    print("###################Problem 5################")
    print("###################Problem 6################")
    print("###################Problem 7################")
main()