Recursive Triangle
Problem to Solve
Recursion lets a function solve a problem by calling itself on a smaller version of that same problem. But the order in which a recursive function does its work, before or after the recursive call, can change its output entirely. How, exactly, does moving a single line of code change what a recursive function prints?
Consider the following function, print_triangle, which recursively outputs a triangular pattern:
def print_triangle(side_length):
if side_length < 1:
return
print_triangle(side_length - 1)
print("[]" * side_length)
Notice that print_triangle has two parts. The first is a base case: if side_length is less than 1, there’s nothing left to print, so the function simply returns. The second is a recursive case: the function calls itself with a slightly smaller side_length, then prints a single row of side_length pairs of brackets.
For example, print_triangle(4) produces the output below.
[]
[][]
[][][]
[][][][]
Distribution Code
For this problem, you’ll extend the functionality of code provided to you by CS50’s staff.
Download the distribution code
Log into cs50.dev, click on your terminal window, and execute cd by itself. You should find that your terminal window’s prompt resembles the below:
$
Next execute
wget https://cdn.cs50.net/2026/fall/psets/6/triangle.zip
in order to download a ZIP called triangle.zip into your codespace.
Then execute
unzip triangle.zip
to create a folder called triangle. You no longer need the ZIP file, so you can execute
rm triangle.zip
and respond with “y” followed by Enter at the prompt to remove the ZIP file you downloaded.
Now type
cd triangle
followed by Enter to move yourself into (i.e., open) that directory. Your prompt should now resemble the below.
triangle/ $
Execute ls by itself, and you should see a file:
triangle.py
If you run into any trouble, follow these same steps again and see if you can determine where you went wrong!
Specification
- In
triangle.py, you’ll find theprint_trianglefunction from above. Make minor modifications to it to create a new function namedprint_triangle_reversed, so that the lines of output are reversed from what the original function produces.- For example,
print_triangle_reversed(4)should output the following:[][][][] [][][] [][] []
- For example,
- Your solution must still be recursive. Solutions that use a loop (e.g.,
fororwhile) in place of recursion will not receive credit. - You should add a comment to
print_triangle_reversedexplaining why your change works. - If
side_lengthis less than 1,print_triangle_reversedshould print nothing at all.
Hints
- You shouldn’t need to write much new code! Think about the order in which the original function does its two jobs: making the recursive call and printing a row.
- It might help to trace through a small example by hand. Try writing out, step by step, what happens when you call
print_triangle(2): which call prints first, and why? - Recall that you can multiply a string by an integer in Python to repeat it. For instance,
"[]" * 3evaluates to"[][][]". -
To try out your function, you might temporarily add a line like the below to the bottom of your file, then run
python triangle.py:print_triangle_reversed(4)
How to Test
There is no check50 for this problem. Instead, your submission will be graded on whether print_triangle_reversed correctly uses recursion (not a loop) and whether your comment clearly explains why your change works. Be sure to test your code on your own for each of the following.
- Call
print_triangle_reversed(4). Your function should output four rows, starting with[][][][]and ending with[]. - Call
print_triangle_reversed(1). Your function should output a single row,[]. - Call
print_triangle_reversed(0). Your function should output nothing at all. - Call
print_triangle_reversed(-3). Your function should output nothing at all. - Call
print_triangle(4). If you left the original function in your file, it should still output its rows in the original order, starting with[]and ending with[][][][].
Style
style50 triangle.py
How to Submit
There is no autograder for this problem! Be sure to test it out yourself.
- Download your
triangle.pyfile by control-clicking or right-clicking on the file in your codespace’s file browser and choosing Download. - Go to CS50’s Gradescope page.
- Click Problem Set 6: Triangle.
- Drag and drop your
triangle.pyfile to the area that says Drag & Drop. Be sure it has that exact filename! If you upload a file with a different name, the autograder likely will fail when trying to run it. Ensuring you have uploaded files with the correct filename is your responsibility! - Click Upload.
You should see a message that says “Problem Set 6: Triangle submitted successfully!”