1

I'm trying to create a triangle like the 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, for in, lists, etc. Just "if-else" cases and recursive functions. I've just learned how to do an asterisk triangle.

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

triangle(6)

It has the same idea I want to apply to my exercise, but I really don't know how to do with the code for changing term by term and print them all to the right like this one.

4 Answers 4

2

Here is my solution. Note that there is neither range nor join, which 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 is acceptable, then here is a short one:

def tri2(size):
    row = map(str, range(1, size + 1))
    print '\n'.join(map(lambda n: ' '.join(row[n:]), range(size)))
Sign up to request clarification or add additional context in comments.

Comments

0

You can use range() or xrange() to get the list of numbers, and decrease the range with each recursion:

def triangle(i, t):
    if i == t:
        return i
    else:
        print " ".join([str(x) for x in range(i,t+1)])
        return triangle( i + 1, t )

output:

>>> triangle(1,6)
1 2 3 4 5 6
2 3 4 5 6
3 4 5 6
4 5 6
5 6
6
>>> triangle(1,8)
1 2 3 4 5 6 7 8
2 3 4 5 6 7 8
3 4 5 6 7 8
4 5 6 7 8
5 6 7 8
6 7 8
7 8
8

Comments

0

By calling the function recursively You have realized a kind of loop. Now you can replicate the same idea:

def OneLess(i,j):
    print i, 
    if i < j: 
        OneLess(i+1,j)
    else:
        print ""

def triangle(i, t=1):
    OneLess(t,i)#print '*' * i
    if i == t:
        return ' '    
    return triangle( i , t + 1 )

triangle(6)

Comments

0

I'd suggest something like this:

def triangle(i, t = 1):
    if i > 0:
        print ' '.join([str(n+t) for n in range(i)])
        triangle( i - 1, t + 1 )

The range gives you a list of the numbers needed at each level, and the t offset is increased by one so you're starting from a higher value each level you go down.

Update

I've just noticed your requirement for no for in and lists, which probably makes the above example wrong. So here is another suggestion using only recursion:

def triangle(size, col = 1, row = 1):
    if col < size:
        print col,
        triangle(size, col+1, row)
    else:
        print col
        if row < size:
            triangle(size, row+1, row+1)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.