Others operators

Luiz Siqueira Neto writes:

I know fine binarie numbers, but mabe um can give me some small example
code. : )

Thanks about help me.

I replied:

Luiz:

Try the attached program. Continue trying different values in line 31 until
you feel that you understand it.

-- Michael Chermside

But the attachment didn't work. So here's the program:

----- demo.py -----
def toBinary(x):
    if False:
        pass
    elif x == 0:
        return "0"
    elif x == 1:
        return "1"
    elif x > 1:
        if x % 2:
            return toBinary(x >> 1) + "1"
        else:
            return toBinary(x >> 1) + "0"
    elif x == -1:
        return "(infinite 1's)"
    elif x < -1:
        return "(infinite 1's)0" + toBinary(-x - 1)

def printResults(op,x,y,xOPy):
    if y == None:
        print ('%s %i = %i ie: %s %s = %s'
               % (op, x, xOPy,
                  op, toBinary(x), toBinary(xOPy)))
    else:
        print ('%i %s %i = %i ie: %s %s %s = %s'
               % (x, op, y, xOPy,
                  toBinary(x), op, toBinary(y), toBinary(xOPy)))

for x, y in [(200, 3), (27, 13), (-6, 4)]:
    printResults('>>', x, y, x >> y)
    printResults('<<', x, y, x << y)
    printResults('&', x, y, x & y)
    printResults('|', x, y, x | y)
    printResults('^', x, y, x ^ y)
    printResults('~', x, None, ~x)

···

-------------------

def toBinary(x):
    if False:
        pass
    elif x == 0:
        return "0"
    elif x == 1:
        return "1"
    elif x > 1:
        if x % 2:
            return toBinary(x >> 1) + "1"
        else:
            return toBinary(x >> 1) + "0"
    elif x == -1:
        return "(infinite 1's)"
    elif x < -1:
        return "(infinite 1's)0" + toBinary(-x - 1)

Exist """toBinary()""" and you override it or you create this method and
make a self call?

def toBinary(x):
    if False:
        pass
    elif x == 0:
        return "0"
    elif x == 1:
        return "1"
    elif x > 1:
        if x % 2:
            return toBinary(x >> 1) + "1"
        else:
            return toBinary(x >> 1) + "0"
    elif x == -1:
        return "(infinite 1's)"
    elif x < -1:
        return "(infinite 1's)0" + toBinary(-x - 1)

Exist """toBinary()""" and you override it or you create this method and
make a self call?

def toBinary(x):
    if False:
        pass
    elif x == 0:
        return "0"
    elif x == 1:
        return "1"
    elif x > 1:
        if x % 2:
            return toBinary(x >> 1) + "1"
        else:
            return toBinary(x >> 1) + "0"
    elif x == -1:
        return "(infinite 1's)"
    elif x < -1:
        return "(infinite 1's)0" + toBinary(-x - 1)

Exist """toBinary()""" and you override it or you create this method and
make a self call?

def toBinary(x):
    if False:
        pass
    elif x == 0:
        return "0"
    elif x == 1:
        return "1"
    elif x > 1:
        if x % 2:
            return toBinary(x >> 1) + "1"
        else:
            return toBinary(x >> 1) + "0"
    elif x == -1:
        return "(infinite 1's)"
    elif x < -1:
        return "(infinite 1's)0" + toBinary(-x - 1)

Exist """toBinary()""" and you override it or you create this method and
make a self call?

> def toBinary(x):
> if False:
> pass
> elif x == 0:
> return "0"
> elif x == 1:
> return "1"
> elif x > 1:
> if x % 2:
> return toBinary(x >> 1) + "1"
> else:
> return toBinary(x >> 1) + "0"
> elif x == -1:
> return "(infinite 1's)"
> elif x < -1:
> return "(infinite 1's)0" + toBinary(-x - 1)
>

Exist """toBinary()""" and you override it or you create this method and
make a self call?

He creates it and makes the "self call". It's called a "recursive"
function. A simple example would be a function that provides an
exponential function:

def exp(base, exponent):
    if exponent:
        return base * exp(base, exponent - 1)
    else:
        return 1

exp(2, 2)

4

exp(2, 3)

8

exp(3, 3)

27

exp(10, 3)

1000

exp(10, 4)

10000

Of course you would never use this in real code, as it performs poorly
compared to iterative algorithms not to mention that Python already has
the ** operator which does exponentiation.

Regards,

Cliff

···

On Wed, 2003-08-27 at 13:55, Luiz Siqueira Neto wrote:

--
She clings to the nearest passerby, she's lost control again
                                              -Joy Division

Thanks about help and sorry about a lot of same message on list.

···

----- Original Message -----
From: "Cliff Wells" <clifford.wells@comcast.net>
To: <wxPython-users@lists.wxwindows.org>
Sent: Thursday, August 28, 2003 2:06 AM
Subject: Re: [wxPython-users] RE: Others operators

On Wed, 2003-08-27 at 13:55, Luiz Siqueira Neto wrote:
> > def toBinary(x):
> > if False:
> > pass
> > elif x == 0:
> > return "0"
> > elif x == 1:
> > return "1"
> > elif x > 1:
> > if x % 2:
> > return toBinary(x >> 1) + "1"
> > else:
> > return toBinary(x >> 1) + "0"
> > elif x == -1:
> > return "(infinite 1's)"
> > elif x < -1:
> > return "(infinite 1's)0" + toBinary(-x - 1)
> >
>
> Exist """toBinary()""" and you override it or you create this method and
> make a self call?

He creates it and makes the "self call". It's called a "recursive"
function. A simple example would be a function that provides an
exponential function:

def exp(base, exponent):
    if exponent:
        return base * exp(base, exponent - 1)
    else:
        return 1

>>> exp(2, 2)
4
>>> exp(2, 3)
8
>>> exp(3, 3)
27
>>> exp(10, 3)
1000
>>> exp(10, 4)
10000

Of course you would never use this in real code, as it performs poorly
compared to iterative algorithms not to mention that Python already has
the ** operator which does exponentiation.

Regards,

Cliff

--
She clings to the nearest passerby, she's lost control again
                                              -Joy Division

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwindows.org
For additional commands, e-mail: wxPython-users-help@lists.wxwindows.org