A palindrome is a string that reads the same forwards and backwards, such as "racecar" or "madam". We can check if a string is a palindrome using recursion by comparing characters from both ends and recursively checking the substring in between. Recursion works by breaking down the problem into smaller subproblems. For palindrome checking, we compare the first and last characters, then recursively check the remaining substring until we reach the base case. Syntax The recursive approach follows this logic ? def check_palindrome(string): if len(string)
When it is required to find the product of two numbers using recursion technique, a simple if condition and recursion is used. The recursion computes output of small bits of the bigger problem, and combines these bits to give the solution to the bigger problem. How Recursion Works for Multiplication Multiplication can be thought of as repeated addition. For example, 5 × 3 = 5 + 5 + 5. We can use recursion to break down multiplication into smaller addition problems ? Example Below is a demonstration for the same ? def compute_product(val_1, ... Read More
The reversal algorithm for array rotation is an efficient technique that rotates an array by reversing specific portions. This algorithm achieves left rotation by performing three strategic reversals instead of moving elements one by one. How the Reversal Algorithm Works To rotate an array left by d positions: Reverse the first d elements Reverse the remaining elements Reverse the entire array Example Here's the complete implementation of the reversal algorithm ? def reverse_list(arr, begin, end): while (begin < end): ... Read More
A Harshad Number (also known as a Niven number) is a positive integer that is divisible by the sum of its digits. For example, 12 is a Harshad number because 12 ÷ (1 + 2) = 12 ÷ 3 = 4, which is a whole number. Basic Approach To check if a number is a Harshad number, we need to ? Calculate the sum of all digits in the number Check if the original number is divisible by this sum Example my_num = 134 remaining = sum_val = 0 print("Checking if", ... Read More
A happy number is a number that eventually reaches 1 when replaced by the sum of the square of its digits repeatedly. For example, 7 is happy because 7² = 49, then 4² + 9² = 97, then 9² + 7² = 130, and so on until we reach 1. Numbers that don't reach 1 will eventually cycle. The most common cycle includes 4, which is why we check for it as a stopping condition. Algorithm To find happy numbers between 1 and 100 ? For each number, repeatedly calculate the sum of squares of ... Read More
A Disarium number is a number where the sum of its digits raised to the power of their respective positions equals the original number itself. For example, 89 is a Disarium number because 81 + 92 = 8 + 81 = 89. To find all Disarium numbers between 1 and 100, we need to calculate the length of each number and check if the sum of digits raised to their positional powers equals the original number. Complete Program def length_calculation(my_val): len_val = 0 while(my_val != 0): ... Read More
A Disarium Number is a number where the sum of its digits raised to the power of their respective positions equals the original number itself. For example, 175 is a Disarium number because 11 + 72 + 53 = 1 + 49 + 125 = 175. Understanding Disarium Numbers To check if a number is a Disarium number, we need to ? Extract each digit from the number Calculate the total number of digits Raise each digit to the power of its position (1-based indexing) Sum all the results and compare with the original number ... Read More
When it is required to find all numbers that are odd and palindromes within a given range, list comprehension and the modulo operator (%) can be used to achieve this efficiently. A palindrome is a number that reads the same forwards and backwards, such as 121 or 1331. For a number to be both odd and a palindrome, it must satisfy two conditions: divisibility check and string comparison. Syntax result = [x for x in range(start, end+1) if x%2!=0 and str(x)==str(x)[::-1]] Example Here's how to find all odd palindromes in a given range ... Read More
A binomial tree is a data structure used in computer science and financial modeling. It consists of nodes where each tree of order k has 2^k nodes. In Python, we can implement this using object-oriented programming with a class that manages tree creation and combination operations. Binomial Tree Class Implementation Here's how to create a binomial tree class with methods to add children and combine trees ? class BinomialTree: def __init__(self, key): self.key = key self.children ... Read More
When it is required to split the array and add the first part to the end, we can use list slicing or implement a rotation algorithm. This operation is commonly known as left rotation of an array. A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on). Method 1: Using List Slicing The simplest approach is to use Python's list slicing to split and rearrange the array ? def split_and_rotate(arr, k): """Split array at position k and move ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance