Codewar每日學(xué)習(xí)

這是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]
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容