Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > RE: invert or reverse a string... warning this is a rant

Reply
Thread Tools

RE: invert or reverse a string... warning this is a rant

 
 
Demel, Jeff
Guest
Posts: n/a
 
      10-19-2006
John Salerno wrote:
>'m not steeped enough in daily programming to argue that it sn't
>necessary, but my question is why do you need to reverse strings? Is it


>something that happens often enough to warrant a method for it?


I've been programming professionally for over 10 years, and have never
once needed to reverse a string. Maybe it's a lack of imagination on my
part, but I can't think of a single instance this might be necessary.

However, if I did ever run across this need while using Python, it would
seem awkward to use [::-1], just from a clarity standpoint. A
string.reverse() method would be a decent thing to have.

Just my $.02.

-Jeff

P.S. How about a string.shuffle() method that splits the string in half
into two new strings str1 and str2, and then recompiles the string by
alternating one character from each str1 and str2 as it goes? Like
shuffling cards.
This email is intended only for the individual or entity to which it is addressed. This email may contain information that is privileged, confidential or otherwise protected from disclosure. Dissemination, distribution or copying of this e-mail or any attachments by anyone other than the intended recipient, or an employee or agent responsible for delivering the message to the intended recipient, is prohibited. If you are not the intended recipient of this message or the employee or agent responsible for delivery of this email to the intended recipient, please notify the sender by replying to this message and then delete it from your system. Any use, dissemination, distribution, or reproduction of this message by unintended recipients is strictly prohibited and may be unlawful.
 
Reply With Quote
 
 
 
 
Paul McGuire
Guest
Posts: n/a
 
      10-19-2006

"Demel, Jeff" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
John Salerno wrote:
P.S. How about a string.shuffle() method that splits the string in half
into two new strings str1 and str2, and then recompiles the string by
alternating one character from each str1 and str2 as it goes? Like
shuffling cards.


You mean:

"".join(sum(map(list,zip(s,s[len(s)/2:])),[]))

perhaps?

>>> s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
>>> "".join(sum(map(list,zip(s,s[len(s)/2:])),[]))

'ANBOCPDQERFSGTHUIVJWKXLYMZ'
>>>


-- Paul


 
Reply With Quote
 
 
 
 
J. Clifford Dyer
Guest
Posts: n/a
 
      10-19-2006
Demel, Jeff wrote:

> P.S. How about a string.shuffle() method that splits the string in half
> into two new strings str1 and str2, and then recompiles the string by
> alternating one character from each str1 and str2 as it goes? Like
> shuffling cards.


Jeff,

To my mind that would be the exact opposite of shuffling a deck of
cards. In your case, each time, you know exactly which stack the card
comes from, but not which card out of the stack. When you shuffle
cards, it's more like you know exactly which card is coming next off of
each stack, you just don't know which stack it's pulling from next.

import random

def shuffle(seq):
midpoint = len(seq)/2
stack1 = seq[:midpoint]
stack2 = seq[midpoint:]
shuffled = []
while stack1 and stack2:
next = random.choice((stack1, stack2))
shuffled.append(next.pop())
if stack1:
shuffled.extend(stack1)
else:
shuffled.extend(stack2)
return shuffled
 
Reply With Quote
 
Duncan Booth
Guest
Posts: n/a
 
      10-20-2006
"Demel, Jeff" <(E-Mail Removed)> wrote:

> I've been programming professionally for over 10 years, and have never
> once needed to reverse a string. Maybe it's a lack of imagination on my
> part, but I can't think of a single instance this might be necessary.


I did want to reverse some strings once, but it may have been nearly 10
years ago now.

If I remember correctly the situation was that I wanted to store a lot of
strings on a pda with limited memory and no gzip library. One way of
doing simple compression would be to store them sorted with the first byte
simply containing the number of characters in common with the previous
string. It happened that the strings in question had common suffixes more
than prefixes, so I stored them sorted by the reverse of the strings with a
lead byte containing the number of common characters at the tail of the
string.

Shortly afterwards I ported gzip to the target platform.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
invert or reverse a string... warning this is a rant rick Python 42 10-23-2006 11:27 PM
RE: invert or reverse a string... warning this is a rant Demel, Jeff Python 7 10-20-2006 08:31 PM
Re: invert or reverse a string... warning this is a rant Tim Chase Python 3 10-20-2006 07:01 AM
OT: <Rant> .... </Rant> forum theodore_blovius@yahoo.co.uk Digital Photography 0 02-15-2006 08:39 AM
spam increasing? <rant></rant> T.N.O. NZ Computing 9 11-21-2003 09:57 AM



Advertisments