medium_functions.py

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 must be done using recursion. The last problem is a real bear. It's Stupid Questions for $2000, according to Alex. I don't expect everyone to get it. The rest are pretty reasonable.


#################################################
#  Author:
#  Date:
#  harder_functions.py
#################################################

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 rectangle(m, n):
    """Precondition: m and n are nonnegative integers.
Postcondition:  prints a rectangle of *s to the screen that 
has m rows and n columns"""
    pass

###################Problem 2################
def power(base, exp):
    """prec:  base is a number, exp is an integer`
postc: computes base**exp from scratch (no using libraries
or built-in functions).  Pay attention to the case where exp < 0!"""
    return 1
###################Problem 3################
def convert_to_binary(n):
    return ""
###################Problem 4################
def sum_of_digits(n):
    """precondition: n is an integer
postcondition:  returns the sum of the digits of n.  You should
be able to take negative input; in such cases disregard the - sign.  """
    return 0
###################Problem 5################
def super_summer(f, n):
    """precondition: f is a function that is defined for all nonnegative
integers.
postcondition:  returns sum(f(k), k = 1..n) """
    return 0
###################Problem 6################
def reverse_string(x):
    """prec: x is a string
    postc:  return a string containing x is reverse."""
    return ""
###################Problem 7################
def sum_from(n, x):
    """prec:  n is an integer, x is a list of integers.
postc: returns a sublist of x whose sum is n if it exists
and the graveyard object None otherwise.  """
    return None

def main():
    print("###################Problem 1################")
    print("You should get a rectangle with 5 rows, 10 columns.")
    rectangle(5,10)
    print("###################Problem 2################")
    base = 4.5
    exp = 3
    y = 91.125
    print(f"Case base = {base}, exp = {exp}:", end="")
    print("PASS" if close_enough(power(base, exp), y) else "FAIL")
    base = 4.5
    exp = -3
    y = 0.010973936899862825
    print(f"Case base = {base}, exp = {exp}:", end="")
    print("PASS" if close_enough(power(base, exp), y) else "FAIL")
    print("###################Problem 3################")
    print("###################Problem 4################")
    print("###################Problem 5################")
    print("###################Problem 6################")
    print("###################Problem 7################")

main()