medium_functions.py

This is an intermediate function writing exercise. It includes a facility for easily testing your functions.


#################################################
#  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 44th 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."""
    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################")
    run_test(day_in_year, 146, [2021, 5, 26])
    run_test(day_in_year, 147, [2020, 5, 26])
    run_test(day_in_year, 366, [2020, 12, 31])
    run_test(day_in_year, 365, [2019, 12, 31])
    run_test(day_in_year, 1, [2019, 1, 1])
    print("###################Problem 3################")
    run_test(days_left_in_year, 219, [2021, 5, 26])
    run_test(days_left_in_year, 219, [2020, 5, 26])
    run_test(days_left_in_year, 0, [2020, 12, 31])
    run_test(days_left_in_year, 365, [2020, 1, 1])
    print("###################Problem 4################")
    run_test(days_to_graduation, "ILLEGAL INPUT", [2019, 5, 2])
    run_test(days_to_graduation, 148, [2021, 1, 1])
    run_test(days_to_graduation, "ILLEGAL INPUT", [2021, 6, 1])
    run_test(days_to_graduation, 149, [2020, 12, 31])
    print("###################Problem 5################")
    run_test(dhms, "0:00:00:20", 20)
    run_test(dhms, "1:00:00:01", 86401)
    run_test(dhms, "0:01:00:10", 3610)
    run_test(dhms, "11:13:46:40", 1000000)
    print("###################Problem 6################")
    s = ""
    run_test(water_closet, (0, 0, 0), s)
    s = "The quick brown fox\njumped over the lazy dog"
    run_test(water_closet, (44, 9, 2) , s)
    s = "s t e v e"
    run_test(water_closet, (9, 5, 1) , s)
    print("###################Problem 7################")
    run_test_float(math_case, 6, [10])
    run_test_float(math_case, 0, [4])
    run_test_float(math_case, 0, [0])
    run_test_float(math_case, 0, [-5])
    run_test_float(math_case, -5, [-10])
main()