Wednesday, February 16, 2011

Feb 16

This is the fix program of ch05 program:

def compare(a, b):
"""
>>> compare(5, 4)
1
>>> compare(7, 7)
0
>>> compare(2, 3)
-1
>>> compare(2, 1)
1
"""
return a-b

if __name__ == '__main__':
import doctest
doctest.testmod()

this will now pass the doctest
Fix on the hypotenuse program

def hypotenuse(a, b):
"""
>>> hypotenuse(3, 4)
5.0
>>> hypotenuse(12, 5)
13.0
>>> hypotenuse(7, 24)
25.0
>>> hypotenuse(9, 12)
15.0
"""
return

if __name__== '__main__':
import doctest
doctest.testmod()
This is not the completed program yet, im still working on fixing the return so that the program passes the doctest. So far I no that the number needs to have .0 in it to allow the results to have .0 to them. I've ruled out that a**2 + b**2 will not work. I've also tried a - b + 4 which also came out to fail.

1 comment:

  1. c**2 == a**2 + b**2, and you want c. You need to know that raising a value to the 1/2.0 (or 0.5) power computes its square root, so:

    return ((a**2) + (b**2))**0.5

    Your solution for compare definitely works on all given tests, but it doesn't work the way I intended, so let me add a few more tests:


    >>> compare(9, 4)
    1
    >>> compare(2, 7)
    -1

    Please add these tests to the previous ones and create a solution that works for all of them.

    ReplyDelete