title Description: try to implement a function reversewords, the function passed in the parameter is a string, the return value is the word between the words to adjust the string (only the word order adjustment can be).
For example: The incoming parameter is "The sky is blue" and the adjusted string is "Blue is sky the".
Problem Solving Ideas: the string is converted into a character array and then adjusted in reverse order to "the sky is Blue", getting [' E ', ' u ', ' l ', ' B ', ', ' s ', ' I ', ', ' Y ', ' k ', ' s ', ', ' e ', ' H ' ', ' t ']; then look for the boundaries of each word, adjust each word in reverse order, and eventually concatenate into a string.
defReversewords (s):"""reverse the word between words"""s= S.strip (' ')#Remove First space ifs = = NoneorLen (s) = =0:returnNone S= List (s) [::-1] #Search for word boundaries and adjust the order of words in reverse orderStart =-1End=-1 forIinchRange (len (s)):ifS[i]! =' ': Start= Iif(i==0ors[i-1]==' ')ElseStart End= Iif(I==len (s)-1ors[i+1]==' ')ElseEndifStart! =-1 andEnd! =-1: Reverse (S, start, end) Start=-1End=-1return "'. Join (s)defreverse (s, start, end):"""Reverse Word""" whileStart <End:s[start], S[end]=S[end], S[start] Start+ = 1End-= 1
Lintcode Flip String problem-python implementation