【每日一题0701】数字列表排序

给你一个包含一些正整数和零的数字列表,例如[3,2,1,0,5,6,4,0,1,5,3,0,4,2,8,0],它可以被分解为一些零终止的子列表,如[3,2,1,0] ,[5,6,4,0] …。

亲爱的霍格沃茨魔法师,你的的任务是:

  1. 首先,按照升序对每个子列表进行排序(不要对零排序,它总是在最后) ;
  2. 其次,按照所有子列表的和值进行排序(也是升序)

结果及注意事项:

  1. 返回结果是列表
  2. 如果某些子序列具有相同的和值,则按照它们的原始顺序对它们进行排序

难度:中等
来源:codewars

def sort_list(nums: list):
    pass

assert sort_list([3,2,1,0,5,6,4,0,1,5,3,0,4,2,8,0]) == [1,2,3,0,1,3,5,0,2,4,8,0,4,5,6,0]
assert sort_list([3,2,1,0,5,6,4,0,1,5,3,0,2,2,2,0]) == [1,2,3,0,2,2,2,0,1,3,5,0,4,5,6,0]
assert sort_list([2,2,2,0,5,6,4,0,1,5,3,0,3,2,1,0]) == [2,2,2,0,1,2,3,0,1,3,5,0,4,5,6,0]
def sort_list(nums: list):
    a = []
    s = []
    for i in range(len(nums)):
        s.append(nums[i])
        if i == len(nums)-1 or nums[i]==0:
            a.append(s)
            s=[]
    for i in range(len(a)):
        for j in range(len(a)-1-i):
            x = 0
            y = 0
            for n  in  a[j] :
                x +=n
            for n in  a[j+1]:
                y+=n
            if x > y :
                a[j] , a[j+1] = a[j+1],a[j]
    for i in a:
        for x in range(len(i)):
            for y in range(len(i)-2-x):
                if i[y] > i[y+1]:
                    i[y],i[y+1] = i[y+1],i[y]

    return a
def sort_list(nums: list):
    split_list = []
    while True:
        try:
            split_list.append(sorted(nums[:nums.index(0)]))
            nums = nums[nums.index(0) + 1:]
        except:
            break
    sum_list = sorted(sum(l) for l in split_list)
    for x in split_list:
        x.append(0)
        sum_list[sum_list.index(sum(x))] = x
    return [j for i in sum_list for j in i]


assert sort_list([3, 2, 1, 0, 5, 6, 4, 0, 1, 5, 3, 0, 4, 2, 8, 0]) == [1, 2, 3, 0, 1, 3, 5, 0, 2, 4, 8, 0, 4, 5, 6, 0]
assert sort_list([2, 2, 2, 0, 5, 6, 4, 0, 1, 5, 3, 0, 3, 2, 1, 0]) == [2, 2, 2, 0, 1, 2, 3, 0, 1, 3, 5, 0, 4, 5, 6, 0]
assert sort_list([2, 2, 2, 0, 5, 6, 4, 0, 1, 5, 3, 0, 3, 2, 1, 0]) == [2, 2, 2, 0, 1, 2, 3, 0, 1, 3, 5, 0, 4, 5, 6, 0]

‘’’
def get_new_list(target_list) → list:
new_tart_list = ‘’.join([str(temp) for temp in target_list])
str_list = new_tart_list.split(“0”)
new_list =
for temp in str_list:
if temp:
temp_list =
for _i in temp:
temp_list.append(int(_i))
temp_list.append(0)
new_list.append(temp_list)
return new_list

def sort_by_sub_value(target_list: list):
list_temp: list = get_new_list(target_list)
for temp in list_temp:
for i in range(len(temp)):
for j in range(i + 1, len(temp) - 1):
if temp[i] > temp[j]:
temp[i], temp[j] = temp[j], temp[i]

return list_temp

def sort_by_sub_sum(target_list):
list_temp: list = get_new_list(target_list)
new_list =

for temp in list_temp:
    new_list.append((temp, sum(temp)))
for i in range(len(new_list)):
    for j in range(i + 1, len(new_list) - 1):
        if new_list[i][1] > new_list[j][1]:
            new_list[i], new_list[j] = new_list[j], new_list[i]
sort_list = [item[0] for item in new_list]

return sort_list

target_list = [3, 2, 1, 0, 5, 6, 4, 0, 1, 5, 3, 0, 4, 2, 8, 0]
res = sort_by_sub_sum(target_list)

print(res)

参考题解:

def sort_list(nums: list):
    group = [sorted(nums[n:n+3]) for n in range(0, len(nums), 4)]
    sorted_gp = sorted(group, key=lambda x:sum(x))
    
    result = []
    for item in sorted_gp:
        result += item + [0]
    return result

assert sort_list([3,2,1,0,5,6,4,0,1,5,3,0,4,2,8,0]) == [1,2,3,0,1,3,5,0,2,4,8,0,4,5,6,0]
assert sort_list([3,2,1,0,5,6,4,0,1,5,3,0,2,2,2,0]) == [1,2,3,0,2,2,2,0,1,3,5,0,4,5,6,0]
assert sort_list([2,2,2,0,5,6,4,0,1,5,3,0,3,2,1,0]) == [2,2,2,0,1,2,3,0,1,3,5,0,4,5,6,0]

:partying_face: :partying_face: :partying_face: :partying_face: :partying_face: :partying_face: