Underrated step for logic building in programming.

Image
Logic building is a crucial and complex skill in programming. In essence, it is ability to come-up with solution of coding problem and write precise instructions ( or code) that a computer can execute autonomously. This skill requires aligning your thought process with computer and its capabilities. And running through code some-what abstractly to know and predict the behavior of code before it is executed. To be able to do this, one essential step that many beginner programmers overlook is performing dry runs. Understanding Dry Runs The concept of a dry run in programming is straightforward: can you mentally execute your code and predict its output without actually running it on a computer? While this seems simple, it is a challenging task. Typically, we are taught to write code, run it, and observe the output. This cycle is essential because code needs to run to be validated. However, if you rely solely on running your code to understand its behavior, you may struggle with building

Simple recursion function's explanation with Russian doll analogy.

 


The Russian Doll Analogy with Code Example

Imagine a set of magical Russian nesting dolls, each with a unique ability to reveal a smaller doll inside. You start with the largest doll, which opens to reveal a slightly smaller one, continuing until you reach the tiniest doll that cannot open anymore.

The Largest Doll (Initial Function Call):

The journey begins here. You start by examining the largest doll.

Opening Dolls (Recursive Calls):

Each time you open a doll to find another inside, it's like making a recursive function call.

The Smallest Doll (Base Case):

When you reach the smallest doll that doesn't open anymore, you've hit the base case of the recursion, the stopping point.

Code Example with Explanation

Let’s implement this analogy in code by writing a simple recursive function that opens the dolls until it reaches the smallest one.

def open_doll(doll_size):
    # Base Case: If the doll size is 1, it's the smallest doll that doesn't open
    if doll_size == 1:
        print("Reached the smallest doll.")
        return
    else:
        # Recursive Case: Open the current doll to reveal a smaller one
        print(f"Opening doll of size {doll_size} to find a smaller doll inside.")
        open_doll(doll_size - 1)
        print(f"Finished with doll of size {doll_size}")

# Start with the largest doll, say of size 5
open_doll(5)

Explanation of the Code

  1. Initial Function Call:
    • The function open_doll(5) starts with the largest doll, size 5.
  2. Base Case:
    • The function checks if doll_size is 1. If it is, it prints "Reached the smallest doll." and stops (returns).
    • This prevents further recursive calls, ensuring the process stops at the smallest doll.
  3. Recursive Case:
    • If the doll_size is greater than 1, it prints a message indicating it's opening the current doll to reveal a smaller one.
    • The function then calls itself with doll_size - 1, representing the next smaller doll.
    • After the recursive call completes, it prints a message indicating it has finished with the current doll, simulating closing or completing the examination of the current doll.
  4. Execution Flow:
    • The function starts with open_doll(5), prints the message, and calls open_doll(4).
    • This process repeats, with the size decreasing each time, until it reaches open_doll(1).
    • At open_doll(1), it hits the base case, prints "Reached the smallest doll.", and stops the recursion.
    • The function then unwinds, finishing with each doll size from 2 back up to 5.

Output

The output will illustrate the process:

Opening doll of size 5 to find a smaller doll inside.
Opening doll of size 4 to find a smaller doll inside.
Opening doll of size 3 to find a smaller doll inside.
Opening doll of size 2 to find a smaller doll inside.
Reached the smallest doll.
Finished with doll of size 2
Finished with doll of size 3
Finished with doll of size 4
Finished with doll of size 5

Each step of opening a doll and reaching the smallest doll mirrors the function calls and the base case in the code.

Happy coding !

Comments

Popular posts from this blog

Building JavaScript Array Methods from Scratch in 2024 - Easy tutorial for beginners # 1

Python Code # 12

A better way to learn programming.