Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^3: Five Ways to Reverse a String of Words (C#, Perl 5, Perl6, Ruby, Haskell)

by Anonymous Monk
on Jun 05, 2007 at 13:41 UTC ( #619391=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Five Ways to Reverse a String of Words (C#, Perl 5, Perl6, Ruby, Haskell)
in thread Five Ways to Reverse a String of Words (C#, Perl 5, Perl 6, Ruby, Haskell)

[word for word in words.split()] ?
No need for a list comprehensions here. How about this...
def reverseWords(words): return ' '.join(words.split()[::-1])


Comment on Re^3: Five Ways to Reverse a String of Words (C#, Perl 5, Perl6, Ruby, Haskell)
Select or Download Code
Re^4: Five Ways to Reverse a String of Words (C#, Perl 5, Perl6, Ruby, Haskell)
by paddy3118 (Acolyte) on Aug 15, 2007 at 08:40 UTC
    It is sometimes hard to not use ::-1 once learnt, but you can use reversed which makes it clearer:
    >>> def reverseWords(word):
    ... 	return " ".join( reversed(word.split()) )
    ... 
    >>> reverseWords("  one   two three four    ")
    'four three two one'
    

    - Paddy

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://619391]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (11)
As of 2014-08-21 17:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (139 votes), past polls