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. On Thu, 19 Oct 2006 11:30:24 -0500, "Demel, Jeff"
    <> declaimed the following in
    comp.lang.python:

    >
    > 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.
    >

    ONE usage... The old intro-to-programming Palindrome detector
    becomes a very simple assignment given an easy means of reversing a
    string (vs the "expected" loop comparing from the ends in to the center)
    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
     
    Dennis Lee Bieber, Oct 19, 2006
    #2
    1. Advertising

  3. Dennis Lee Bieber wrote:

    > ONE usage... The old intro-to-programming Palindrome detector
    > becomes a very simple assignment given an easy means of reversing a
    > string (vs the "expected" loop comparing from the ends in to the center)


    you mean like

    >>> s = "a man a plan a canal panama"
    >>> if list(s) != list(reversed(s)):

    .... print repr(s), "is not a palindrome"
    ....
    'a man a plan a canal panama' is not a palindrome

    ?

    </F>
     
    Fredrik Lundh, Oct 19, 2006
    #3
  4. "Fredrik Lundh" <>wrote:
    8<---------------------------
    > 'a man a plan a canal panama' is not a palindrome
    >
    > ?


    not if spaces count -

    able was I ere I saw elba - is one - but its a tougher test...

    - Hendrik
     
    Hendrik van Rooyen, Oct 20, 2006
    #4
  5. Hendrik van Rooyen wrote:

    >> 'a man a plan a canal panama' is not a palindrome

    >
    > not if spaces count -


    which they do if you rely on a single reverse operation (my point was
    that it takes a bit more work than that to check for a palindrome).

    </F>
     
    Fredrik Lundh, Oct 20, 2006
    #5
  6. Demel, Jeff

    Carl Banks 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.


    Say you're using a function from a third-party library that finds the
    first character in a string that meets some requirement. You need to
    find the last such character.


    Carl
     
    Carl Banks, Oct 20, 2006
    #6
  7. Carl Banks wrote:

    > Say you're using a function from a third-party library that finds the
    > first character in a string that meets some requirement. You need to
    > find the last such character.


    You seem to imply that invoking a function on a reversed input is
    somehow a generic solution to the problem (finding first vs finding
    last) that you describe above above.

    That would be the exception rather than the rule.

    i.
     
    Istvan Albert, Oct 20, 2006
    #7
  8. Demel, Jeff

    Carl Banks Guest

    Istvan Albert wrote:
    > Carl Banks wrote:
    >
    > > Say you're using a function from a third-party library that finds the
    > > first character in a string that meets some requirement. You need to
    > > find the last such character.

    >
    > You seem to imply that invoking a function on a reversed input is
    > somehow a generic solution to the problem (finding first vs finding
    > last) that you describe above above.


    Nope, I wasn't.

    > That would be the exception rather than the rule.


    Oh, don't be such a Debbie Downer. The rule is, it works as long as
    the character you're looking for isn't relative to any other character
    in the string.

    (Geez, I was just giving a counterexample.)


    Carl Banks
     
    Carl Banks, Oct 20, 2006
    #8
    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:
    728
    osmium
    Oct 10, 2003
  2. rtilley

    invert the order of a string

    rtilley, Feb 13, 2006, in forum: Python
    Replies:
    10
    Views:
    9,805
    Bruno Desthuilliers
    Feb 14, 2006
  3. rick
    Replies:
    42
    Views:
    2,118
    DaveM
    Oct 24, 2006
  4. Demel, Jeff
    Replies:
    3
    Views:
    296
    Duncan Booth
    Oct 20, 2006
  5. Tim Chase
    Replies:
    3
    Views:
    247
    Simon Forman
    Oct 20, 2006
Loading...

Share This Page