Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I don't know how to trace this code, the result is shown in the second pic with black color of triangles(assume we type depth =

image text in transcribedimage text in transcribedimage text in transcribedI don't know how to trace this code, the result is shown in the second pic with black color of triangles(assume we type depth = 3). So when we type depth = 3, and length = 100, and start tracing the code, when you call the draw_triangles_rec2(length /2 , depth -1) at the line 17, do you have to go back to the original function draw_triangles_rec2(length , depth) with length/100 (which will be 50), and (depth -1 which will be 2), and start checking the condition if depth >0 and go into the for loop again if the condition is true? so order of drawing this graphic is draw blue line , then green line, then start drawing complete red triangle and so on... I dont know how does it start drawing the smallest triangle first? please help me how to trace it and how to draw it step by step, just take depth = 3 and length = 100 in this case. Thanks !

def draw_triangles_rec2(length: int, depth: int) -> int: Draw the recursive triangle. 6 iparam length: length of side of triangle :param depth: the depth of recursion we are currently at (counting down 8. 9. to 0) : return: None 10 11 12 sum = 0 if depth > 0: 13 for _ in range(3): # turtle.pencolor(COLORS[depth-1]) turtle.forward(length) sum += length + draw_triangles_rec2(length / 2, depth - 1) turtle.left(120) 14 15 16 17 18 19 return sum 20 21 def main(): 22 IIIIII 23 $ python3 triangles depth : return: 24 25 26 length = 100 27 depth = int(input("Enter the depth: ")) print('sum:', draw_triangles_rec2(length, depth)) turtle.update() turtle.mainloop() 28 29 30 31 main()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Management Databases And Organizations

Authors: Richard T. Watson

3rd Edition

0471418455, 978-0471418450

More Books

Students also viewed these Databases questions

Question

Persuasive Speaking Organizing Patterns in Persuasive Speaking?

Answered: 1 week ago