Re: Reversing a string

2007-07-05 Thread Martin Durkin
[EMAIL PROTECTED] (Alex Martelli) wrote in news:[EMAIL PROTECTED]: > Aahz <[EMAIL PROTECTED]> wrote: >> I would agree with people who claim >> that you should memorize most of the built-in functions (which is >> precisely why there is a high barrier to adding more built-in >> functions). > > I

Re: Reversing a string

2007-07-05 Thread Sion Arrowsmith
Jan Vorwerk <[EMAIL PROTECTED]> wrote: > [ lots of sensible stuff to discover "reversed" ] > >>> print reversed.__doc__ See also: >>> help(reversed) -- \S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/ "Frankly I have no feelings towards penguins one way or the other" -- Arth

Re: Reversing a string

2007-07-04 Thread Jan Vorwerk
Martin Durkin a écrit , le 02.07.2007 06:38: > This is an interesting point to me. I am just learning Python and I > wonder how I would know that a built in function already exists? > At what point do I stop searching for a ready made solution to a > particular problem and start programming my o

Re: Reversing a string

2007-07-04 Thread Alex Martelli
Aahz <[EMAIL PROTECTED]> wrote: ... > This works in all versions of Python back to 1.5.2 IIRC. reversed() is > a moderately new built-in function; Yep: it came with Python 2.4, first alpha just 4 years ago, final release about 3 years and 8 months ago. "Moderately new" seems an appropriate ta

Re: Reversing a string

2007-07-04 Thread Aahz
In article <[EMAIL PROTECTED]>, Martin Durkin <[EMAIL PROTECTED]> wrote: >[EMAIL PROTECTED] (Alex Martelli) wrote in >news:[EMAIL PROTECTED]: >> >> So, something like: >> >> for c in reversed(x): print c >> >> is mostly likely how I'd present the solution to the task. > >This is an interesting

Re: Reversing a string

2007-07-01 Thread Paul Rubin
Martin Durkin <[EMAIL PROTECTED]> writes: > Is it just a matter of reading *all* the documentation before I start > coding? It's worth spending an hour or two reading through the library manual, yes. -- http://mail.python.org/mailman/listinfo/python-list

Re: Reversing a string

2007-07-01 Thread Martin Durkin
[EMAIL PROTECTED] (Alex Martelli) wrote in news:[EMAIL PROTECTED]: > Martin Durkin <[EMAIL PROTECTED]> wrote: >... >> >> def rev(x): >> >>> mylist = [] >> >>> for char in x: >> >>> mylist.append(char) >> >>> mylist.reverse() >> >>>

Re: Reversing a string

2007-07-01 Thread Jay Loden
Alex Martelli wrote: > since what you're doing is...: > s = "onomatopoeia" s = s.join(s[::-1]) s > 'aonomatopoeiaionomatopoeiaeonomatopoeiaoonomatopoeiaponomatopoeiaoonoma > topoeiatonomatopoeiaaonomatopoeiamonomatopoeiaoonomatopoeianonomatopoeia > o' > > ...which isn't really jus

Re: Reversing a string

2007-07-01 Thread Frank Swarbrick
Alex Martelli wrote: > Martin Durkin <[EMAIL PROTECTED]> wrote: >... > print "\n".join("spam"[::-1]) >... OK, maybe I'm missing the point here as I'm new to Python. The first one seems clearer to me. What am I missing? >>> I think all you are missing is familarity wit

Re: Reversing a string

2007-07-01 Thread Alex Martelli
Jay Loden <[EMAIL PROTECTED]> wrote: ... > For what it's worth, with python 2.5 on my Macbook: Hmmm, doesn't look to me as if it's worth much...: > [EMAIL PROTECTED] jloden]$ python -m timeit 's = "onomatopoeia"; s = s.join(s[::-1])' since what you're doing is...: >>> s = "onomatopoeia" >>>

Re: Reversing a string

2007-07-01 Thread Alex Martelli
Martin Durkin <[EMAIL PROTECTED]> wrote: ... > >> def rev(x): > >>> mylist = [] > >>> for char in x: > >>> mylist.append(char) > >>> mylist.reverse() > >>> for letter in mylist: > >>> print letter > >>> > >>>

Re: Reversing a string

2007-07-01 Thread Jay Loden
Evan Klitzke wrote: > >> I guess that's it. The first one reads more like a textbook example which >> is about where I am at. Is there any speed benefit from the one liner? > > The one line is quite a bit faster: > > [EMAIL PROTECTED] ~ $ python -m timeit 's = "onomatopoeia"; s = > s.join(s[::-

Re: Reversing a string

2007-07-01 Thread Evan Klitzke
On 1 Jul 2007 11:09:40 GMT, Martin Durkin <[EMAIL PROTECTED]> wrote: > Duncan Booth <[EMAIL PROTECTED]> wrote in > news:[EMAIL PROTECTED]: > > > Martin Durkin <[EMAIL PROTECTED]> wrote: > > > >> def rev(x): > >>> mylist = [] > >>> for char in x: > >>> my

Re: Reversing a string

2007-07-01 Thread Stefan Behnel
Martin Durkin wrote: > Duncan Booth <[EMAIL PROTECTED]> wrote in > news:[EMAIL PROTECTED]: > >> Martin Durkin <[EMAIL PROTECTED]> wrote: >> >>> def rev(x): mylist = [] for char in x: mylist.append(char) mylist.reverse()

Re: Reversing a string

2007-07-01 Thread Martin Durkin
Duncan Booth <[EMAIL PROTECTED]> wrote in news:[EMAIL PROTECTED]: > Martin Durkin <[EMAIL PROTECTED]> wrote: > >> def rev(x): >>> mylist = [] >>> for char in x: >>> mylist.append(char) >>> mylist.reverse() >>> for letter in myl

Re: Reversing a string

2007-07-01 Thread Duncan Booth
Martin Durkin <[EMAIL PROTECTED]> wrote: > def rev(x): >> mylist = [] >> for char in x: >> mylist.append(char) >> mylist.reverse() >> for letter in mylist: >> print letter >> >> However, compare the incredible d

Re: Reversing a string

2007-06-30 Thread Martin Durkin
ptn <[EMAIL PROTECTED]> wrote in news:1182997438.541012.54100 @o61g2000hsh.googlegroups.com: > def rev(x): > mylist = [] > for char in x: > mylist.append(char) > mylist.reverse() > for letter in mylist: > print

Re: Reversing a string

2007-06-27 Thread ptn
> mylist = [] > > That's bad. If you need to use a list in the rev function, you > should bind a new list to a local variable inside rev. > He's right. If you want to use a list to temporarily store the reversed version of your string, it should exist only in the local namespace of your funct

Re: Reversing a string

2007-06-27 Thread ptn
> > or one letter per line: > > >>> print "\n".join("spam"[::-1]) > > m > a > p > s > One liners rock ;) -- http://mail.python.org/mailman/listinfo/python-list

Re: Reversing a string

2007-06-27 Thread Neil Cerutti
On 2007-06-27, Scott <[EMAIL PROTECTED]> wrote: > Yeah I know strings == immutable, but question 1 in section > 7.14 of "How to think like a computer Scientist" has me trying > to reverse one. No, it just wants to to print the characters in reverse, one per line. > I've come up with two things, o

Re: Reversing a string

2007-06-27 Thread Terry Reedy
"Scott" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] | Yeah I know strings == immutable, but question 1 in section 7.14 of "How to | think like a computer Scientist" has me trying to reverse one. >>> 'this is a test'[::-1] 'tset a si siht' -- http://mail.python.org/mailman/l

Re: Reversing a string

2007-06-27 Thread vbr
> Původní zpráva > Od: Will Maier <[EMAIL PROTECTED]> > Předmět: Re: Reversing a string > Datum: 27.6.2007 19:08:40 > > On Wed, Jun 27, 2007 at 12:53:36PM -0400, Scott wrote: > > So how on earth wou

Re: Reversing a string

2007-06-27 Thread Diez B. Roggisch
Scott wrote: > Yeah I know strings == immutable, but question 1 in section 7.14 of "How > to think like a computer Scientist" has me trying to reverse one. > > I've come up with two things, one works almost like it should except that > every traversal thru the string I've gotten it to repeat the

Re: Reversing a string

2007-06-27 Thread Stefan Behnel
Scott wrote: > So how on earth would be the best way to: Write a function that takes a > string as an argument and outputs the letters backward, one per line. Homework? Anyway, what about: for c in string[::-1]: print c Stefan -- http://mail.python.org/mailman/listinfo/python-list

Re: Reversing a string

2007-06-27 Thread Will Maier
On Wed, Jun 27, 2007 at 12:53:36PM -0400, Scott wrote: > So how on earth would be the best way to: Write a function that > takes a string as an argument and outputs the letters backward, > one per line. >>> def rev(forward): ... backward = list(forward) ... backward.reverse()

Reversing a string

2007-06-27 Thread Scott
Yeah I know strings == immutable, but question 1 in section 7.14 of "How to think like a computer Scientist" has me trying to reverse one. I've come up with two things, one works almost like it should except that every traversal thru the string I've gotten it to repeat the "list" again. This is