Lab 1: Algorithms

An algorithm is a sequence of steps that performs a task. In order to sucessfully program you must take the task that’s assigned and break it down into steps. When you have an idea of the steps required begin writing code. This notebook will use the pen class which will help you understand algorithms.

Drawing Shapes

How can you use the pen to draw shapes? Every shape is a sequence of pen strokes and turns. For example, think of a triangle. You can draw a triangle by:

  1. Drawing an initial line

  2. Turning 120 degrees

  3. Drawing a second line

  4. Turning 120 degrees

  5. Trawing a third line

Every closed shape has one important thing in common, the sum of all the turns equals 360 degress. A triangle has three turns of 120 degrees each. The code below draws a triangle.

[ ]:
from p4e.drawing import Turtle

You only have to run the import cell once unless you restart the python kernel.

[ ]:
tu = Turtle()
tu
[ ]:
tu.clear()
tu.draw(80)   # Draw the first line
tu.turn(120)
tu.draw(80)   # Draw the second line
tu.turn(120)
tu.draw(80)   # Draw the third line
tu.turn(120)

Lab

In the cells provided write code to implement the requested algorithms.

1. Write an algorithm that draws a square.

[ ]:

2. Write an algorithm that draws a five pointed star.

Hint: the sum of the angles should add to 720.

[ ]:

3. Write an algorithm that draws a capital “T”

[ ]:

4. Write an algorithm that draws a capital “A”

[ ]:

5. Escape the Maze!

Use pen commands to help the turtle escape the maze.

[ ]:
tu = Turtle()
tu.background('files/maze.png')
tu
[ ]: