알고리즘 문제/CodeWar

Row Of The Odd Triangle

BEstyle 2022. 11. 15. 13:23

DESCRIPTION:

Given a triangle of consecutive odd numbers:

             1
          3     5
       7     9    11
   13    15    17    19
21    23    25    27    29
...

find the triangle's row knowing its index (the rows are 1-indexed), e.g.:

odd_row(1)  ==  [1]
odd_row(2)  ==  [3, 5]
odd_row(3)  ==  [7, 9, 11]

Note: your code should be optimized to handle big inputs.


def odd_row(n):
    start=pib(n)
    alist=[]
    for i in range(n):
        alist.append(start+(2*i))
    return alist

def pib(n):
    return n*(n-1)+1