Tracy needs to draw a circle inside a square.

Which of the following programs solves the problem at the highest level of abstraction? (Assume all functions being used have been previously defined.)

forward(50)
left(90)
forward(50)
left(90)
forward(50)
left(90)
forward(50)
left(90)
forward(25)
circle(25)

draw_square()
draw_circle()

draw_square()
forward(25)
circle(25)

for i in range(4):
forward(50)
left(90)
forward(25)
circle(25)

1 answer

To determine which of the provided programs solves the problem at the highest level of abstraction, we need to consider how the functions are structured and how clearly they communicate their intent.

  1. Program 1:

    forward(50)
    left(90)
    forward(50)
    left(90)
    forward(50)
    left(90)
    forward(50)
    left(90)
    forward(25)
    circle(25)
    

    This program explicitly defines each movement to draw a square and then draws a circle. While it works, it is very low-level because it describes each action step by step.

  2. Program 2:

    draw_square()
    draw_circle()
    

    This program is at a higher level of abstraction because it uses function calls (draw_square() and draw_circle()) that likely encapsulate the details of how to draw a square and circle. This abstracts away the specifics of the drawing operations.

  3. Program 3:

    draw_square()
    forward(25)
    circle(25)
    

    This program is somewhat higher than Program 1 because it uses a predefined function to draw the square but still includes a low-level instruction (forward(25)) without clearly abstracting everything.

  4. Program 4:

    for i in range(4):
        forward(50)
        left(90)
    forward(25)
    circle(25)
    

    This program uses a loop to create the square, which is more abstract than just listing out each command, but it still does not abstract the circle drawing away into a function.

Based on these analyses, Program 2 is the one that solves the problem at the highest level of abstraction as it utilizes function calls that likely encapsulate the details of drawing, completely abstracting away the actual movements and operations needed to draw the shapes.