Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In this function:

Python
def print_triangle (sideLength):
   if sideLength < 1 :
     return
   print_triangle (sideLength-1)
   print ( "[]"* sideLength)


The instruction:

Python
print ( "[]"* sideLength)


Should never be reached, because the instruction:

Python
print_triangle (sideLength-1)


Would be preventing it to be reached as it keeps calling the function, which would send the flow of the program again to the beginning of the function, that is to the top. Instead of allowing it to keep going below to the:

Python
print ( "[]"* sideLength)


Hence, it would continue doing that until the variable
Python
sideLength 
becomes zero.

And yet the line:

Python
print ( "[]"* sideLength)


Is reached and draws a triangle when you call it with say a 4 as a parameter... but how?

Python
print_triangle (4)


Python
[]
[][]
[][][]
[][][][]


What I have tried:

I have seen numerous video-tutorials and read various website tutorials, but they just skip this particular kind of example, they do have similar examples but they just forget to explain this detail
Posted
Updated 13-Apr-22 20:53pm
v2

consider the simple case of the print_triangle(1) call
pseudocode
print_triangle(1):
  if 1 < 1 :
    return //since sideLength=1, then  sideLength < 1 is false, hence the execution goes on
  print_triangle(0) // inside print_triangle(0),  sideLength=0, then sideLength < 1 is true, hence it returns immediately
  print ( "[]"* 1)//  the statement print ( "[]"* 1) is executed, producing a [] on the screen.

Apply a similar line of reasoning to print_triangle(2) and so on, in order to discover the magic of recursion.
 
Share this answer
 
Comments
Patrice T 15-Apr-22 14:24pm    
+5
Quote:
Python, how an instruction can be done eventhough the recursion shouldn't allow it?

Your code flow is wrong.
Try :
Python
def print_triangle (sideLength):
   if sideLength > 1 :
     print_triangle (sideLength-1)
   print ( "[]"* sideLength)

The principle :
As long we are not on top of triangle, call the function for a triangle minus 1 row.
Then print last row of current triangle.

Try this code to see the execution of your code
Python
<pre>
def print_triangle (sideLength):
    print ( sideLength, " def print_triangle (sideLength)")
    print ( sideLength, " if sideLength < 1")
    if sideLength < 1 :
        print ( sideLength, " return")
        return
    print ( sideLength, " print_triangle (sideLength-1)")
    print_triangle (sideLength-1)
    print ( sideLength, " print ( ""[]"" * sideLength)")
    print ( "[]"* sideLength)
    
print_triangle (5)

Result is
5  def print_triangle (sideLength)
5  if sideLength < 1
5  print_triangle (sideLength-1)
4  def print_triangle (sideLength)
4  if sideLength < 1
4  print_triangle (sideLength-1)
3  def print_triangle (sideLength)
3  if sideLength < 1
3  print_triangle (sideLength-1)
2  def print_triangle (sideLength)
2  if sideLength < 1
2  print_triangle (sideLength-1)
1  def print_triangle (sideLength)
1  if sideLength < 1
1  print_triangle (sideLength-1)
0  def print_triangle (sideLength)
0  if sideLength < 1
0  return
1  print ( [] * sideLength)
[]
2  print ( [] * sideLength)
[][]
3  print ( [] * sideLength)
[][][]
4  print ( [] * sideLength)
[][][][]
5  print ( [] * sideLength)
[][][][][]
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900