Re: How on Factorial

2010-11-01 Thread Lawrence D'Oliveiro
In message dvukp7-7sa@satorlaser.homedns.org, Ulrich Eckhardt wrote: Geobird wrote: def fact(x): return x 1 and x * fact(x - 1) or 1 I'd say this is about as small as it gets. fact = lambda x : x 1 and x * fact(x - 1) or 1 -- Lawrence “Functionalism Strikes Again”

Re: How on Factorial

2010-11-01 Thread Lawrence D'Oliveiro
In message qoteibcq9pr@ruuvi.it.helsinki.fi, Jussi Piitulainen wrote: (I agree that no one should write factorial like that, except as a joke. I have nothing against (x if (a b) else y). The trick with and and or was used before Python had an actual conditional expression.) You know

Re: How on Factorial

2010-11-01 Thread Steven D'Aprano
On Mon, 01 Nov 2010 20:23:42 +1300, Lawrence D'Oliveiro wrote: In message qoteibcq9pr@ruuvi.it.helsinki.fi, Jussi Piitulainen wrote: (I agree that no one should write factorial like that, except as a joke. I have nothing against (x if (a b) else y). The trick with and and or was used

Re: How on Factorial

2010-11-01 Thread Vito 'ZeD' De Tullio
Lawrence D'Oliveiro wrote: You know what, I think I actually prefer the trick to Python’s backwards-if syntax... fact = lambda x: x*fact(x-1) if x1 else 1 naa, it's not too bad... -- By ZeD -- http://mail.python.org/mailman/listinfo/python-list

Re: How on Factorial

2010-10-30 Thread Wolfram Hinderer
On 27 Okt., 10:27, Arnaud Delobelle arno...@gmail.com wrote: 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! You can replace n=0 with n1. Then you can leave out the space before or

Re: How on Factorial

2010-10-29 Thread Steven D'Aprano
On Thu, 28 Oct 2010 10:13:15 -0400, Dave Angel wrote: Inverting the bits of a floating point number wouldn't make much sense, so fortunately it gives an error. from struct import pack, unpack def float_as_int(x): ... bits = pack(d, x) ... return unpack(q, bits)[0] ... def

Re: How on Factorial

2010-10-28 Thread Bj Raz
I'm working on some factorial stuff myself, and I'm running into that issue that the CPU or ALU (Algorithmic Logical Unit), isn't powerful enough to compute the numbers I'm trying to produce, including the OS has its own number crunching limitation for accuracy. To accurately generate the numbers

Re: How on Factorial

2010-10-28 Thread Xavier Ho
On 27 October 2010 18:27, Arnaud Delobelle arno...@gmail.com wrote: 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! Huh, I've never seen that one before. Seems to work on both

Re: How on Factorial

2010-10-28 Thread Dave Angel
On 2:59 PM, Xavier Ho wrote: On 27 October 2010 18:27, Arnaud Delobellearno...@gmail.com wrote: 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! Huh, I've never seen that one

Re: How on Factorial

2010-10-28 Thread Xavier Ho
On 28 October 2010 23:52, Dave Angel da...@ieee.org wrote: The ~- trick only works on two's complement numbers. I've worked on machines in the past that used one's complement, and this wouldn't work there. DaveA I imagine this wouldn't work on floating point numbers either. Cheers, Xav

Re: How on Factorial

2010-10-28 Thread Dave Angel
On 10/28/2010 10:01 AM, Xavier Ho wrote: On 28 October 2010 23:52, Dave Angelda...@ieee.org wrote: The ~- trick only works on two's complement numbers. I've worked on machines in the past that used one's complement, and this wouldn't work there. DaveA I imagine this wouldn't work on

Re: How on Factorial

2010-10-28 Thread Xavier Ho
On 29 October 2010 00:13, Dave Angel da...@ieee.org wrote: From the help: The unary ~ (invert) operator yields the bitwise inversion of its plain or long integer argument. The bitwise inversion of x is defined as -(x+1). It only applies to integral numbers Inverting the bits of a floating

Re: How on Factorial

2010-10-28 Thread karmaguedon
On 27 oct, 10:27, Arnaud Delobelle arno...@gmail.com wrote: Chris Rebert c...@rebertia.com writes: On Tue, Oct 26, 2010 at 11:25 PM, Geobird a1chan...@gmail.com wrote:  I  am a beginner in Python and would ask for a help. I  was searching for  smaller  version  of  code  to calculate

Re: How on Factorial

2010-10-28 Thread Stefan Behnel
karmaguedon, 28.10.2010 18:46: On 27 oct, 10:27, Arnaud Delobelle wrote: Chris Rebert writes: On Tue, Oct 26, 2010 at 11:25 PM, Geobird wrote: I am a beginner in Python and would ask for a help. I was searching for smaller version of code to calculate factorial . Found this one

Re: How on Factorial

2010-10-28 Thread Arnaud Delobelle
Stefan Behnel stefan...@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:

Re: How on Factorial

2010-10-28 Thread Stefan Behnel
Arnaud Delobelle, 28.10.2010 20:38: 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.) Sure, good call. And look, it's only slightly slower than the obvious code: $ python3

How on Factorial

2010-10-27 Thread Geobird
I am a beginner in Python and would ask for a help. I was searching for smaller version of code to calculate factorial . Found this one def fact(x): return x 1 and x * fact(x - 1) or 1 But I don't really get how ( x 1 and x * fact(x - 1)) works . --

Re: How on Factorial

2010-10-27 Thread Nitin Pawar
focus on the AND condition ... return is true only if both conditions are true so unless the factorial is calculated (second portion of AND statement) return will not give factorial. the second portion is recursive call to self as long as x is greater than 1 On Wed, Oct 27, 2010 at 11:55 AM,

Re: How on Factorial

2010-10-27 Thread Chris Rebert
On Tue, Oct 26, 2010 at 11:25 PM, Geobird a1chan...@gmail.com wrote:  I  am a beginner in Python and would ask for a help. I  was searching for  smaller  version  of  code  to calculate factorial . Found  this one def fact(x):        return x 1 and x * fact(x - 1) or 1  But I don't  

Re: How on Factorial

2010-10-27 Thread Ulrich Eckhardt
Geobird wrote: I am a beginner in Python and would ask for a help. I was searching for smaller version of code to calculate factorial . Found this one def fact(x): return x 1 and x * fact(x - 1) or 1 I'd say this is about as small as it gets. But I don't really get how (

Re: How on Factorial

2010-10-27 Thread Arnaud Delobelle
Chris Rebert c...@rebertia.com writes: On Tue, Oct 26, 2010 at 11:25 PM, Geobird a1chan...@gmail.com wrote:  I  am a beginner in Python and would ask for a help. I  was searching for  smaller  version  of  code  to calculate factorial . Found  this one def fact(x):        return x 1 and

Re: How on Factorial

2010-10-27 Thread Geobird
@ Ulrich : Tx @ Rebert : Appreciate your interpretation. It made me think about ternary operation . Say (a b) and x or y Are all ternary operations prone to ...( in your words ) It exploits short-circuit evaluation (http://en.wikipedia.org/wiki/Short-circuit_evaluation

Re: How on Factorial

2010-10-27 Thread Jussi Piitulainen
Geobird writes: @ Ulrich : Tx @ Rebert : Appreciate your interpretation. It made me think about ternary operation . Say (a b) and x or y Are all ternary operations prone to ...( in your words ) It exploits short-circuit evaluation