What's the deal with algorithms?
Tags: algorithms, beginners, career
Published: 2022-06-03 16:22:24

Growing up, math was always my favorite subject. I loved being able to look at a set of numbers and instructions and then be able to transform that into a solution using a prescribed method. It made me feel secure and confident. For that reason, I knew I wanted to go into some sort of engineering field. After a pursuing a career in social science for a while, I finally decided that I wanted to pursue an engineering career, but I was unsure what type of engineering that would be. I planned to return to undergrad and earn another bachelors in civil, mechanical, electrical, and software engineering. I was learning more toward the first three because I knew that I would be able to use more of my mathematical skills. However, as I looked deeper into which types of engineering I enjoyed and wanted to pursue as a career, software engineering quickly rose to the top. I thought, “this is great! A career that I enjoy with financial stability. But where is the math?!” While software engineering definitely requires technical thinking, in the beginning, it didn’t seem like a field that would make me use mathematical reasoning. That is until I discovered algorithms and their role in the field!

## What are algorithms?

Simply put, algorithms are a set of clear, discrete, step-by-step instructions that accomplish a task and/or solve a problem (Rao 2019). A day-to-day example of an algorithm is a recipe with a set of instructions. For example, to bake a cake, you must:

     Preheat the oven
     Gather dry ingredients
     Mix dry ingredients
     Gather wet ingredients
     Mix wet ingredients
     Mix both wet and dry ingredients
     Put batter into a pan
     Put into the oven and bake for 40 minutes
     Meanwhile, mix ingredients for frosting
     Take cake out and let it cool
     Frost the cake
     Enjoy!

While it might not seem like it, we just created an algorithm! We created a step-by-step solution to accomplish the task of baking a cake. Every day, we use algorithms, whether we are following google maps, using a recipe to bake a cake, or reading a sign on the road. Clearly, algorithms are important. But I understand what you may be thinking: okay, sure, algorithms are important, but this seems a little elementary. Why is it important to understand algorithms if they’re something we take for granted? There are many good answers to this question, but I will answer it for the field of computer science.

## Why do we need algorithms?

Let’s say you have to give a set of instructions to someone on how to make a sandwich. How would you do it? You would probably tell them get the following ingredients: bread, jelly, and peanut butter. You tell them to put the jelly on one slice of bread, peanut butter on the other, stack the bread, and then eat it. Pretty simple, right? But what if that person didn’t now what peanut butter was? Or didn’t know how to spread the peanut butter? Or didn’t know that you should stack the bread in a way where the peanut butter and jelly was on the inside of the bread? You’d become quickly exasperated in such a situation. But this is the life of programmers everywhere. Daily, we deal with a machine that requires such explicit instructions. So, we’ve learned to appreciate the algorithm.

This might sound counterintuitive, but computers are not smart. If you told a computer to make you a sandwich, it would throw up its hands and tell you “ERROR!” Why? A computer doesn’t know what peanut butter is, or how to spread it. In other words, a computer needs very specific instructions. This is where algorithms come to the rescue. If we try to talk to the computer like a human, it won’t listen. If we talk to it in a language it understands, it will do what it tells us to do. Algorithms thus help us break down problems into a way that a computer can understand and solve. And because computers can do great things, the power to control with algorithms computers gives us great power.

## How do we write algorithms?

Let’s switch up the problem. Let’s say we want to create an algorithm that determines if a word is a palindrome or not. A palindrome is a word that is spelled the same way forwards and backward (e.g., racecar, mom, dad, etc.). First, let’s think about what you would tell a human to do if they needed to determine if a word was a palindrome. What would you do? You would tell a human to look at a word and then maybe write it out backwards. If it’s the same as the original word, then it’s a palindrome! Yay!

But could you do that to a computer? If you opened up a coding simulator and typed that in, your computer would throw an error. We have to speak a language that it can understand. So, how would you start? If you’re getting overwhelmed thinking about how to begin, you’re not alone. We are not computers, so it makes sense that we don’t think like them. So how do we bridge this gap? We do it with pseudocode!

Pseudocoding is writing out an algorithm in non-technical, easy-to-understand language (Grant 2020). Instead of worrying about how to accomplish the task and the language we need to write the algorithm in, let’s first worry about how to accomplish the task. What do we need to do to get a computer to determine if something is a palindrome? Let’s start by giving it an input that’s a string (or word/collection of words).

```
//function takes a word

```
Okay, then what? Well, the computer has to reverse the word. Let’s not worry about how to do that just yet. Let’s just get the computer to reverse the word.

```
   //function takes a word
   //function reverses the word
```

Now, let’s have the computer compare these two words. To compare them, we can have the computer give these two words a name.

```
   //function takes a word (called word)
   //function reverses the word (called revWord)
   //function compares word and revWord
```

Okay, so if the words are the same, the computer should tell us. Let’s have it tell us “We have a palindrome!” If not, it can tell us “This is not a palindrome.”

```
   //function takes a word (called word)
   //function reverses the word (called revWord)
   //function compares word and revWord
   //if they are same, say “We have a palindrome!”
   //if not, say “This is not a palindrome.”
```

And we have written our pseudocode! The next step is to turn this into language that the computer can understand. Let’s begin in javascript:

```
function palindrome (word) {

}
```

Okay, that’s our first step. Now, we have the next step to reverse the word. This is not as simple, but let’s think about this. Javascript cannot reverse a string, but it can reverse an array. What if we turn the word into an array of letters? Let’s try it.

```
function palindrome (word) {
     let split = word.split();
}
```

Okay, now we have an array in the variable split. Let’s reverse that array.

```
function palindrome (word) {
	Let split = word.split();
	Let revArray = split.reverse();
}
```

Now, if we had the input of “dog”, we would have [“g”, “o”, “d”]. While we clearly see that this is reverse, it’s still an array and therefore cannot be read as a word. Let’s put it back together.

```
function palindrome (word) {
	Let split = word.split();
	Let revArray = split.reverse();
	Let revWord = revArray.join(“”);
}
```

Now it’s back to normal. Let’s take a look back at our pseudo code. We need to determine if the word is a palindrome or not. Let’s do that using an “if else” method.

```
function palindrome (word) {
	Let split = word.split();
	Let revArray = split.reverse();
	Let revWord = revArray.join(“”);
	
	If (word === revWord) {
		Return “This is a palindrome!”;
	} else {
		Return “This is not a palindrome!”;
	}
   }
```

And we have our function! We have told a computer to determine whether or not something is a palindrome.

## What’s the point?

At this point, you may be thinking, “Okay, great, I know how to get a computer to do something, but I can pretty easily determine whether or not something is a palindrome.” Really? In 2002, Peter Norvig of Google sought out to create the longest palindrome. The previous record was over 56,000 words. Let’s look at a brief chunk of it: 

    “A man, a plan, a cameo, Zena, Bird, Mocha, Prowel, a rave, 
     Uganda, Wait, a lobola, Argo, Goto, Koser, Ihab, Udall, a 
     revocation, Ebarta, Muscat, eyes, Rehm, a cession, Udella, E- 
     boat, OAS, a mirage, IPBM, a caress, Etam, FCA, a mica, Ojai, 
     Lebowa, Yaeger, a barge, Rab, Alsatian, a mod, Adv, a rps, 
     Ileane, Valeta, Grenada, Hetty, Fayme, REME, HCM, Tsan, 
     Owena, Tamar…”

Would you want to look at this manually? Of course not. But we could give this to our algorithm that we just made and easily see if it’s a palindrome or not. That’s the power of understanding algorithms!

## Conclusion

I love that I’ll be able to write algorithms. I think they force me to use my mathematical skills that I’ve always wanted to use in my career. They also help you accomplish tasks that are too difficult to do without computers. And not to mention that the more able you are to create algorithms to solve difficult problems, the more likely you are to land your dream programming job. For this reason, it’s important to practice as much as you can. Try looking at an algorithm once a day. Just look at one. If you can solve it, great. But as you expose yourself to this, you’ll begin to think like a computer and more easily be able to communicate with one! Good luck, and happy programming!

## Sources

- https://junilearning.com/blog/guide/what-are-algorithms/
- https://www.makeuseof.com/tag/what-is-pseudocode/#:~:text=Pseudocode%20helps%20you%20plan%20out,what%20your%20code%20is%20doing.
- https://www.dailydot.com/debug/worlds-longest-palindrome-sentence/