How on Factorial

Arnaud Delobelle arnodel at gmail.com
Thu Oct 28 14:38:03 EDT 2010


Stefan Behnel <stefan_ml at behnel.de> writes:

> karmaguedon, 28.10.2010 18:46:
>> On 27 oct, 10:27, Arnaud Delobelle wrote:
>>> Chris Rebert writes:
>>>> This is stunt coding / code golf; no one should actually write
>>>> factorial like that.
>>>
>>> True.  It's far too verbose.  I'd go for something like:
>>>
>>>      f=lambda n:n<=0 or n*f(~-n)
>>>
>>> I've saved a few precious keystrokes and used the very handy ~-
>>> idiom!
>>
>> I didn't know the idiom, nice tip.
>
> Not really. Given that many people won't know it, most readers will
> have a hard time deciphering the above code, so it's best not to do
> this.
>
> Stefan

My reply was obviously tongue in cheek, triggered by Chris Rebert's
"code golf" remark.  using ~-n instead of n-1 if obfuscatory and doesn't
even save keystrokes!  (Although it could in other situations, e.g
2*(n-1) can be written 2*~-n, saving two brackets.)

-- 
Arnaud



More information about the Python-list mailing list