How to Tell the Computer to Make PB&J Sandwiches

Sarah Dye - Jan 15 - - Dev Community

Now that you can start thinking like a computer, it is time to start applying these principles yourself. The next lesson of Skillcrush 102 is one of the first challenges in the course. In this lesson, students are given the challenge to teach a computer how to make a peanut butter and jelly sandwich.

Students need to use the 4 principles of computational theory to create an algorithm that would tell the computer how to make a peanut butter and jelly sandwich. Today's post is going to walk you through the entire challenge. I'll describe how each of the principles of computational thinking will help you write an algorithm a computer might be able to understand. At the end of the post, you will have an algorithm that lists the steps and list of ingredients needed to make a peanut butter and jelly sandwich.

Today's Challenge

The first challenge in Skillcrush 102 is to teach a computer how to make a peanut butter and jelly sandwich. You will use the 4 principles of computational thinking to help you write an algorithm a computer might use to make a peanut butter and jelly sandwich. We won't be writing any code in this challenge so a computer won't be able to make a peanut butter and jelly sandwich.

The algorithm you create will be the foundational structure a developer might use and turn into code in the future. Before you can start writing your algorithm, you need to figure out everything your algorithm needs. This is where the 4 principles of computational thinking will come in handy since they will help you get organized and identify everything you need before you start writing your algorithm.

You can think of this as a chef doing mise en place before they start cooking a recipe. Doing this extra work now will make it easier for you in the future. Remember a computer doesn't know anything.

So if you did ask a computer how to make a peanut butter and jelly sandwich, it wouldn't know what to do since it doesn't understand what you are talking about. While you might know what spreading means or how to hold a knife, these are details a computer doesn't know. So you need to put yourself in the computer's perspective for this challenge to figure out what information you need for your algorithm.

What is a peanut butter and jelly sandwich?

A peanut butter and jelly sandwich is a type of sandwich that uses peanut butter and jelly. You can use any bread and jelly you want to make your sandwich. One slice of bread will have peanut butter spread on one side.

Another slice of bread has jelly spread on one side. You just the slices of bread together so the sides with peanut butter and jelly mix together.

1. Decomposition

Let's start this challenge by breaking the problem into smaller problems. This is where you'll be making lots of lists. First, let's create a list of ingredients and tools you will need.

The items you will need to make a peanut butter and jelly sandwich are peanut butter, jelly, bread, knife, and plate. Once you have your list of items, it is time to tackle the steps. Now you will need to create a list of actions that you need to take to make a peanut butter and jelly sandwich.

There are going to be more actions in this list since you are putting yourself in the computer's perspective. Below is a sample list of actions you would need to take to make a peanut butter and jelly sandwich.

  • open all the containers
  • open the bread bag
  • take out two slices of bread
  • open the peanut butter jar
  • open the jelly jar
  • Dip the knife into the peanut butter
  • spread the peanut butter on the bread
  • Dip the knife into the jelly
  • spread the jelly on the other piece of bread

2. Pattern Recognition

Now that we have our lists and have broken down all the problems into smaller ones, it is time to take a closer look at our lists to see what patterns we can find. The goal here is to see what repeats. Anything that repeats means you can use similar methods later when you write the algorithm.

As you take a look at your lists, you will notice that some of the steps repeat opening jars and spreading things on bread. These are good examples of patterns. It means that if you can figure out how to open the peanut butter jar and how to spread the peanut butter onto a slice of bread, these methods can be used for jelly.

3. Abstraction

Once you have identified the patterns in your list, it is time to tackle abstraction. This means solving the simplest version of the problem you possibly can. In this step, you will spend a lot of your time identifying the details you need vs. the ones you don't.

That is what makes this step very challenging because it means ignoring a lot of details. Skillcrush asks students to think of how a peanut butter and jelly sandwich would look like as a math equation. Chances are the equation that comes to mind would look like the following:

// pb&j = peanut butter + jelly + bread
Enter fullscreen mode Exit fullscreen mode

While it sounds simple and looks like it would do the job, Skillcrush reminds students that just putting bread, peanut butter, and jelly together doesn't mean it will make a sandwich. In this equation, this is the case because you are leaving out lots of details and assumptions. These details are the kind of bread being used, the type of peanut butter, and what jelly you want to use. These are a lot of details, but you don't need them for this step since we are just trying to solve a basic version of the problem.

Checking Assumptions

Abstraction is much more than solving a simple version of the problem. This principle is great for checking assumptions. Although you are trying to put yourself in a computer's perspective and assuming the computer doesn't know anything, this principle allows you to make guesses to solve parts of the problem.

As you look at your peanut butter and jelly sandwich problem, think about what assumptions you can make. Identifying and even writing out these assumptions will help you figure out parts of the smaller problems you identified earlier. Some of the assumptions you could make for the peanut butter and jelly problem are the following items:

  • The computer knows what a knife is
  • The computer knows how to open jars
  • The bread isn't in a bag
  • The bread is sliced

These assumptions don't just let a developer think the computer knows each of these items. It also means you don't need to define these items immediately. That makes the smaller problem a little bit easier to solve.

So the assumption the computer knows a knife means that you don't need to define what a knife is right away and can define it later when it is time to do so. It also means you can work better and put your attention on solving the more important components of the problem.

3. Algorithm Writing

Once you've solved the simplest version of the peanut butter and jelly sandwich problem, you are ready to put together your algorithm. You've done all the hard work so now you are just writing out the instructions for the computer to use. Think of this step as writing a final draft for an essay or paper.

When developers reach this stage, this is where they would write the instructions they would need to turn into code. Skillcrush has students write their algorithm in a script.js file, but you can write your algorithm in a text file, Word document, or Google Doc. Regardless of what you decide to use to write your algorithm, the goal here is to write out all the ingredients and instructions.

As you write your algorithm, always keep yourself in the mindset that the computer doesn't know anything. Having this mindset is going to help you not only look at the algorithm from a computer's perspective but will help you double-check your algorithm to see if you missed anything or where potential errors might pop up. Try writing the peanut butter and jelly algorithm on your own first.

Once you are satisfied with your algorithm, take a look at the algorithm example below. This example is just one version of how a developer might write this algorithm.

/* Algorithm Example

Ingredients
-peanut butter
-jelly
-bread


Instructions
1. Decide what bread you want to use.
2. Open the bread bag.
3. Pick a slice of bread closest to you and put it on the plate.
4. Open the peanut butter jar.
5. Dip the knife in a peanut butter jar.
6. Spread peanut butter on the slice of bread.
7. Put down the knife.
8. Decide what jelly you want to use.
9. Open the jelly jar.
10. Dip the knife in the jelly jar.
11. Spread jelly on the slice of the bread.
12. Put down the knife.
13. Pick one slice of bread and place it on top of the other slice of bread. 

*/
Enter fullscreen mode Exit fullscreen mode

Conclusion

Congratulations! You finished your first challenge in Skillcrush 102. You now have an algorithm that shows your computer how to make a peanut butter and jelly sandwich.

It has also given you more practice using the 4 principles of computational thinking so you get more comfortable thinking like a computer. If you like, share your version of the peanut butter and jelly sandwich algorithm in the comments below. Do you need more practice with the 4 principles of computational thinking?

Don't worry. You'll be getting more practice using these principles in the next lesson of Skillcrush 102. In the next lesson, you will start figuring out the logic for some of your Skillcrush projects.

You won't just be using the 4 principles of computational thinking to work on the logic for these projects. Skillcrush shows students how flowcharts can help them figure out and organize their logic for the algorithms they will write in the future.

This post was originally published on January 23, 2021 on the blog BritishPandaChick Codes. I made minor changes to the original post to work here on DEV.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .