samedi 14 mars 2015

Python newbie trying to recursively generate a pattern of numbers

I need to write a function that generates these patterns:



>>> pattern(1)
1
>>> pattern(2)
1
121
1
>>> pattern(3)
1
121
1
12321
1
121
1


This is what I have tried:



def pattern(n):
if n>=1:
pattern(n-1)
print(n,end='')
pattern(n-1)


and then I tried to iterate over it using:



>>>for i in range(3):
pattern(i)
print()

1
121


Can anyone help me understand this concept?


Aucun commentaire:

Enregistrer un commentaire