# Algorithms - Lesson 1: Algorithms Solve Problems

## Overview

Students will complete two exploratory activities that introduce the concept of a problem and an algorithm. In the first students answer a series of questions about birthdates and names of their classmates. They then discuss the similarities and differences between the problems. In the second activity students are given six different algorithms and must analyze them to determine which they think are the same or different. At the end of the lesson they are introduced to the formal definitions of a problem and an algorithm.

### Goals

Students will be able to:

- Explain the formal definitions of a problem, an algorithm, sequencing, selection, and iteration.
- Explain that some problems may look different but be similar or look similar but be different.
- Explain that some algorithms may look or operate differently but still solve the same problem.

### Purpose

This lesson is an approachable and interactive introduction to the main concepts of this short unit. Students have been writing a lot of code, and now they are ready to think on a high level about the patterns that make two different problems, or two different algorithms, similar or different. This mindset will be important as they tackle the more challenging material later in the unit where students will learn to compare different algorithms that address the same problem.

## Getting Started (5 minutes)

**Prompt: ** What makes two pieces of code "the same"? Could there ever be two pieces of code that you consider to be "the same" even if they aren't identical?

###### Discussion Goal

This is an optional prompt. If you are able to move directly to the main activity you should do so. This prompt should get students thinking about the themes of the lesson.

There are no "wrong answers" here though you should expect answers that focus on the fact that often there's lots of ways to write code that does the same thing.

Have students think about their answers on their own, then share with a partner, and then finally discuss responses with the entire room.

## Activity (30 minutes)

### Remarks

- Today we are going to kick off a short unit about how computer scientists think about problem solving. A really important skill will be recognizing patterns and similarities.

**Do This:** Ask students to take out their journals or give them some blank paper for working on the following problems

**Display:** Show students the ten problems students will need to solve:

```
1. Find a person whose birthday is before yours
2. Find a person whose birthday is after yours
3. Find the person whose birthday is the closest before yours
4. Find the person whose birthday is the closest after yours
5. Find the person whose birthday is closest to yours
6. Find the person with an equal number of birthdays before and after theirs
7. Find the two people with the closest birthdays in the room
8. Find the shortest period of time in which three people have birthdays
9. Find the shortest period of time in which four people have birthdays
10. Find the longest period of time in which no one has a birthday
```

**Circulate:** Ask students to review the problems for one minute, and then let them move around the room collecting information needed to solve the problems. This may take them several minutes.

**Prompt:** Which problems did you need to do something similar in order to solve them?

**Discuss:** Have students discuss the prompt with a neighbor before asking them to share with the room. Lead a discussion on their experiences.

###### Discussion Goal

This discussion should focus on what made the problems that students solved similar to one another. You likely will want to put the problems back on the screen to make it easier to talk through. Here are some connections you may pull out though there are more students may make.

- Problems 1 and 2 are very similar. As soon as you find one person who meets the criteria you know you're done.
- Problem 3 and 4 are very similar but you need to talk to every other student to answer it. You only need to keep track of the closest birthday you've heard so far, however.
- Problem 5 is easy to solve as soon as you've solved problems 3 and 4.
- Problem 6 - 10 require you to have written down everyone's birthday, likely in order.
- Problems 7 - 9 are the same problem but for different numbers of people. Whatever strategy you use for one of those would be helpful to solve the others
- Problem 10 is a different version of problem 7 but instead of finding the smallest gap you're finding the largest.

- The first thing that we need to think about as computer scientists is what is a "problem". We just looked at 10 problems, but as we discussed, a lot of them are similar. If we solve one problem we may actually solve another, or at least have a good idea for how to start solving another. As computer scientists it's important to ask "have I seen this problem before" or "how is this problem similar to others I've solved?"
- We just thought about whether problems are similar. Now we're going to look at whether we're actually solving the same problem.

**Prompt:** Which of these algorithms are "the same" as one another?

#### Algorithm 1

```
MOVE_FORWARD()
TURN_RIGHT()
MOVE_FORWARD()
TURN_RIGHT()
MOVE_FORWARD()
TURN_RIGHT()
MOVE_FORWARD()
TURN_RIGHT()
```

#### Algorithm 2

```
REPEAT 2 TIMES
{
MOVE_FORWARD()
MOVE_FORWARD()
TURN_RIGHT()
MOVE_FORWARD()
TURN_RIGHT()
}
```

#### Algorithm 3

```
moves <- ["F", "R", "F", "R", "F", "R", "F", "R"]
FOR EACH move IN moves
{
IF (move = "F")
{
MOVE_FORWARD()
}
ELSE
{
TURN_RIGHT()
}
}
```

#### Algorithm 4

```
count <- 0
REPEAT WHILE count < 4
{
MOVE_FORWARD()
TURN_RIGHT()
count <- count + 1
}
```

#### Algorithm 5

```
REPEAT 2 TIMES
{
REPEAT 2 TIMES
{
MOVE_FORWARD()
}
REPEAT 3 TIMES
{
TURN_LEFT()
}
MOVE_FORWARD()
REPEAT 3 TIMES
{
TURN_LEFT()
}
}
```

#### Algorithm 6

```
count <- 0
REPEAT WHILE count < 2
{
MOVE_FORWARD()
MOVE_FORWARD()
turnCount <- 0
REPEAT WHILE turnCount < 3
{
TURN_LEFT()
turnCount <- turnCount + 1
}
MOVE_FORWARD()
TURN_RIGHT()
count <- count + 1
}
```

**Circulate:** Ask students to review the algorithms with a partner and group them into categories. Move around the room making sure students are not getting stuck. If they finish early push them to see if they can think about the problem in a different way.

**Prompt:** Discuss with another group. which of these algorithms are "the same" as one another?

**Discuss:** Have students discuss the prompt with another before asking them to share with the room. Lead a discussion on their experiences using tips from the discussion goal below.

###### Discussion Goal

This discussion should focus on what made the algorithms the same. While they are designed to fall into two categories, ideally a number of points should come out of this discussion.

- Algorithms 1, 3, and 4 draw a square while 2, 5, and 6 draw a rectangle.
- Some of these algorithms turn the robot right by turning left three times. It's debateable whether we can really call these algorithms "the same"
- Some of these algorithms create lists or variables to store information. Depending on the context we may not be able to call these algorithms "the same"

## Wrap up (10 Minutes)

### Journal

Students add the following vocabulary words and definitions to their journals:

- Problem: a general description of a task that can (or cannot) be solved with an algorithm
- Algorithm: a finite set of instructions that accomplish a task
*Note:*There are usually many algorithms to solve the same problem, and many ways to write or express one algorithm including natural language, psuedocode, diagrams, and are implemented using programming code. All algorithms can be created by combining steps in three different ways: sequencing, selection, and iteration.

- Sequencing: Putting steps in an order
- Selection: Deciding which steps to do next
- Iteration: Doing some steps over and over

**Prompt:** How did today's activities change the way you think about algorithms and problems?

**Discuss:** Have students think about their answers on their own, then share with a partner, and then finally discuss responses with the entire room.

###### Discussion Goal

Use this discussion to reinforce vocabulary introduced in this lesson and check in on whether students have begun the transition towards thinking on a higher level about algorithms and problems.

### Remarks

- Computer scientists don't just think about "code", they think about problems and the algorithms that solve them. In this unit we're going to explore what makes two problems, or two algorithms, similar or different from one another, and the way computer scientists talk about them. Not only will you be a better programmer, but you'll get to work on some really interesting problems along the way.

## Assessment: Check for Understanding

### For Students

Open a word doc or google doc and copy/paste the following question.

### Question

In your own words explain the difference between a problem and an algorithm.

## Standards Alignment

**CSP2021:**AAP-2.A.1, AAP-2.A.2, AAP-2.A.3, AAP-2.A.4**CSP2021:**AAP-2.B.1**CSP2021:**AAP-2.G.1**CSP2021:**AAP-2.J.1**CSP2021:**AAP-2.L.1, AAP-2.L.2, AAP-2.L.5

## Next Tutorial

In the next tutorial, we will discuss Code.Org Unit 6 Lesson 2, which describes Learn about efficiency in algorithms.