我做的練習如下:差之間2個Python代碼
# B. front_x
# Given a list of strings, return a list with the strings
# in sorted order, except group all the strings that begin with 'x' first.
# e.g. ['mix', 'xyz', 'apple', 'xanadu', 'aardvark'] yields
# ['xanadu', 'xyz', 'aardvark', 'apple', 'mix']
# Hint: this can be done by making 2 lists and sorting each of them
# before combining them.
樣品溶液:
def front_x(words):
listX = []
listO = []
for w in words:
if w.startswith('x'):
listX.append(w)
else:
listO.append(w)
listX.sort()
listO.sort()
return listX + listO
我的解決方案:
def front_x(words):
listX = []
for w in words:
if w.startswith('x'):
listX.append(w)
words.remove(w)
listX.sort()
words.sort()
return listX + words
,我測試了我的解決辦法,結果有點奇怪。以下是我的解決方案的源代碼:http://dl.dropbox.com/u/559353/list1.py。你可能想嘗試一下。
你有什麼特別的問題,除了「我的代碼出了什麼問題」? – Bobby 2010-12-22 11:02:05
-1:「結果有點不可思議」。含糊不清,難以回答。請提供您想要解決的具體問題。 – 2010-12-22 14:26:31