這是Codewar的習(xí)題,我通過(guò)它來(lái)鍛煉編程技巧。
2018/1/4
這里的字符串join方法是給它一個(gè)可以迭代的,形式
" ".join(w.capitalize() for w in string.split())
返回值:
0+r1+r2+r3+r4+r5+r6+r7+r8+r9
2018/1/6
題目
https://www.codewars.com/kata/578aa45ee9fd15ff4600090d/solutions/python
You have an array of numbers.
Your task is to sort ascending odd numbers but even numbers must be on their places.
Zero isn't an odd number and you don't need to move it. If you have an empty array, you need to return it.
我的答案
def sort_array(source_array):
last = len(source_array)
for i in range(len(source_array)):
now = -1
for j in range(last):
if (now < 0):
if (source_array[j] != 0 and source_array[j] % 2 != 0):
now = j
continue
else:
continue
if (source_array[j] == 0 or source_array[j] % 2 == 0):
continue
last = j
if (source_array[now] < source_array[j]):
now = j
if(source_array[last]<source_array[now]):
source_array[last], source_array[now] = source_array[now], source_array[last]
return source_array
參考答案
def sort_array(arr):
odds = sorted((x for x in arr if x%2 != 0), reverse=True)
return [x if x%2==0 else odds.pop() for x in arr]