At some point, you’ve probably heard someone talking about “algorithms” and wondered what they were. First of all, algorithms are just computer programs that solve specific problems by following a set of rules under certain constraints. They’re how computers work!
This blog post lays out ten algorithms listed by Something With Numbers that everyone should know about. You’ll learn the basics of each algorithm at the beginning, then implement several in your own programming language (in this case, Python) in the second half of the post.
10: The Problem: Shuffling Ludo Cards
This algorithm is used to shuffle a deck of cards so that no two of the same card are ever grouped together. It is named after its inventor, Edward Phillips who invented it in 1998.
9: Solving Sudoku Problems
Solving Sudoku puzzles is often referred to as “solving” because it’s all about moving numbers into logical groups. Solving Sudoku can be done by hand or through computers, but if you know the algorithms below then the computer version will be very quick for you. It takes less than two minutes to find an optimal solution on most computers.
8: The Problem: Finding a Path from Point A to Point B
Somewhat incredibly, this algorithm is how your GPS system finds your way home. Imagine trying to find your way from Boston, Massachusetts to New York City, NY. It is hard and involves a lot of trial and error. If you use an algorithm like this one it takes just a few minutes and the results are correct every time!
7: The Problem: Creating a Subset of Items from a List
This algorithm is commonly used in database systems as well as text editors such as Notepad where you want to remove certain items from a list before printing the list out. This algorithm will help remove items very quickly with little chance of error.
6: The Problem: Finding the Lowest Number in a List
This is a simple algorithm that you can use to find the lowest number in a list. It’s really useful if you want to know which points on a map are closest to each other. For example, if you work at Amazon and you want to find the three closest stores and their distance from your location. You can do this with an algorithm like this one.
5: The Problem: Adding a Number to a List
This is an algorithm that’s similar to one above, but this one will allow you to add a number from 1-100 to a list of numbers 1-100. It’s useful if you have a set of numbers that needs incrementing. For example, if you’re building a website and want the “order total” for each order, this algorithm will help you do it quickly and efficiently.
4: The Problem: Finding the Least Digit in a Number
This is an algorithm that takes a number as input and returns the least number of digits needed to accurately represent the original number. For example, if you have a number it could be “1255” this algorithm will help you find the smallest number of digits that accurately represent the original. For example, if you wanted to know how many digits needed to represent “1255” this would be important for you.
3: The Problem: Finding the Least Spaced Number in Number
This algorithm will return the least number of digits needed to accurately represent a given number. For example, suppose we had the number “245”. We’d like to know how many digits we’d need to use as our string representation. In this case, we only need the digits 2 and 5 to represent “1255”. In this case, we’d return 9 as the least number of digits needed to represent 245.
2: The Problem: Finding the Highest Spaced Number in a Number
This algorithm will return the most number of digits needed to accurately represent a given number. For example, suppose we had a number “1255”. We’d like to know how many digits we’d need to use as our string representation. In this case, we only need 4 digits for “245” because of the way that it is spelled out. In this case, we’d return 3 as the most number of digits needed for “1255”.
1: The Problem: Finding the Sum of a Number
This algorithm returns the sum of a list of numbers by only adding the one extra number to the sum. It is very fast because it avoids lots of unnecessary repetitions and does not need to track which numbers have been added. This makes it great for adding numbers in-place.
In the example above, we have a list of numbers 0-100. We want to return the sum of this list without having to rewrite it or having to do lots of extra work. This algorithm will allow us to take every number from 0-100 and only add 1 extra number to it. In this case, we added a 1 instead of a 0 at the end. This works well because we just need a sum instead of a string representation that looks nice and neat.
Implementing the algorithms:
Now that you have learned all 10 algorithms you will implement them in Python on your machine.