The art of thinking like a programmer: Algorithmic Mindset

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Programmers make computers do things that humans would never even try. And this makes it very hard to think like a programmer. Let me give you an example. Imagine that I give you all the nine digits from 1 to 9. And I ask you a very simple question. How many 9 digit numbers can you make using digits from 1 to 9 given that you can use a digit only once? How would a human solve it? They would pick any digit from 1 to 9 as their first digit. So, they have 9 options to pick the first digit. For the second digit, they would have only 8 options left because they can’t reuse the number they picked for the first digit. In the same way, they will have 7 options for third digit, 6 for fourth and so on. Now, if this person knows a little bit of Math, they can easily see that total such 9 digit numbers would be multiplication of 9 into 8 into 7 until 1 which is also called 9 factorial. As I learnt from the comments on my previous video, Programmers seem to hate Math. So, how would a programmer solve this problem? It might sound counterintuitive but the programmers would actually start making all the 9 digit numbers from scratch. Let me explain what I mean by this. Imagine that you have a bucket that contains all 2 digit numbers you can make using digits 1 and 2. This bucket would contain only 2 numbers, 12 and 21. At the next step, we want to add 3 to the mix and calculate all the 3 digit numbers you can make using 1, 2 and 3. For every number in the bucket, we can add 3 at the front, middle or the end of the number. So, for every number in the bucket, we can generate 3 new numbers. So, the total numbers using digits 1 to 3 is equal to total numbers using digits 1 to 2 multiplied by 3. In general, total numbers using digits 1 to n is equal to total numbers using digits 1 to n-1 multiplied by n. Now, all a programmer would do is start at n equal to 1 and return the total numbers after n is equal to 9. If you look closely at the code, we are doing nothing but calculating factorial of 9. Now, making all the 9 digit numbers from scratch is something our lazy brain doesn’t allow us to do. I don't know about you, but when I first started learning programming, going against my basic lazy human instincts was the biggest challenge that I had to overcome. If you have read the book “Thinking fast and slow”, you would know that our brain contains 2 different kinds of brains within it. First is the “Fast” brain which is the result of thousands of years of evolution. This part of our brain is lazy and jumps to conclusions really fast. The Fast brain served our ancestors really well because when they heard footsteps approaching them while foraging through bushes, they instinctively knew that sprinting is the better choice than carefully analyzing the situation. But, as we moved from a hunter gatherer to a cushy urban lifestyle, more and more problems require us to engage our “Slow” brain rather than the “Fast” brain. Let’s understand this with the help of a problem. You are given a bat and a ball which together cost one dollar and 10 cents. The bat costs one dollar more than the ball. How much does the ball cost? You can pause the video and write your answer in the comments. If you answered 10 cents, then your fast brain is not allowing your slow brain to function properly and your answer is wrong. If you forced your slow brain to engage, you would easily reach the right answer which is 5 cents. In the same way, when you are programming, you can reach the right solution easily if you learn to engage your slow brain. That’s because programming requires “first principles” thinking. For those who don’t know, “first principles thinking” is a problem solving technique that requires breaking a complex problem down to its most basic, fundamental elements. A very good example of first principles thinking is “the total 9 digit numbers” example we already discussed. In order to solve the problem for 9 digit numbers, we broke the problem down to a simple case where we already had a solution for total 2 digit numbers and we tried to solve for 3 digit numbers. This idea of breaking a complex problem down into smaller digestible chunks is quite central to thinking like a programmer. Once you start programming, you will be amazed at how many different flavors this idea shows up in. For example, let’s say that you are given a list of words. And you need to tell whether this list contains a palindrome. Iterating over the list of words and actually checking whether a word is a palindrome are two different things. So, they should be written as two different functions. By doing this, testing your code becomes easier because you can test your main logic that lives in isPalindrome function separately. This is also called Modular programming. Another place where breaking a complex problem into smaller ones shows up is Dynamic Programming or DP. I don’t want to cover DP in too much detail here but in short, what we did to solve the “total 9 digit numbers problem” is also called Dynamic Programming. In my opinion, this ability to break a large complex problem into smaller solvable ones is what distinguishes senior engineers from junior engineers. I was recently working on a project where we had to design the architecture of a complex system that is closely dependent on a couple of upstream services and 3 downstream services. While going through this process, I learnt that the senior engineers understand the bigger picture and break the problem into subproblems whereas junior engineers work on one of these small subproblems. Another key to thinking like a programmer can be summed up by this quote from sir Isaac Newton. “If I have seen further, it’s by standing on the shoulders of the giants”. As a programmer, you are constantly building upon the work of others. For example, let’s say that you need to generate a random number for one of your projects. What will you do to achieve this? Well you will import some library that has some function in there that can generate a random number for you. Now, this is a very simple example but there are some other more interesting cases of this. For example, in front end development, you have many libraries that can provide pre-made UI components to you. So, if you want to build this basic grid of images, you don’t have to write it from scratch. In programming, you don’t need to know everything. You just need to know where to find it when you need it. If you are trying to solve something that's new for you, just be aware that there is a good chance that someone has already done it and you don’t need to reinvent the wheel. Nothing can sum up how to think like a programmer better than this Bruce Lee statement. To understand why that is, let me take you to England. Peppered moth is a species of moth in England and it comes in two colors: White with black spots and Black with white spots. For the longest period of time, there were more white moths than black ones. That’s because the environment these moths lived in had mostly light colored trees. And the predators could spot black moths very easily on these light trees whereas white moths were very hard to distinguish. But in the early 1800s, the white moths started to disappear whereas the number black moths began to rise. Nobody was able to see why this was happening until one fine day, someone pointed out that the trees in the area were also turning black. Everyone got scared for their life because it seemed like some new virus was attacking the area and it was only a matter of time until it got to humans. And that’s when some crazy guy tried to scrub the trees clean to make them light again and to his surprise, it worked. You see, in the early 1800s, the industrial revolution was at its peak. A lot of new factories had opened up in the area. All these factories were causing a lot of pollution and releasing a lot of soot and ash. And this is what caused the trees to turn black. This in turn made white moths very easy to pick out whereas black moths could flourish now. And natural selection did the rest. Programming world has also seen many of these revolutions. Discovery of computers, then internet, Web2, AI, Blockchain and Web3. Everytime this happens, natural selection picks new winning programmers. White moth could not change its color. But we as programmers are not as unlucky as white moths because we can always adapt to the new reality by learning new technologies. When Bruce Lee says, “become water my friend”, all he means is that one should be open minded and constantly evolving as required by the environment around him. And this mindset is essential to become a programmer. Now that you know how to think like a programmer, you might actually want to become one yourself. Here are 3 roadmaps to learn coding and actually become a programmer. My name is Sahil and I will see you in the next one.
Info
Channel: Power Couple
Views: 18,478
Rating: undefined out of 5
Keywords: how to learn coding, how to become a software engineer, how to learn programming, how to learn coding for beginners, how to learn programming for beginners, how to learn coding fast, how to become a software developer, how to get software engineer job, how to get software developer job, how to learn to code, coding, coding advice for beginners, coding advice, learn to code, programming
Id: QHdJscEpxaU
Channel Id: undefined
Length: 7min 58sec (478 seconds)
Published: Fri May 26 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.