Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles on Trending Technologies
Technical articles with clear explanations and examples
Python Program to Find the Total Sum of a Nested List Using Recursion
When working with nested lists, we often need to calculate the sum of all elements regardless of their depth. Recursion is an elegant technique where a function calls itself to solve smaller parts of the problem until reaching a base case. Recursion breaks down complex nested structures by processing each element. If an element is a list, the function calls itself recursively. If it's a number, it adds to the total sum. Syntax def recursive_sum(nested_list): total = 0 for element in nested_list: ...
Read MorePython Program to Reverse a String Using Recursion
When it is required to reverse a string using recursion technique, a user-defined method is used along with recursion. 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 String Reversal The recursive approach works by taking the first character and appending it to the reversed substring. The base case is when the string becomes empty. Example Below is a demonstration for the same − def reverse_string(my_string): if len(my_string) == 0: ...
Read MorePython Program to Check Whether a String is a Palindrome or not Using Recursion
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)
Read MorePython Program to Find the Product of two Numbers Using Recursion
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 MorePython Program for Reversal algorithm for array rotation
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 MorePython program to determine whether the given number is a Harshad Number
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 MorePython program to print all Happy numbers between 1 and 100
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 MorePython program to print all Disarium numbers between 1 to 100
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 MorePython program to check if the given number is a Disarium Number
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 MorePython Program to Find All Numbers which are Odd and Palindromes Between a Range of Numbers
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