[Tutor] Re: recursion question

Lee Harr missive at hotmail.com
Wed Dec 3 16:47:06 EST 2003


def mult(m, n):
    if n == 0:
        return 0
    if n == 1:
        return m
    if n < 0:
        p = abs(n)
        result = -1 * (m + mult(m, p-1))
        return result
    return m + mult(m, n-1)


Nice. Here's my version for comparison...

def mult(m, n):
    if n < 0:
        return -mult(m, -n)
    elif n == 0:
        return 0
    elif n == 1:
        return m
    else:
        return m + mult(m, n-1)


Now all we need to do is write a docstring and some regression tests
and we're all set  :o)

_________________________________________________________________
Protect your PC - get McAfee.com VirusScan Online 
http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963




More information about the Tutor mailing list