function - Recursive numeric triangle in python -


i'm trying create triangle following:

1 2 3 4 5 6 2 3 4 5 6 3 4 5 6 4 5 6 5 6 6 

without using while, in, lists, etc. "if-else" cases , recursive functions. i've learned how asterisk triangle.

def triangle(i, t=0):     if == 0:         return ' '     else:         print '*' *         return triangle( - 1, t + 1 )  triangle(6) 

it has same idea want apply exercise, don't know how code changing term term , print them right one.

here solution. note there neither range nor join, implies for or list

in [1]: def tri(size, row = 0, col = 0):    ...:     if row < size:    ...:         num = row + col + 1    ...:         if num == size + 1:    ...:             print '\n',    ...:             tri(size, row + 1, 0)    ...:         if num <= size:    ...:             print num, '',    ...:             tri(size, row, col + 1)    ...:  in [2]: tri(6) 1  2  3  4  5  6 2  3  4  5  6 3  4  5  6 4  5  6 5  6 6 

if range acceptable, here short one:

def tri2(size):     row = map(str, range(1, size + 1))     print '\n'.join(map(lambda n: ' '.join(row[n:]), range(size))) 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -