【每日一题20220616】奇数排序

:mage:‍ 给定一个数字列表,请在保持偶数原来的位置不变的前提下,将其中的奇数按照升序排列。返回排列后的结果。

【示例】
输入:[5, 8, 6, 3, 4]
输出:[3, 8, 6, 5, 4]
解释:其中的奇数有5和3,经过升序排列后,3在前,5在后,其余偶数不动。所以最终顺序是[3, 8, 6, 5, 4]

题目难度:中等
题目来源:codewars-Sort the odd

def solution(nums: list)-> list:
    # your code here

assert solution([7, 1]) == [1, 7]
assert solution([5, 8, 6, 3, 4]) == [3, 8, 6, 5, 4]
assert solution([9, 8, 7, 6, 5, 4, 3, 2, 1, 0]) == [1, 8, 3, 6, 5, 4, 7, 2, 9, 0]

先来个low的

def solution(nums: list) -> list:
    # your code here
    tmp = []
    index_list = []
    for k, v in enumerate(nums):
        if v % 2 != 0:
            index_list.append(k)
            tmp.append(v)
    tmp.sort()
    for _index in range(len(tmp)):
        nums[index_list[_index]] = tmp[_index]
    return nums


assert solution([7, 1]) == [1, 7]
assert solution([5, 8, 6, 3, 4]) == [3, 8, 6, 5, 4]
assert solution([9, 8, 7, 6, 5, 4, 3, 2, 1, 0]) == [1, 8, 3, 6, 5, 4, 7, 2, 9, 0]

def solution(nums: list) -> list:
    ji_list = list(filter(lambda x: x % 2 == 1, nums))
    s = sorted(ji_list)
    for i in range(len(s) - 1):
        for j in range(len(s) - 1 - i):
            if nums.index(s[j]) > nums.index(s[j + 1]):
                nums[nums.index(s[j])], nums[nums.index(s[j + 1])] = s[j + 1], s[j]
    return nums

assert solution([7, 1]) == [1, 7]
assert solution([5, 8, 6, 3, 4]) == [3, 8, 6, 5, 4]
assert solution([9, 8, 7, 6, 5, 4, 3, 2, 1, 0]) == [1, 8, 3, 6, 5, 4, 7, 2, 9, 0]
def solution(nums: list)-> list:
    # your code here
    odds = [i for i in nums if i%2 != 0]
    odds.sort(reverse=True)
    return [a if a%2 ==0 else odds.pop() for a in nums]


# print(solution([1, 7]))

assert solution([7, 1]) == [1, 7]
assert solution([5, 8, 6, 3, 4]) == [3, 8, 6, 5, 4]
assert solution([9, 8, 7, 6, 5, 4, 3, 2, 1, 0]) == [1, 8, 3, 6, 5, 4, 7, 2, 9, 0]
2 个赞
def solution(nums: list) -> list:
    v_list = [v for v in nums if v % 2 != 0]
    v_list.sort()
    j = 0
    for i,v in enumerate(nums):
        if v % 2 != 0:
            nums[i] = v_list[j]
            j += 1
    return nums


assert solution([7, 1]) == [1, 7]
assert solution([5, 8, 6, 3, 4]) == [3, 8, 6, 5, 4]
assert solution([9, 8, 7, 6, 5, 4, 3, 2, 1, 0]) == [1, 8, 3, 6, 5, 4, 7, 2, 9, 0]
def solution(nums: list)-> list:
    # your code here
    indexlist = []
    for i in range(len(nums)):
        if nums[i] % 2 != 0:
            indexlist.append(i)
    for i in range(len(indexlist)):
        for j in range(i+1,len(indexlist)):
            if nums[indexlist[i]]>nums[indexlist[j]]:
                nums[indexlist[i]],nums[indexlist[j]] = nums[indexlist[j]],nums[indexlist[i]]
    return nums
assert solution([7, 1]) == [1, 7]
assert solution([5, 8, 6, 3, 4]) == [3, 8, 6, 5, 4]
assert solution([9, 8, 7, 6, 5, 4, 3, 2, 1, 0]) == [1, 8, 3, 6, 5, 4, 7, 2, 9, 0]
def solution(nums: list)-> list:
    odd = []
    index = []
    for i, value in enumerate(nums):
        if value % 2 != 0:
            odd.append(value)
            index.append(i)

    odd.sort()
    for i, value in enumerate(odd):
        nums[index[i]] = value

    return nums


if __name__ == '__main__':
    assert solution([7, 1]) == [1, 7]
    assert solution([5, 8, 6, 3, 4]) == [3, 8, 6, 5, 4]
    assert solution([9, 8, 7, 6, 5, 4, 3, 2, 1, 0]) == [1, 8, 3, 6, 5, 4, 7, 2, 9, 0]

先冒个泡,codewar上还不能完全通过。

def solution(nums: list)-> list:
    # your code here
    m = 0
    odd_nums = None
    while m < len(nums):
        for j in range(len(nums)-1, m-1, -1):
            if nums[j] % 2 != 0 and odd_nums == None:
                odd_nums = nums[j]
            elif nums[j] % 2 != 0 and odd_nums != None:
                if odd_nums <= nums[j]:
                    nums[nums.index(odd_nums)], nums[j] = nums[j], odd_nums
        odd_nums = None
        m += 1
    return nums

只对奇数排序的话,冒泡可以通过

我冒得还有点问题,我再想想。

def solution(nums: list) -> list:
    # your code here
    odds_dict = {}
    for i, e in enumerate(nums):
        if e % 2 != 0:
            odds_dict[i] = e
    k = list(odds_dict.keys())
    v = list(odds_dict.values())
    k.sort()
    v.sort()
    for i, e in zip(k, v):
        nums[i] = e
    return nums

def solution(nums:list):
list_old = list(filter(lambda v : v % 2==1,nums))
list_new = sorted(list_old)
v=0
for i,j in enumerate(nums): #遍历num数组
if j % 2 == 1:
nums[i] = list_new[v]
i+=1
v+=1
return nums

assert solution([7, 1]) == [1, 7]
assert solution([5, 8, 6, 3, 4]) == [3, 8, 6, 5, 4]
assert solution([9, 8, 7, 6, 5, 4, 3, 2, 1, 0]) == [1, 8, 3, 6, 5, 4, 7, 2, 9, 0]

太厉害了

def solution(nums: list)-> list:
    odd_index=[i for i in range(len(nums)) if nums[i]%2 != 0]
    odd = [nums[i] for i in range(len(nums)) if nums[i]%2 != 0]
    odd.sort()
    for i in list(zip(odd_index,odd)):
        nums[i[0]]=i[1]
    return nums

assert solution([7, 1]) == [1, 7]
assert solution([5, 8, 6, 3, 4]) == [3, 8, 6, 5, 4]
assert solution([9, 8, 7, 6, 5, 4, 3, 2, 1, 0]) == [1, 8, 3, 6, 5, 4, 7, 2, 9, 0]
def solution(nums: list)-> list:
    old_odd_num=[i for i in nums if i%2!=0]
    new_odd_num=old_odd_num.copy()
    new_odd_num.sort()
    return [new_odd_num[old_odd_num.index(i)] if i in old_odd_num else i for  i in nums]
def solution(nums: list)-> list:
    choose_num = [x for x in nums if x%2!=0]
    choose_num.sort(reverse=True)
    return [y if y%2==0 else choose_num.pop() for y in nums]

assert solution([7, 1]) == [1, 7]
assert solution([5, 8, 6, 3, 4]) == [3, 8, 6, 5, 4]
assert solution([9, 8, 7, 6, 5, 4, 3, 2, 1, 0]) == [1, 8, 3, 6, 5, 4, 7, 2, 9, 0]
def solution(nums: list) -> list:
    # your code here
    for i in range(0, len(nums)):
        if nums[i] % 2 != 0:
            for j in range(i+1, len(nums)):
                if nums[j] % 2 != 0 and nums[i] > nums[j]:
                    nums[i], nums[j] = nums[j], nums[i]
    return nums
    # odds = [i for i in nums if i%2 != 0]
    # odds.sort(reverse=True)
    # return [a if a % 2 ==0 else odds.pop() for a in nums]


assert solution([7, 1]) == [1, 7]
assert solution([5, 8, 6, 3, 4]) == [3, 8, 6, 5, 4]
assert solution([9, 8, 7, 6, 5, 4, 3, 2, 1, 0]) == [1, 8, 3, 6, 5, 4, 7, 2, 9, 0]
def solution0722(nums: list)-> list:
    #解法1
    odd_value = []
    odd_index = []
    for index,value in enumerate(nums):
        if value % 2 != 0:
            odd_index.append(index)
            odd_value.append(value)
    odd_value.sort()
    for i,v in enumerate(odd_index):
        nums[v]=odd_value[i]
    return nums
   #解法2
   #插入排序
    cur_index = 1
    while cur_index<len(nums):
        if nums[cur_index] % 2 != 0:
            i = cur_index-1
            while i>=0:
                if nums[i] % 2 != 0:
                    tmp = nums[cur_index]
                    if nums[i]>tmp:
                        nums[i],nums[cur_index]=nums[cur_index],nums[i]
                        cur_index = i
                    else:
                        break
                i -= 1
        cur_index += 1
    return nums
def solution(nums: list) -> list:
    res_list = [nums[index] if nums[index] % 2 == 0 else 'x' for index in range(len(nums))]
    ji_list = sorted([nums[index] for index in range(len(nums)) if nums[index] % 2 != 0],reverse=False)
    for i in range(len(res_list)):
        if res_list[i]=='x':
            res_list[i]=ji_list[0]
            ji_list.pop(0)
    return res_list

assert solution([7, 1]) == [1, 7]
assert solution([5, 8, 6, 3, 4]) == [3, 8, 6, 5, 4]
assert solution([9, 8, 7, 6, 5, 4, 3, 2, 1, 0]) == [1, 8, 3, 6, 5, 4, 7, 2, 9, 0]