Most un-Pythonic and shameless solution


#1
class Solution:
# @param A : string
# @return a strings
def solve(self, A):
    n=len(A)
    i=0
    buffer=''
    words=[]
    while i<n:
        if A[i]==' ' and not buffer:
            j=i+1
            while j<n and A[j]==' ':
                j+=1
            if j==n:
                break
            i=j-1
        elif A[i]==' ' and buffer:
            words.append(buffer)
            buffer=''
        else:
            buffer+=A[i]
        i+=1
    if buffer:
        words.append(buffer)
    return ' '.join(words[::-1])