是否有一個函數可用於查找具有最小ASCII值的字符串中的大寫字母。列表中的最小ASCII值
例如,假設該輸入字符串:
"Parker,Peter,Wayne,Bruce,Wilson,Wade"
# I want the function to return Parker,Peter \n Wayne,Bruce \n Wilson,Wade
# I know that I have to use the ord function in some sort of way,
# and is there a way to accomplish this task using the min function?
# I have tried doing this function with a while loop and it works with
# two names but not with any more.
def alphabetize(names):
T = ''
subscript = 0
names = names.split(",")
champ = ord(names[subscript][0])
while len(names) > 0:
if ord(names[subscript][0]) < champ:
T += (names[subscript])
T += " "
T += (names[subscript + 1])
T += "\n"
del names[subscript]
del names[subscript]
elif ord(names[subscript][0]) > champ:
T += (names[subscript])
T += " "
T += (names[subscript + 1])
T += "\n"
del names[subscript]
del names[subscript]
else:
T += (names[subscript])
T += " "
T += (names[subscript + 1])
T += "\n"
del names[subscript]
del names[subscript]
return T
print alphabetize("Kent,Clark,Wayne,Bruce")
預先感謝任何幫助。
編輯:sort()函數是不允許的。
此問題的規範之一是不允許使用sort()函數。 –
然後做你自己的家庭作業。然而,你的老師可能希望你實現你自己的排序算法 - 所以看看那些。 https://en.wikipedia.org/wiki/Sort_algorithm Quicksort,mergesort,heapsort可能對你非常有用 – teambob