Articles on Trending Technologies

Technical articles with clear explanations and examples

Python program to find number of local variables in a function

Pavitra
Pavitra
Updated on 25-Mar-2026 998 Views

In this article, we will learn how to find the number of local variables in a function using Python's built-in code object attributes. Problem statement − We are given a function, and we need to display the number of local variables defined within that function. Understanding Local Variables Local variables are variables that are defined inside a function and can only be accessed within that function's scope. Python stores information about functions in code objects, which include the count of local variables. Using __code__.co_nlocals Every function in Python has a __code__ attribute that contains a ...

Read More

Python Program to Count trailing zeroes in factorial of a number

Pavitra
Pavitra
Updated on 25-Mar-2026 892 Views

In this article, we will learn how to count the number of trailing zeros in the factorial of a given number without actually computing the factorial itself. Problem statement − We are given an integer n, we need to count the number of trailing zeros in n! (factorial of n). Understanding the Concept Trailing zeros are produced by factors of 10, and 10 = 2 × 5. In any factorial, there are always more factors of 2 than factors of 5, so we only need to count the factors of 5. ...

Read More

Python Program to Count set bits in an integer

Pavitra
Pavitra
Updated on 25-Mar-2026 514 Views

In this article, we will learn how to count set bits (1's) in the binary representation of an integer. A set bit refers to a bit position that contains the value 1. Problem statement − We are given an integer n, we need to count the number of 1's in the binary representation of the number. For example, the number 15 in binary is 1111, which has 4 set bits. Method 1: Using Iterative Approach This approach uses bitwise AND operation to check each bit ? def count_set_bits(n): count = ...

Read More

Python Program to Count number of binary strings without consecutive 1'

Pavitra
Pavitra
Updated on 25-Mar-2026 349 Views

In this article, we will learn about counting binary strings of length N that don't contain consecutive 1's. This is a classic dynamic programming problem that follows the Fibonacci pattern. Problem statement − We are given a positive integer N, and we need to count all possible distinct binary strings of length N such that no two consecutive 1's exist in the string. Approach We can solve this using dynamic programming by considering two cases ? a[i] = number of valid strings of length i ending with 0 b[i] = number of valid strings of ...

Read More

Python Program to Count Inversions in an array

Pavitra
Pavitra
Updated on 25-Mar-2026 316 Views

In this article, we will learn about counting inversions in an array. An inversion occurs when a larger element appears before a smaller element in an array. Problem statement − We are given an array, we need to count the total number of inversions and display it. Inversion count represents how far an array is from being sorted. For example, in array [2, 3, 8, 6, 1], the pairs (2, 1), (3, 1), (8, 6), (8, 1), and (6, 1) are inversions. What is an Inversion? An inversion is a pair of indices (i, j) where ...

Read More

Find Circles in an Image using OpenCV in Python

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 25-Mar-2026 8K+ Views

The OpenCV platform provides a cv2 library for Python. This can be used for various shape analyses which is useful in Computer Vision. To identify the shape of a circle using OpenCV we can use the cv2.HoughCircles() function. It finds circles in a grayscale image using the Hough transform. Common Approaches Some of the common methods to find circles in an image using OpenCV are as follows − Circle detection using Hough transform OpenCV ...

Read More

Python Program for Triangular Matchstick Number

Pavitra
Pavitra
Updated on 25-Mar-2026 212 Views

In this article, we will learn how to calculate the total number of matchsticks required to build a triangular pyramid. Given the number of floors X, we need to find the total matchsticks needed to construct the entire pyramid structure. Problem statement − We are given a number X which represents the floor of a matchstick pyramid, we need to display the total number of matchsticks required to form a pyramid of matchsticks with X floors. Understanding the Pattern In a triangular matchstick pyramid: Floor 1 requires 3 matchsticks (forming 1 triangle) Floor 2 requires ...

Read More

Python Program for Subset Sum Problem

Pavitra
Pavitra
Updated on 25-Mar-2026 2K+ Views

In this article, we will learn how to solve the Subset Sum Problem using Python. This is a classic computer science problem where we need to determine if there exists a subset of given numbers that adds up to a target sum. Problem Statement: Given a set of non-negative integers and a target sum, determine if there exists a subset of the given set with a sum equal to the target sum. Naive Recursive Approach The recursive approach explores all possible subsets by either including or excluding each element ? def subset_sum_recursive(numbers, n, target_sum): ...

Read More

Python Program for Stooge Sort

Pavitra
Pavitra
Updated on 25-Mar-2026 288 Views

In this article, we will learn about the Stooge Sort algorithm and implement it in Python. Stooge Sort is a recursive sorting algorithm that divides the array into overlapping parts and sorts them multiple times to ensure correctness. Problem statement − We are given an array, we need to sort it using stooge sort. Algorithm The Stooge Sort algorithm follows these steps ? 1. Check if value at index 0 is greater than value at last index, then swap them. 2. Sort the initial 2/3rd of the array. 3. Sort the last 2/3rd of the ...

Read More

Python Program for Sieve of Eratosthenes

Pavitra
Pavitra
Updated on 25-Mar-2026 2K+ Views

The Sieve of Eratosthenes is an ancient algorithm for finding all prime numbers up to a given limit. It works by iteratively marking the multiples of each prime as composite, leaving only the prime numbers unmarked. Problem statement − We are given a number n, we need to print all primes smaller than or equal to n. Constraint: n is a small number. Algorithm Overview The algorithm follows these steps: Create a boolean array of size n+1, initially all True Mark 0 and 1 as False (not prime) For each number p from 2 to √n, ...

Read More
Showing 1–10 of 61,304 articles
« Prev 1 2 3 4 5 6131 Next »
Advertisements