Introduction to Algorithms

The challenge of writing an algorithm is one of my favourite things to do. It has the upside of helping you write better performing and more effective code through practice and repetition (the Japanese have a word for this kata), while making you a better developer. You can think of algorithms as a set of instructions on how to solve to a problem.

For example,  An Old Fashioned Whiskey Cocktail is one of my favourite things to have at a bar. There are numerous variations on how to make it but essentially it comes down to this:

  • Get a small bar glass
  • Add 1 teaspoon of sugar and 2 dash of Angostura bitters
  • Add some water
  • Mix thoroughly
  • Add 50 ml whiskey
  • Add Ice to glass
  • Stir till heart’s content
  • Finish with an orange peel

Tada! Photo by Adam Jaime on Unsplash
Tada! Photo by Adam Jaime on Unsplash

So, really, the same instruction should give you the same drink, and by drawing a parallel, the same instruction should give you the same result, irrespective of the programming language used.

To cite a few more examples of other implementation of algorithms, Google uses a routing algorithm to work out the quickest route between 2 locations and a compression algorithm is used to send stream across the internet quickly.

To the code 👉

Now that we have a good understanding of algorithms I have picked out two famous ones to help us get our feet wet.
For the purpose of this tutorial, I will be using Javascript.

Famous FizzBuzz

FizzBuzz is a popular assessment test given to developers at tech interviews to determine if the developer can actually write code. It has the upside of testing basic reasoning and problem solving without taking up a lot of time to solve.

Write a program that prints the numbers from 1 to 100. But for multiples of three (3, 9, 12… etc) print “Fizz” instead of the number and for the multiples of five (5, 10, 15… etc) print “Buzz”. For numbers which are multiples of both three and five (15, 30, 45… etc) print “FizzBuzz”.

There are multiple ways to implement this algorithm. In researching this, I came across at least 5 variations on how to solve it. Here are two:

Rough draft Instruction
  • Step 1: Iterate through the numbers 1…n ( n in our case is 100 ).
    I have used a do while loop , for the solution because I believe it’s most appropriate, but a for loop is just as applicable;
  • Step 2: If i is a modulus of 3 and 5 output “FizzBuzz”
  • Step 3: If i is a modulus of 3 output “Fizz”
  • Step 4: If i is a modulus of 5 output “Buzz”

Slightly BETTER Performance Instruction

This solution below is an improvement on the one above because it follows a well know software design principal that state Don't Repeat Yourself (DRY). With that in mind, we can optimize the previous solution by

  • Assigning the Boolean values of (i%3 === 0) and (i%5 === 0) to variables i.e fizz and buzz.
  • As a result we don’t use statements like i%3 === 0 && i%5 === 0 repeatedly. Readable code is clean code!

CAESAR Cipher (ROT13)

Julius Caesar proposed this famous cipher that is also known as the ROT13 encryption. It applies the idea of substitution such that each letter in a phrase or text is replaced by moving the letter n places down. For example, the word YOU 3 places down becomes BRX.

Something to note, in the case that shifting n places down takes us past the end of the alphabet, we simply return to the start of the alphabet.

Instruction
  • Create a list of all the alphabets such that A...Z is represented as number 0...25.
  • Iterate through each letter of phrase to be encoded
    • Retrieve the number i for the current letter in the list of alphabets i.e A = 0, B = 1 , C = 2 , D = 3
    • Add the n places down to the current number i i.e
      • If A = 0 and we shift it 3 places down,
        then new number is 0 + 3 = 3;
        Look up the Letter assigned to new number 3
        i.e 3 = D.
      • If Z = 25 and we shift it 3 places down ,
        then new position is 25 + 3 = 28; 28 is larger than the 26 numbers (0...25) representing letter available.
        To solve the issue, get the modulus for (28%26)which is 2
        Look up the Letter assigned to new number 2
        i.e 3 = B.
    • Repeat till all the letters have been encoded
Real life Implementation

Thank you for reading!

Resources:

https://www.codewars.com/dashboard – CodeWars is an excellent place to get started with lots of exercise with different complexities ranging from easy to difficult accompanied with varied solutions offered by developers the world over.

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting – Great resources for simple to intermidiate algorithms to solve.

https://www.hackerrank.com/interview/interview-preparation-kit – Excellent resources for algorithm related interviews or a bit of competitive programming.

Related Posts