Why Algorithm?
What is an algorithm? An algorithm is a finite set of instructions carried out in a specific order to perform a particular task. Per simplilearn, it is not the entire program or code; it is simple logic to a problem represented as an informal description in the form of a flowchart or pseudocode. It’s used to find the best possible way to solve a problem and typically the most efficient way.
Being able to understand and implement algorithm in your code will not only increase your skills but also further your growth in your everyday work… but where do we begin? There are multiple sites that can help you start, leetcode, ProjectEuler, hackerrank, etc.; each one having problems that become increasingly harder as you progress. No matter your skill level, I would recommend attempting an “easy” problem first to set a base on where you stand.
This is the first challenge from ProjectEuler.
Below I am using Python for the challenges.
Let’s break this down:
We need to find the sum of all the multiples of 3 or 5 below 1000
- example: [3, 5, 6, 9](multiples of 3 and 5) == sum of 23 (add the numbers together to get 23)
- Find all the natural numbers below 1000
- Check each number to see if it is a multiple of 3 or 5
- Accumulate the number if it is a multiple
With my time coding, I have found breaking challenges down helps alleviate some of the intimidation that we may feel initially. It also helps to understand what the goal is and the steps to get there.
So how do we start? We know that we need the numbers below 1000, so that tells me that we need a range(). The range() function returns a sequence of numbers to which you set. It can take up to 3 arguments: the first being where to start, default is 0; second, where we need to stop without including it; third, how we want to step (if we put 2, it will increment by 2s otherwise default with 1).
range(1000)To check each number, we will need to use a loop, in this case a “for” loop. By looping through our range, we can set a set a condition to check if the number is a multiple of 3 or 5 and print those numbers.
for n in range(1000):
if n % 3 == 0 or n % 5 == 0:
print(n)In the loop above, n will be the number that is divisible by 3 or 5. The “%” is used as a remainder operator. We are using this to divide the number and making sure the remainder is equal to 0. Printing (n) will show all the numbers that met that condition.
Now to accumulate those numbers, we need to set a variable equal to 0 and increase that variable by each corresponding number. We will need to refactor our code.
total = 0
for n in range(1000):
if n % 3 == 0 or n % 5 == 0:
total += n
print(total)Now when the loop runs, each number that meets the condition will be added to the total. Printing (total) outputs 233,168 which is the sum of all the numbers in that sequence, thus completing your first challenge. Let’s take a look at a harder challenge from leetcode with an acceptance rate of 49.5%.
Following the same process as before, let’s break this down:
Get Tim Hoang’s stories in your inbox
Join Medium for free to get updates from this writer.
We need to retrieve the most money without hitting adjacent houses.
- (Think of the List as the street, each index as a house, and number as the value)
- Start with the first house
- Check if the List is 1 house long, if only 1 house, return that amount
- If 2, return the highest of the two
- Check the amount if we include the current house and 2 houses down
- Compare the amount to the skipped house
- Return the highest accumulated possibility
We show two examples above, both give us a variable nums. Nums in both is equal to a List of numbers. Just like before, since we need to check each index, it is safe to assume we will be using a “for” loop with a range() but since we are working with a List, we want the first value so we will give it a start of 1 and end with the length of the List using our len() helper method.
# idx will be our index that we check
for idx in range(1, len(nums)):Since we need to get an accumulated value, we will need to set a variable. The variable in this situation will be set to a [List] that has one index with the value of 0. We will then append the first index of nums to our variable List so we can compare the two.
def rob(nums):
riches = [0]
riches.append(nums[0])If nums only had one index, we would return that since there are constraints in our challenge.
Going back to our loop, with the variable that we just set, [riches], since it has the first index of [nums] appended to it, we can write some logic to add the next index of [nums], 2, to our first index of [riches], 0. This will be our setup to skip houses and check if the accumulated value is greater than the prior. If it is, we can append that value to our [riches].
def rob(nums):
riches = [0]
riches.append(nums[0])
for idx in range(1, len(nums)):
with_current = nums[idx] + riches[idx - 1]
not_with_current = riches[idx]
riches.append(max(with_current, not_with_current)with_current = nums[idx] + riches[idx — 1]
- We are setting a variable that is equal to the sum of the current house in [nums] and two houses prior.
not_with_current = riches[idx]
- This variable will be equal to the current index of [riches]: the highest accumulated value in [nums].
riches.append(max(with_current, not_with_current)
- Using the max() helper method, we are comparing then appending the higher to our [riches].
Now that we have our method written out, we need to make sure we return what we want to see: the highest accumulated value of [nums]. [riches] has been appending all of the higher values, so grabbing the last will suffice the solution.
def rob(nums):
riches = [0]
riches.append(nums[0])
for idx in range(1, len(nums)):
with_current = nums[idx] + riches[idx - 1]
not_with_current = riches[idx]
riches.append(max(with_current, not_with_current))
return riches[-1]With a [List], to grab the last index, we can use -1.
Now if we run our method with both the examples above, it should return the same output given.
These are just two of many algorithm challenges but you can see how much harder it can become. Understanding algorithm helps one think out of the box and find the best possible way to find a solution. The greatest thing about this, is that there are multiple ways to complete these challenges; different algorithms for the same challenge, some better than others.









