【每日一题1025】蜗牛排序

给定一个n×n的正整数列表,请编写一个函数,按照顺时针方向,从外围到中心重新排列元素。

示例:
输入:[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
输出:[1, 2, 3, 6, 9, 8, 7, 4, 5]
image

题目难度:难
题目来源:codewars: Snail

def snail(nums: list) -> list:
    pass

assert [[1,2,3],[4,5,6],[7,8,9]] == [1,2,3,6,9,8,7,4,5]
assert [[1,2,3],[8,9,4],[7,6,5]] == [1,2,3,4,5,6,7,8,9]
1 个赞
def snail(nums: list) -> list:
    new_sort_list = []
    while True:
        new_sort_list += nums[0]
        del nums[0]
        if len(nums) == 0:
            break
        new_sort_list += [ns[len(ns) - 1] for ns in nums]
        for ns in nums:
            del ns[len(ns) - 1]
        new_sort_list += nums[len(nums) - 1][::-1]
        del nums[len(nums) - 1]
        if len(nums) == 0:
            break
        new_sort_list += [ns[0] for ns in nums][::-1]
        for ns in nums:
            del ns[0]
        if len(nums) == 0:
            break
    return new_sort_list


assert snail([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) == [1, 2, 3, 6, 9, 8, 7, 4, 5]
assert snail([[1, 2, 3], [8, 9, 4], [7, 6, 5]]) == [1, 2, 3, 4, 5, 6, 7, 8, 9]
assert snail([[1, 2, 3, 1], [4, 5, 6, 4], [7, 8, 9, 7], [7, 8, 9, 7]]) == [1, 2, 3, 1, 4, 7, 7, 9, 8, 7, 7, 4, 5, 6, 9,
                                                                           8]
def test(dobleArr, list):
    if len(dobleArr) > 0:
        for i in dobleArr:
            if dobleArr[0] == i:
                for j in i:
                    list.append(j)
            elif dobleArr[-1] == i:
                for j in reversed(i):
                    list.append(j)
            else:
                list.append(i[-1])
                i.remove(i[-1])
        dobleArr.remove(dobleArr[0])
        if len(dobleArr) > 0:
            dobleArr.remove(dobleArr[-1])
        for i in reversed(dobleArr):
            list.append(i[0])
            i.remove(i[0])
        test(dobleArr, list)
    return list
写复杂了 
def get_list(nums_list: list):
    # n数列
    n = len(nums_list)
    # 汇总数字
    temp_list = []
    for nums in nums_list:
        for num in nums:
            temp_list.append(num)
    # 初始开始索引
    x = 0
    # 初始结束索引
    y = n
    # 定义初始幅度为正
    is_plus = True
    # 总共走的步数为2n-1,循环步数
    times = 2 * n - 1
    step_list = []
    num_list = []
    # 获取步调集合
    for t in range(times):
        # 第奇数次循环,步频为n,否则为1步
        if t % 2 == 1:
            step = n
        else:
            step = 1
        # 判断是加还是减去
        for a in range(2, times, 4):
            if t == a:
                is_plus = False
                break
        for b in range(0, times, 4):
            if t == b:
                is_plus = True
                break

        # 如果是减,步频为负数
        if not is_plus:
            step = -step
        step_list.append(step)

    s_index = 0

    for s in step_list:
        #每一步打印数字的初始索引
        d = 0
        for i in range(x, y, s):
            num_list.append(temp_list[i])
            d = d + 1
        if s_index < len(step_list) - 1:
            step_item_next = step_list[s_index + 1]
            step_item = step_list[s_index]
            #定义下一步骤集合的倍数,用于计算集合的结束值
            if s_index % 2 == 0:
                e = d - 2
            else:
                e = d - 1
            if e < 0:
                e = 0
            if step_item < 0:
                x = y + 1 + step_item_next
            else:
                x = y - 1 + step_item_next
            if step_item_next > 0:
                y = x + e * step_item_next + 1
            else:
                y = x + e * step_item_next - 1

        s_index = s_index + 1
    print(num_list)