Re: Reverse Iteration Through Integers

2009-10-19 Thread Martien Verbruggen
On Sun, 18 Oct 2009 23:04:36 -0700 (PDT), alex23 wrote: > On Oct 19, 3:53 pm, Jabba Laci wrote: >> Would someone explain how str[::-1] work? I'm new to Python and I only >> saw so far the str[begin:end] notation. What is the second colon? > > Slice notation is of the form [start:stop:step

Re: Reverse Iteration Through Integers

2009-10-19 Thread Bearophile
Paul Rubin: > If you want to peel off digits from an int one by one without string > conversions, it's easiest to do that in reverse order: > >   n = 961 >   digits = [] >   while n > 0: >     n,d = divmod(n, 10) >     digits.append(d) > > Look up the docs for "divmod" for an explanation of that h

Re: Reverse Iteration Through Integers

2009-10-18 Thread alex23
alex23 wrote: > The only mention I could find was > http://docs.python.org/dev/3.0/library/functions.html#slice No idea why that link was the one that came up, this is more appropriate: http://docs.python.org/3.1/library/functions.html#slice -- http://mail.python.org/mailman/listinfo/python-l

Re: Reverse Iteration Through Integers

2009-10-18 Thread alex23
On Oct 19, 3:53 pm, Jabba Laci wrote: > Would someone explain how str[::-1] work? I'm new to Python and I only > saw so far the str[begin:end] notation. What is the second colon? Slice notation is of the form [start:stop:step]. start defaults to the start of the sequence, stop to the end, and ste

Re: Reverse Iteration Through Integers

2009-10-18 Thread Chris Rebert
On Sun, Oct 18, 2009 at 10:53 PM, Jabba Laci wrote: > Hi, > > Would someone explain how str[::-1] work? I'm new to Python and I only > saw so far the str[begin:end] notation. What is the second colon? Specifies the step value, as in: foo[start:stop:step] When not specified it, defaults to 1. S

Re: Reverse Iteration Through Integers

2009-10-18 Thread Jabba Laci
Hi, Would someone explain how str[::-1] work? I'm new to Python and I only saw so far the str[begin:end] notation. What is the second colon? Thanks, Laszlo > Here is a simplistic version that doesn't use fancy math: > str(24) > '24' str(24)[::-1] > '42' int(str(24)[::-1]) > 42 --

Re: Reverse Iteration Through Integers

2009-10-18 Thread Mick Krippendorf
Paul Rubin wrote: > Yet another way is to use recursion. I'll leave that as an exercise too. This time with big numbers: def trampoline(bouncing, *args, **kwargs): while bouncing: result, bouncing, args, kwargs = bouncing(*args, **kwargs) if result: return result

Re: Reverse Iteration Through Integers

2009-10-18 Thread MRAB
Benjamin Middaugh wrote: I'm trying to make an integer that is the reverse of an existing integer such that 169 becomes 961. I guess I don't know enough yet to figure out how to do this without a ton of awkward-looking code. I've tried for loops without much success. I guess I need a good way o

Re: Reverse Iteration Through Integers

2009-10-18 Thread Paul Rubin
Benjamin Middaugh writes: > I'm trying to make an integer that is the reverse of an existing > integer such that 169 becomes 961. I guess I don't know enough yet to > figure out how to do this without a ton of awkward-looking code. I've > tried for loops without much success. I guess I need a good

Re: Reverse Iteration Through Integers

2009-10-18 Thread Christian Heimes
Benjamin Middaugh schrieb: > I'm trying to make an integer that is the reverse of an existing integer > such that 169 becomes 961. I guess I don't know enough yet to figure out > how to do this without a ton of awkward-looking code. I've tried for > loops without much success. I guess I need a g

Re: Reverse Iteration Through Integers

2009-10-18 Thread Rhodri James
On Sun, 18 Oct 2009 19:34:09 +0100, Benjamin Middaugh wrote: I'm trying to make an integer that is the reverse of an existing integer such that 169 becomes 961. I guess I don't know enough yet to figure out how to do this without a ton of awkward-looking code. I've tried for loops withou

Re: Reverse Iteration Through Integers

2009-10-18 Thread Lie Ryan
Benjamin Middaugh wrote: I'm trying to make an integer that is the reverse of an existing integer such that 169 becomes 961. I guess I don't know enough yet to figure out how to do this without a ton of awkward-looking code. I've tried for loops without much success. I guess I need a good way o

Reverse Iteration Through Integers

2009-10-18 Thread Benjamin Middaugh
I'm trying to make an integer that is the reverse of an existing integer such that 169 becomes 961. I guess I don't know enough yet to figure out how to do this without a ton of awkward-looking code. I've tried for loops without much success. I guess I need a good way of figuring out the length