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”
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
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
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
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
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
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
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
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
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
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
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
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
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
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:
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
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 .
--
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,
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
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 (
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
@ 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
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
23 matches
Mail list logo