How to think like a programmer: Why languages don’t matter but algorithms do

How to think like a programmer: Why languages don’t matter but algorithms do

The foundation of thinking like a programmer lies in understanding that computers are dumb!!! In this era where technology seems to surpass human capabilities, it is very common to get tricked into thinking that computers are inherently smart.

Computers, from smartphones to supercomputers, operate on binary code — a series of 1s and 0s. When a computer beats a human at chess or suggests a product you might like, it’s not because the computer is inherently intelligent. It’s because human programmers have fed it a set of instructions (algorithms) to follow.

A computer is a beautiful machine which is capable of performing tasks at at a speed far beyond our human capabilities. However, it needs to be told how to do so and that is exactly what programming is.

A question that I get from a lot of beginners in programming is, “What is the best programming language?” or “What language is the most used?” or maybe “What language should I learn?”. My answer to all of those questions is usually, “What problem are you trying to solve?” or “What do you aim to achieve by learning a specific language?”. The real question should be about the problem they are trying to solve. Programming isn’t about the language itself; rather, it’s about how you use the language to articulate solutions to problems. Just as you would explain to a toddler how to hold a spoon in your native language, you explain to a computer how to perform a specific task in a language that helps it understand. This is achieved by converting our instructions into Binary (0s and 1s) through a programming language.

Phonebook Example

Let’s lay down the foundations of thinking like a programmer. I have a task for you. I will give you a phonebook, and you need to find the name, Tom, in it. Forget about the language or the tool you would use; assume you know all languages and just think about how you would explain the solution to this problem in English in the most detailed way possible, assuming that the person you are trying to explain to is not very knowledgeable.

The point I am trying to make here is that all languages are similar in some sense; they have similar concepts, paradigms, etc. The essence of programming is not the language but the algorithms that power your program.

Okay, let’s come back to the problem of finding Tom in the phonebook. Let me help you do that.

One method you might consider when faced with the task of finding a name, like Tom, in a phonebook, is to start from the first page and proceed, page by page, reading every name until you find Tom. This approach is straightforward because it guarantees that you will find the name you’re looking for. However, consider a scenario where the phonebook is 2000 pages long. If it takes you about 5 seconds to scan each page for Tom’s name, you would end up spending 10,000 seconds, which amounts to roughly 2.75 hours, just searching for one name because you have to go through every page in the phonebook.

Let’s break this down into steps:

  1. Check the first page.

  2. Read all names and see if any match Tom.

  3. If not, proceed to the next page and repeat the process.

  4. Continue this until you’ve either found Tom or finished the phonebook.

Here is a visualization of how this approach works:

Here is a python code snippet on how this works:

def linear_search(arr, target):
    # Loop over each item in the array
    for i in range(len(arr)):
    # If the current item matches the target value
    if arr[i] == target:
    # Return the current index as the target is found
    return i
    # If the loop completes without finding the target, return -1
    return -1

Now, let’s think like a programmer. Is this really the best approach? How would you, as a human, solve this problem more efficiently? You might start by opening the phonebook randomly somewhere in the middle. Then, you would determine if Tom is likely to be found after or before the page you opened, based on alphabetical order. If we convert this strategy into an algorithm, it would look something like this:

  • Start in the middle of the phonebook.

  • Check if Tom is on that page.

  • If not, determine if Tom would be in the earlier or later part of the book, based on alphabetical order.

  • Repeat the process with the relevant half of the phonebook until you’ve either checked the entire book or found Tom.

Both approaches are simple to understand, right? However, the first approach requires you to check all pages, while the second one halves the number of pages to check each time you don’t find the name you’re looking for. So, if the phonebook has 2000 pages and it takes you 5 seconds to read each page:

  1. After 5 seconds, you’ll have 1000 pages left to check.

  2. After 10 seconds, you’re down to 500 pages.

  3. After 15 seconds, only 250 pages remain, and so on, until at around 55 seconds, you find the page you’re looking for.

Here is a visualization of how our new approach works:

Here is a python code snippet on how this works:

def binary_search(arr, target):
    # Initialize the starting points
    left, right = 0, len(arr) - 1

    # Loop until the left index is less than or equal to the right index
    while left <= right:
    # Calculate the middle index
    mid = (left + right) // 2

    # Check if the middle element is the target
    if arr[mid] == target:
    return mid # Target found, return its index

    # If the target is smaller than the middle element, narrow the search to the left half
    elif arr[mid] > target:
    right = mid - 1

    # If the target is larger than the middle element, narrow the search to the right half
    else:
    left = mid + 1

    # If we exit the loop, the target was not found
    return -1

This method is significantly faster than the first approach. A difference between a minute and 3 hours is substantial. The first approach is known as linear search in programming, and the second one is referred to as binary search.

Finding a lost dog in the neighbourhood Example

Let us say that your dog got lost in the neighbourhood, how would you go about finding it? Now again, you can always look in random places unless you stumble upon your dog and its a happy ending for everyone, but being programmers, and after learning what we learn in the last example, we can break down this problem and think of a better solution. As humans, what we usually do is that you look in the places around you thoroughly before progressing to the next spot and thoroughly searching that place. Congratulations, that is exactly what thinking like a programmer means. This algorithm that you just used is called Breadth First search.

Breadth-First Search (BFS) is like creating a treasure map of your neighborhood. Instead of digging for treasure, you’re searching for your lost dog. The trick is to start where you are and look all around that spot before going further. Think of it as throwing a stone in a pond and watching the ripples spread out wider and wider.

Let us suppose your town looks something like this, your dog was chasing butterflies and somehow ended up at the park and you are at home.

Step 1: Start at Home

First, you begin at your house. This is the starting point, like the center of your ripple.

Step 2: Check the Nearest Places

Next, you move to places close to your home.

Step 3: Move to the Next Circle and Keep Expanding Your Search

After checking the closest spots, you move a little further out. You keep expanding your search area, like the ripples in the pond. Each time, you move to places that are one step further than the last places you checked. This way, you make sure you’re covering the entire neighborhood without missing any spots.

Congratulations!!!! you have found your dog.

This method is great because it makes sure you check everything close to you first. It’s like when you’re looking for a toy in your room; you start by looking right around where you last saw it, then under the bed, then in the closet, and so on. By searching in widening circles, you’re thorough, and you don’t waste time checking the same places twice.

Here is a python snippet to help you understand how we are looking to find the dog using BFS:

def bfs(graph, start, target):
    """Performs BFS on the graph from the start node to find the target."""
    visited = set() # To keep track of visited locations.
    queue = deque([start]) # Queue for BFS, starting from 'Home'.

    while queue:
    current_location = queue.popleft()
    print(current_location)
    if current_location == target:
    return f"Hooray! Found the dog at {current_location}!"

    visited.add(current_location)

    # Add neighbouring locations to the queue if not visited.
    for neighbor in graph[current_location]:
    if neighbor not in visited:
    queue.append(neighbor)

    return "Oh no! Couldn't find the dog."

Conclusion

Now that we’ve explored some examples in detail, you should have a clearer understanding of the essence of programming. It’s about comprehending an algorithm thoroughly and then using a programming language as a tool to express your ideas to the computer, allowing it to solve problems for you efficiently in the future.

We find this need to solve problems in everyday life and are usually successful at that. Computer programming is the same thing.

Programming can indeed be challenging, but mastering it largely depends on your ability to deconstruct complex problems into smaller, manageable solutions. Understanding and applying algorithms is what truly embodies thinking like a programmer. By selecting the most efficient approach to a problem, you not only save time and resources but also embody the fundamental principles of effective programming.