看到一个面试题 要求是倒序打印一句英语
例如’this is a book’ 转成 ‘book a is this’ 要考虑空间

写法①

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
str = 'this is a book'
list = list(str)

a = []
b = ''

for i in range(0,len(list)):

a.append(list[i])
if list[i] == ' ':
b = ' ' + ''.join(a[0:-1]) + b
a=[]

if i == (len(list)-1) :
b = ''.join(a) +b
print b

写法②

1
2
3
4
5
6
7
8
9
10
11
12
13
14
def descStr():

str = 'this is a book'
str = str.split(' ') #['this', 'is', 'a', 'book']
list = []

n = -1
for i in str:
list.insert(n,i)
n=n-1

print ' '.join(list)

descStr()

写法③

1
2
3
4
# coding:utf-8
string = "what's your name"
s1 = string.split()
s2 = s1[::-1]