You can download this file by right-clicking on the link in the navigation area. Do this, and open it in your text editor. Writing test code like this is smart and it can save you from silly mistakes. In fact, it is a great idea to write the test code BEFORE you write the function. Be impish; try to break your work. But be fair: don't violate the preconditions.
Everything here can be done with string, list, and tuple methods. No looping is necessary. And it ain't fair because I haven't covered it yet.
#################################################
# Author:
# Date:
# medium_functions.py
#################################################
###################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
def close_enough(a,b):
"""This is free code. You can use it to check for "equality
of floating-point numbers. Consider floats x and y to be equal
here if they are within 1e-6 of each other."""
return abs(a-b)<1e-6
##############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."""
if x > 4:
return x - 4
if x < -5:
return x + 5
return 0
def main():
print("###################Problem 1################")
x = [1,2,3,4,5,6]
y = 3.5
print(f"Case x = {x}, y = {y}:", end = "")
print("PASS" if close_enough(meanie(x), y) else "FAIL")
x = [-3, -2, 0, 0, 6, 5]
y = 1.0
print(f"Case x = {x}, y = {y}:", end = "")
print("PASS" if close_enough(meanie(x), y) else "FAIL")
print("###################Problem 2################")
print("###################Problem 3################")
print("###################Problem 4################")
print("###################Problem 5################")
print("###################Problem 6################")
print("###################Problem 7################")
main()