【每日一题0907】金字塔

:woman_mage:请编写一个函数,接收一个正整数n,返回一个由其子列表组成的列表。子列表按长度升序排列。

示例:
输入:2,返回: [[1], [1, 1]]

题目难度:简单
题目来源:codewars

def pyramid(n: int) -> list:
    pass

assert pyramid(0) == []
assert pyramid(2), == [[1], [1, 1]]
assert pyramid(3) == [[1], [1, 1], [1, 1, 1]]
   /**
     * 请编写一个函数,接收一个正整数n,返回一个由其子列表组成的列表。子列表按长度升序排列。
     *
     * 示例:
     * 输入:2,返回: [[1], [1, 1]]
     * @param i
     * @return
     */
    public ArrayList<ArrayList<Integer>> pyramid(Integer i){
        ArrayList<ArrayList<Integer>> arrayList = new ArrayList<>();
        for (int j = 0; j < i; j++) {
            ArrayList<Integer> integers = new ArrayList<>();
            int step = j;
            while (step>=0) {
                integers.add(1);
                step--;
            }
            arrayList.add(integers);
        }
        return arrayList;
    }
def pyramid(n):
    list=[]
    list1=[]
    for i in range(n):
        for j in range(i+1):
            list1.append(1)
        list.append(list1)
        list1 = []
    print(list)
    return list
def pyramid(num: int) -> list:
    result_list = list()
    for i in range(1, num + 1):
        tmp_list = list()
        for j in range(i):
            tmp_list.append(num)
        result_list.append(tmp_list)
    return result_list
def pyramid(n: int) -> list:
    return [[1] * i for i in range(1, n + 1)]


if __name__ == "__main__":
    assert pyramid(0) == []
    assert pyramid(2) == [[1], [1, 1]]
    assert pyramid(3) == [[1], [1, 1], [1, 1, 1]]

image

关闭