the four cornerstones of computational thinking are:
1.Decomposition
2.Pattern recognition
3.Abstraction
4.Testing and debugging algorithms
Decomposition means breaking down a problem into smaller tasks. Breaking a complex problem into smaller problems or subtasks makes solving the larger problem more manageable. Since the problem that we are working on is putting away the dishes, let's take a look at the following image of a fancy plate, plain plate, and plain bowl to get an idea of what kind of dishes we are putting away:
// Tasks:
// Stack plain plates
// Stack fancy plates
// Stack plain bowls
pattern recognition, or thinking about how we've solved these subtasks previously and finding any patterns that might help us to solve this particular problem. The following image shows that our cupboard space is limited and we only have two shelves to work with:
// Pattern recognition:
// Plain plates go on the bottom shelf
// Fancy plates go on the top shelf
// Bowls go on the top of plain plates
abstraction. In this step, we will make sure that we are only focusing on relevant information and that we are disregarding details that won't help us solve this problem. Let's take a look at the image of our drying rack again:
// Abstraction:
// Ignore knife
// Ignore towel
algorithm is essentially a sequence of steps or rules that we can use to solve our problem. In our case, it will be the sequence of events that will occur in order for us to complete the task of putting away the dishes so that they resemble the following image:
//Sequence:
//1. Event 1: Put away plates:
//2. Event 2: Put away bowls:
Here we've begun to pseudocode our sequence. Notice how we have created two separate events for putting away plates and bowls so that we know to stack the bowls second. This is called an event trigger, because the completion of the first event will trigger the second event.
This is a good start, but we aren't done! We don't want the plain plates to get mixed up with the fancy plates. We need to add steps under the first event that will help us remember to sort the two. When we pseudocode this out, our new sequence might look something like the following:
//Sequence:
//1. Event 1: Put away plates:
// Conditional: If (a plate is fancy) {
// stack it on the top shelf
// } else {
// stack it on the bottom shelf
// }
//2. Event 2: Put away bowls:
Here, we've added what is known as a conditional statement. We will learn more about conditional statements in a future lesson. For now, all we need to understand is that conditional statements allow us to make decisions based on conditions in our code. Here, if a plate meets the condition of being fancy, we will make the decision to stack it on the top shelf. Otherwise, we can assume that a plate is plain and should be stacked on the bottom shelf.
This conditional statement would probably work at first, but it is important that we try to think of edge cases, such as exceptions or unusual scenarios, that might occur so that they are covered by our conditional statement. What if we stumble across a plate that is chipped or broken? Let's adjust our conditional statement so that it includes additional conditions:
//Sequence:
//1. Event 1: Put away plates:
// Conditional: If (a plate is fancy) {
// stack it on the top shelf
// } else {
// stack it on the bottom shelf
// }
// Debug: A fancy plate has a crack in it - what do we do?
// Conditional: If (a plate is fancy) {
// put it on the top shelf
// } else if (a plate is plain) {
// put it on the bottom shelf
// } else if (a plate is cracked) {
// put it in the trash
// }
Changing our original algorithm after finding a mistake or a potential mistake in our pseudocode is called debugging. We will cover debugging in greater depth in a future lesson. For now, just know that this is an important part of a developer's job!
Now that we have pseudocoded the sequence for the first event, let's move on to the second event. In this case, we only have one type of bowl, so we don't have to worry about making decisions based on certain conditions—we know that they are all going to be stacked in the same place! Instead of a conditional statement, we can use something called a for loop, or an action that we will repeat over and over again until a condition is met. When we pseudocode our loop, it might look something like this:
//Sequence:
//1. Event 1: Put away plates:
// Conditional: If (a plate is fancy) {
// stack it on the top shelf
// } else {
// stack it on the bottom shelf
// }
// Debug: A fancy plate has a crack in it - what do we do?
// Conditional: If (a plate is fancy) {
// put it on the top shelf
// } else if (a plate is plain) {
// put it on the bottom shelf
// } else if (a plate is cracked) {
// put it in the trash
// }
//2. Event 2: Put away bowls:
// Loop: for (each bowl on the dish rack) {
// stack it on the bottom shelf on top of the plain plates
// }
//Algorithm: Put away clean dishes
//Tasks:
//Stack plain plates
//Stack fancy plates
//Stack plain bowls
//Pattern recognition:
//Plain plates go on the bottom shelf
//Fancy plates go on the top shelf
//Bowls go on the top of plain plates
//Abstraction:
//Ignore knife
//Ignore towel
//Sequence:
//1. Event 1: Put away plates:
// Conditional: If (a plate is fancy) {
// stack it on the top shelf
// } else {
// stack it on the bottom shelf
// }
// Debug: A fancy plate has a crack in it - what do we do?
// Conditional: If (a plate is fancy) {
// put it on the top shelf
// } else if (a plate is plain) {
// put it on the bottom shelf
// } else if (a plate is cracked) {
// put it in the trash
// }
//2. Event 2: Put away bowls:
// Loop: for (each bowl on the dish rack) {
// stack it
// }
Top comments (0)