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
Programming Articles
Page 534 of 2547
Guess Nearest Square Root in Python
Finding the square root of a number without using built-in functions is a classic programming problem. We need to find the largest integer r such that r * r ≤ n, effectively rounding down to the nearest integer. So, if the input is 1025, then the output will be 32 because 32² = 1024 ≤ 1025, but 33² = 1089 > 1025. Algorithm Steps We'll use binary search to efficiently find the square root ? If n ≤ 1, return n (base case) Set start = 1, end = n While start < end: ...
Read MoreGroup Integers in Python
In Python, you can group integers into equal-sized groups where all numbers in each group are identical. This involves checking if a list can be split into groups where each group has the same size (≥2) and contains identical elements. The solution uses the Greatest Common Divisor (GCD) to find if all frequencies share a common factor greater than 1. Algorithm Steps The approach follows these steps ? Count frequency of each distinct element Find GCD of all frequencies If GCD > 1, ...
Read MoreGreatest common divisors in Python
The greatest common divisor (GCD) is the largest positive number that divides all numbers in a given list. Python's math.gcd() function makes it easy to find the GCD of multiple numbers by applying it iteratively. Problem Statement Given a list of positive numbers, find the largest positive number that divides each number in the list. For example, if the input is [14, 28, 70, 56], the output should be 14 since 14 divides all these numbers. Algorithm To solve this problem, we follow these steps ? Initialize result with the first element of ...
Read MoreGenerate a list of Primes less than n in Python
Finding all prime numbers less than or equal to a given number n is a classic problem in computer science. A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. We'll use the Sieve of Eratosthenes algorithm, which is one of the most efficient methods for finding all primes up to a given limit. So, if the input is like 12, then the output will be [2, 3, 5, 7, 11]. Algorithm Steps To solve this, we will follow these steps − Create a boolean ...
Read MoreNumber of Programmer Worked on given Time in Python
Suppose we have a list of intervals and another input time. In each interval, the structure is [start, end], representing the times when a programmer worked. We have to find the number of programmers that were working at a given time. So, if the input is like intervals = [[2, 6], [4, 10], [5, 9], [11, 14]], time = 5, then the output will be 3 as at time 5, there are three programmers working: [2, 6], [4, 10], [5, 9]. Algorithm To solve this, we will follow these steps − ...
Read MoreJustify Frame Width in Python
When we have a list of words, we can frame them in a rectangular region with stars and proper spacing. This creates a visual frame around the text, line by line. Problem Understanding Given a list of words, we need to create a rectangular frame where each word is on its own line, padded with spaces to maintain uniform width, and surrounded by asterisks. For example, if the input is ['hello', 'world', 'python', 'programming', 'nice'], the output will be ? *************** * hello * * world * ...
Read MoreFlip and Invert Matrix in Python
A binary matrix contains only 0s and 1s. To flip and invert a matrix, we need to reverse each row and then flip each bit (0 becomes 1, 1 becomes 0). This operation is commonly used in image processing and computer graphics. Given this input matrix ? 1 1 0 0 1 0 0 0 1 The output will be ? 1 0 0 1 0 1 0 1 1 Algorithm Steps To solve this problem, follow these ...
Read MorePython finding programming question in a String
When working with strings in Python, we sometimes need to check if we can form a specific subsequence with equal spacing between characters. This problem asks us to find if we can extract the string "programmingquestion" from a given string where each character is picked at regular intervals. The key insight is that we need to find positions of 'p' and 'r' characters, then check if we can form the target string using consistent spacing starting from these positions. Problem Understanding Given a lowercase string, we need to check if we can pick characters at regular intervals ...
Read MoreRemove Substrings in One Iteration in python
When working with string manipulation, we often need to remove multiple substrings efficiently. Python's replace() method allows us to chain operations to remove different patterns in sequence. Problem Statement Given a string, we need to remove all occurrences of "y" and "xz" substrings. For example, if the input string is "xyxxzyyxxzx", the output should be "xxxx" after removing these patterns. Solution Approach To solve this problem, we will follow these steps − First, remove all "xz" substrings from the original string Then, remove all "y" characters from the resulting string Return the final cleaned ...
Read MorePattern After Double, Reverse, and Swap in Python
The Pattern After Double, Reverse, and Swap problem generates a sequence by applying three operations cyclically: double, reverse, and swap characters. Starting with "xxy", we apply these operations to find the nth pattern in the sequence. Understanding the Pattern The sequence follows this pattern: xxy xxyxxy (doubled) yxxyxx (reversed) xyyxyy (swapped x↔y) xyyxyyxyyxyy (doubled) ... Algorithm Steps To generate the nth pattern, we follow these rules cyclically: Step 0 (mod 3): Double the string (concatenate with itself) Step 1 (mod 3): Reverse the string Step 2 (mod 3): Swap all 'x' ...
Read More