Reverse string word order without str.split () allowed

What is the pythonic way to do this?

From this: "This is a line to try": "try to execute a a This This line"

My first guess:

for w in 'This is a string to try'.split(' ')[::-1]:
    print w,

but str.split () is not allowed. Then I came up with this:

def reverse_w(txt):
    tmp = []
    while (txt.find(' ') >= 0):
        tmp.append(txt[:txt.find(' ')])
        txt = txt[txt.find(' ')+1:]
    if (txt.find(' ') == -1):
        tmp.append(txt)
   return tmp[::-1]
+5
source share
9 answers
def reverse(sentence):
sentence = 'This is a string to try'
    answer = ''
    temp = ''
    for char in sentence:
        if char != ' ':
            temp += char
        else:
            answer = temp + ' ' + answer
            temp = ''
    answer = temp + ' ' + answer
    return answer
+3
source

Here is the implementation of O (n) (does not use concatenation through +):

def reverse_w(txt):
    words = []
    word = []

    for char in txt:
        if char == ' ':
            words.append(''.join(word))
            word = []
        else:
            word.append(char)
    words.append(''.join(word))

    return ' '.join(reversed(words))

This implements a literal separation algorithm - manually breaking a string into words and then changing the list of words.

+3
source

, , , . , Python , :

s = "Strings!"
sOne = s[1] // == "t"
0
>>> import re
>>> s = 'This is a string to try'
>>> z = re.split('\W+', s)
>>> z.reverse()
>>> ' '.join(z)
'try to string a is This'

( "import re" ) :

>>> reduce(lambda x, y: u'%s %s' % (y, x), re.split('\W+', 'This is a string to try'))
u'try to string a is This'
0

re

import re
myStr = "Here is sample text"
print " ".join(re.findall("\S+",myStr)[::-1])
0

string.partition :

def reversed_words(s):
    out = []
    while s:
        word, _, s = s.partition(' ')
        out.insert(0, word)
    return ' '.join(out)

string.find:

def reversed_words(s):
    out = []
    while s:
        pos = s.find(' ')
        if pos >= 0:
            word, s = s[:pos], s[pos+1:]
        else:
            word, s = s, ''
        out.insert(0, word)
    return ' '.join(out)
0

Python, . reversed, [::-1] .split().

Python 2.7 ( ):

def revwords(sentence):
    word = []
    words = []

    for char in sentence:
        if char == ' ':
            words.insert(0,''.join(word))
            word = []
        else:
            word.append(char)
    words.insert(0,''.join(word))

    return ' '.join(words)
0

:

def reverse(sentence):
    answer = ''
    temp = ''
    for char in sentence:
        if char != ' ':
            temp += char
            continue
        rev = ''
        for i in range(len(temp)):
            rev += temp[len(temp)-i-1]
        answer += rev + ' '
        temp = ''
    return answer + temp
reverse("This is a string to try")
0

: , str.split , :-) , , .

>>> s = 'This is a string to try'
>>> r = s.split(' ')
['This', 'is', 'a', 'string', 'to', 'try']
>>> r.reverse()
>>> r
['try', 'to', 'string', 'a', 'is', 'This']
>>> result = ' '.join(r)
>>> result
'try to string a is This'

: , .

-3

All Articles