Skip to main content

Poridhi: Basic Recursion



Basic recursion related problems:

This blog covers a series of beginner-friendly yet essential programming problems that introduce the fundamentals of function calling, recursion, iteration, and array/string manipulation using Python. It starts by explaining how general and recursive function calls work, which is crucial for understanding how code execution flows in both linear and self-referential contexts. Following that, it dives into classic number-printing problems such as printing from 1 to N, N to 1 in reverse, and from 0 to N-1, all of which build a strong foundation in loops and control structures.

The blog also addresses basic computational tasks like finding the sum or product (factorial) of the first N natural numbers—important exercises that enhance logical reasoning and mathematical thinking. Additionally, it includes common problems involving arrays and strings such as reversing an array and checking if a string is a palindrome, both of which are widely used in real-world applications. These problems are not only great practice for mastering syntax but also vital for building algorithmic thinking in any beginner programmer.

✅ How General Function Call Works

def greet(name): print(f"Hello, {name}!") greet("Alice")

✅ How Recursive Function Call Works

def recursive_hello(n): if n == 0: return print("Hello") recursive_hello(n - 1) recursive_hello(3)

✅ Print Number from 1 to N


def print_1_to_n(n): for i in range(1, n + 1): print(i, end=' ') print_1_to_n(5)

✅ Print Numbers in Reverse Order from N to 1

def print_n_to_1(n): for i in range(n, 0, -1): print(i, end=' ') print_n_to_1(5)

✅ Print Numbers from 0 to N-1 (Upper Bound Exclusive)

def print_0_to_n_minus_1(n): for i in range(n): print(i, end=' ') print_0_to_n_minus_1(5)

✅ Sum of First N Numbers

def sum_n(n): total = 0 for i in range(1, n + 1): total += i return total print(sum_n(5)) # Output: 15

✅ Factorial / Multiplication of First N Numbers

def factorial(n): result = 1 for i in range(1, n + 1): result *= i return result print(factorial(5)) # Output: 120

✅ Reverse an Array

def reverse_array(arr): return arr[::-1] print(reverse_array([1, 2, 3, 4, 5]))

✅ Check Palindrome String

def is_palindrome(s): return s == s[::-1] print(is_palindrome("madam")) # True print(is_palindrome("hello")) # False

Comments

Popular posts from this blog

Poridhi: Stacks & Queues

  Stacks & Queues related problems: This collection of solutions tackles three classic problems often encountered in technical interviews and competitive programming. The Valid Parentheses problem checks whether a string has properly matched and ordered brackets using a stack. The Sliding Window Maximum efficiently finds the maximum value in every window of size k across an array using a deque, a popular sliding window pattern that ensures optimal performance. The Stock Span Problem simulates a real-world stock analysis scenario and calculates the number of consecutive days before today for which the stock price was less than or equal to today's, also utilizing a stack for efficient computation. These problems test understanding of stacks, queues, and sliding window techniques. ✅ Valid Parentheses def isValid ( s: str ) -> bool : stack = [] mapping = { ')' : '(' , '}' : '{' , ']' : '[' } for char in s: ...

Data Recovery: DevOps pre-state learning

  Data Backup Data backup is the practice of copying data from a primary to a secondary location, to protect it in case of equipment failure, cyberattack, natural disaster or other data loss events. This can include documents, media files, configuration files, machine images, operating systems, and registry files. Essentially, any data that we want to preserve can be stored as backup data. Data Restore Data restore is the process of copying backup data from secondary storage and restoring it to its original location or a new location. A restore is performed to return data that has been lost, stolen or damaged to its original condition or to move data to a new location. Pilot Light A pilot light approach minimizes the ongoing cost of disaster recovery by minimizing the active resources, and simplifies recovery at the time of a disaster because the core infrastructure requirements are all in place. Warm Standby The warm standby approach involves ensuring that there is a scaled down, ...

Poridhi: Basic Programming & Math

Basic Programming and math related problems: Extracting digits means breaking a number like 1234 into [1, 2, 3, 4], often using string conversion or modulo/division. Counting digits involves finding how many digits are in a number using length of a string or repeated division. Reversing an integer flips its digits (e.g., 123 becomes 321), with care for signs and 32-bit limits. A palindrome number reads the same forward and backward, often checked by comparing the original and reversed number. Armstrong numbers are those equal to the sum of their digits each raised to the number of digits, like 153 = 1³ + 5³ + 3³. To find the sum of all divisors of a number, loop through integers and check which divide the number without a remainder. Prime numbers are only divisible by 1 and themselves, and checking up to the square root is efficient. GCD, the greatest common divisor of two numbers, can be found using a simple loop or more efficiently with the Euclidean algorithm, which uses repeated mo...