Code explorer 1.1


In [13]:
def rect_area( width, height ):
    """
    Real, Real -> Real
     
    Produces the area of rectangle of the given width and height
     
    >>> rect_area( 0, 0 )
    0
     
    >>> rect_area( 10, 20 )
    200
     
    >>> act = rect_area( 10.5, 2.0 )
    >>> exp = 21.0
    >>> abs( act - exp ) < abs( exp ) * EPS
    True
    """
    return width * height
     
 
# Make sure doctests run when this code is run
 

import doctest
EPS = 1.0e-6
print( doctest.run_docstring_examples(rect_area,globals())  )


None

Code explorer 1.2


In [14]:
#
# Data Definitions
#
 
# Account is dict(name=str, balance=float)
# interp. a bank account with a name and a balance
 
A1 = dict(name='Joe', balance=212.23)
A2 = dict(name='Chris', balance=-12.34)
A3 = dict(name='Pat', balance=0.00)
 
# def fn_for_acc(a):
#     return ...a['name'] ...a['balance']
 
 
#
# Functions
#
 
def is_overdrawn(a):
    """
    Account -> bool
     
    Determines if account a is overdrawn
     
    >>> is_overdrawn(A1)
    False
     
    >>> is_overdrawn(A2)
    True
     
    >>> is_overdrawn(A3)
    False
    """
    return a['balance'] < 0.0
 
 
#
# Make sure tests run when we run this program
#
 


print( doctest.run_docstring_examples(is_overdrawn,globals()) )


None

In [11]:
def rect_area( width, height ):
    """
    Real, Real -> Real
     
    Produces the area of rectangle of the given width and height
     
    >>> rect_area( 0, 0 )
    0
     
    >>> rect_area( 10, 20 )
    2000
     
    >>> act = rect_area( 10.5, 2.0 )
    >>> exp = 21.0
    >>> abs( act - exp ) < abs( exp ) * EPS
    True
    """
    return width * height
     
 
# Make sure doctests run when this code is run
 
print( doctest.run_docstring_examples(rect_area,globals()) )


**********************************************************************
File "__main__", line 10, in NoName
Failed example:
    rect_area( 10, 20 )
Expected:
    2000
Got:
    200
None