[Tutor] Cannot Understand

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Fri Mar 10 21:02:57 CET 2006


> The expression n-1 + abs(n-1) is always a true value... except in the
> case where n is zero.

Sorry, that's wrong of me, but you know what I mean.  *grin*

It zeros out when n-1 <= 0, that is, when n <= 1.

######
>>> def test(n):
...     return n-1 + abs(n-1)
...
>>> for x in range(-5, 5):
...     print x, test(x)
...
-5 0
-4 0
-3 0
-2 0
-1 0
0 0
1 0
2 2
3 4
4 6
######

So I wasn't paying enough attention.


With this, f(n) now looks something like this:

##############################
def f(n):
    if n > 1:
        return f(n-1) * n or 1
    else:
        return 0
##############################

The rest of the analysis follows from the previous email.  So it's not
exactly the factorial function, but it's very similar to it.  It's off by
a small constant.



More information about the Tutor mailing list