# Lab: 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()
display(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)


## Part 1: Drawing Algorithms¶

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

### 1. Draw a Square¶

Write an algorithm that draws a square.

[ ]:




### 2. Draw a Star¶

Write an algorithm that draws a five pointed star. Hint: the sum of the angles should add to 720.

[ ]:




### 3. Draw a “T”¶

Write an algorithm that draws a capital “T”

[ ]:




### 4. Draw an “A”¶

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')
display(tu)

[ ]:




## Part 2: Explore Turtle Commands¶

Use the Lesson 2 notebook to discover how to solve the challenges in this section.

### 1. Draw an “M”¶

Use the goto Turtle function to draw an “M”. Do not use draw or turn.

[ ]:




### 2. Draw an “X”¶

Use the goto function to draw an “X”. You will need to pick the pen up and put it down again. Do not use draw or turn.

[ ]:




### 3. Colorful Writing¶

Write your name three times using three different colors.

[ ]: