задание из codewars

Вот само задание:

Task:
You have to write a function pattern which returns the following Pattern(See Pattern & Examples) upto n number of rows.

Note:Returning the pattern is not the same as Printing the pattern.
Rules/Note:
If n < 1 then it should return "" i.e. empty string.
There are no whitespaces in the pattern.
Pattern:
1
22
333
....
.....
nnnnnn
Examples:
pattern(5):

1
22
333
4444
55555
pattern(11):

1
22
333
4444
55555
666666
7777777
88888888
999999999
10101010101010101010
1111111111111111111111
Hint: Use \n in string to jump to next line

Вот мой код:

def pattern(n):
    result = ""
    if n<=0:
        return ""
    elif n>0:
        result=''
        counter=0
        while n!=0:
            counter=counter+1
            n=n-1
            result=result+((str(counter)*counter)+r"\n")
        
        f=list(result)
        f.pop(-1)
        f.pop(-1) 
        f=' '.join(f)
        print(f.replace(" ", ""))
        print(type(f))

Я начинающий, по сути это, наверняка, можно сделать в одну строку, но мне в голову пришла идея сделать именно так :) Скажите, пожалуйста, что тут не так


Ответы (1 шт):

Автор решения: Sergey Tatarincev

безусловно это задача в одну строку. для решения этой задачи вам надо внимательно изучить темы генераторов и тему работы со строками

n=10
res = '\n'.join([str(x)*x for x in range(1,n)])
print(res)
→ Ссылка