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

Discussion in 'Python' started by Demel, Jeff, Oct 19, 2006.

  1. Demel, Jeff

    Demel, Jeff Guest

    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.
     
    Demel, Jeff, Oct 19, 2006
    #1
    1. Advertising

  2. Demel, Jeff

    Paul McGuire Guest

    "Demel, Jeff" <> wrote in message
    news:...
    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
     
    Paul McGuire, Oct 19, 2006
    #2
    1. Advertising

  3. OT--Shuffling, was Re: invert or reverse a string... warning thisis a rant

    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
     
    J. Clifford Dyer, Oct 19, 2006
    #3
  4. Demel, Jeff

    Duncan Booth Guest

    "Demel, Jeff" <> 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.
     
    Duncan Booth, Oct 20, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dogbite
    Replies:
    4
    Views:
    694
    osmium
    Oct 10, 2003
  2. rtilley

    invert the order of a string

    rtilley, Feb 13, 2006, in forum: Python
    Replies:
    10
    Views:
    9,756
    Bruno Desthuilliers
    Feb 14, 2006
  3. rick
    Replies:
    42
    Views:
    2,089
    DaveM
    Oct 24, 2006
  4. Demel, Jeff
    Replies:
    7
    Views:
    275
    Carl Banks
    Oct 20, 2006
  5. Tim Chase
    Replies:
    3
    Views:
    232
    Simon Forman
    Oct 20, 2006
Loading...

Share This Page