Problem set: Given a fibonacci list of 2 items, add the next 10 numbers in the sequence and return the sum of the even numbers only. I was working on a variation of a fibonacci problem set on Project Euler recently. I spent the best part of two weeks trying to find a solution. I ended up solving about 50% of it at most. Does this hurt my ego to admit this? HELL YES. Does this matter in the greater scheme of learning Python? NO, NO IT DOES NOT.
There were a number of things that I had to wrap my head around just to acquire an understanding of how to approach this problem. The two primary challenges were:
- Finding a way to add the next number in the sequence. (In a fibonacci sequence you get the next number by adding the sum of the two previous numbers.)
- Adding the even numbers.
In the initial few days, I try to figure out these problems using only my thought process. This is a lot tougher than searching for answers across the internet, but it means that my thinking process will gradually change and improve. This needs to happen in order to become an effective computer programmer.
Attempting to solve the whole problem within one function was the biggest error of judgement that I made. I was unaware of the fact that I could make a second function to calculate the sum of the even numbers, and then pass this function into the fibonacci function. It was by going through the fibonacci problem set with my Python mentor that I came to see that a function can be passed inside another function. Once I saw this it made sense for us to create a sum_even function that could be passed inside the fibonacci function.
This makes use of the Don't Repeat Yourself (DRY) method. It saves me time in the long run because if I want reuse this code I just need to call the function named sum_even
In programming, when I look at the reusable code in concepts like functions, classes and libraries I am often reminded of the Isaac Newton quote:
"If I have seen further it is by standing on the shoulder of giants"
I know that as I move forward I will need further exposure to this concept of passing a function inside another function, in order to incorporate it into my muscle memory and my automatic way of thinking. However, I know that my newly acquired awareness that such a concept even exists means that it will now be in the back of my mind somewhere.
Frequently, what seems to catch me out is where to put the return statement. Up until a couple of weeks ago I wasn't clear on the difference of returning inside a loop or outside it. A simple way to see whether a return statement is located inside or outside a certain loop is to look at what it matches up with. For example, in the above sum_even function the return statement matches up with the for loop and is outside of the if loop.
Fibonacci Solution Summary In order to solve the fibonacci problem set, we had to break it into smaller subsets. First I figured out a way to add the next number in the sequence a certain number of times. In Python [-1] is always the index of the last item and it moves backwards from there. So [-1] is the last index, [-2] is the second last index and so on and so forth. For whatever reason, it took me a while to get used to this logic, but like in linguistics there are certain things that you just have to accept in order to move forward. The .append() method allowed me to add an item to the list and what it adds is the sum of index [-1] and index [-2]. The number of times I want this to execute is represented by the number within range and -2 serves to take away the number of items that are already in the list.
Next I had to find a way to add the even numbers. The above sum_even function was what allowed me to do this. The main thing to notice within this function is that an easy way to return an even number in Python is to use the modulus operator %. The modulus operator looks like a divisional operator but it gives you the remainder after division. So if a number is even, when it is divided by an even number like 2 there should be a remainder of 0.
Separately these solutions made sense to me, but I wasn't at a level where I could pull the pieces of the jigsaw together just yet. The key things for me were that I put an honest effort into solving this problem, I could understand the component parts and I had a mentor who could help me put the pieces together.
A Blast from the Past: The Fibonacci sequence came into existence from the medieval Italian mathematician Leonardo Pisano in a discussion about the problem of breeding rabbits. In the present day the fibonacci sequence is used in numerous fields such as music, visual arts, and architectural design. It is even used to explain phenomena in nature and human psychology.
Did you find this blog post useful? I am continuously looking for ways to improve on the content so that my readers have the best experience possible. If you have any insights, questions or recommendations feel free to leave a comment or drop me an e-mail.