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 x>1 else 1
naa, it's not too bad...
--
By ZeD
--
http://mail.python.org/mailman/listinfo/python-list
On Mon, 01 Nov 2010 20:23:42 +1300, Lawrence D'Oliveiro wrote:
> In message , 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
In message , 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 what, I think I actually prefer the
In message , 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” D’Oliveiro
--
http://mail.python.or
On 27 Okt., 10:27, Arnaud Delobelle 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 "n<1". Then you can leave out the space
before "or" ("0o
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]
...
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 -
Stefan Behnel 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:
>>>
>>>
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
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
> >> def fact(
On 29 October 2010 00:13, Dave Angel 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 point
On 10/28/2010 10:01 AM, Xavier Ho wrote:
On 28 October 2010 23:52, Dave Angel 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 numbe
On 28 October 2010 23:52, Dave Angel 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
--
http:/
On 2:59 PM, Xavier Ho wrote:
On 27 October 2010 18:27, Arnaud Delobelle 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 27 October 2010 18:27, Arnaud Delobelle 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 positive and
ne
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
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
>>(http://en.wikiped
@ 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_evaluat
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
>> def fact(x):
>> return x > 1 and x * fact(x - 1) or
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 ge
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
> def fact(x):
> return x > 1 and x * fact(x - 1) or 1
>
> But I don't really get h
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, Geob
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 .
--
http://mail
23 matches
Mail list logo