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 126 of 2547
Greatest 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 MoreDomino Covering Board in Python
Suppose we have two numbers n and m representing a board of size n x m. We also have an unlimited number of 1 x 2 dominos. We have to find the maximum number of dominos that can be placed on the board such that they don't overlap and every domino lies completely within the board. Each domino covers exactly 2 squares, so the maximum number of dominos we can place is limited by the total number of squares divided by 2. Understanding the Problem For a board of size n x m: Total squares ...
Read MoreDetect Voter Fraud in Python
Voter fraud detection involves identifying cases where a voter has cast multiple votes in an election. In Python, we can detect this by checking if any voter ID appears more than once in our voting records. Each vote is represented as [candidate_id, voter_id], where the candidate_id is who they voted for and voter_id uniquely identifies the voter. Problem Statement Given a list of votes where each vote contains [candidate_id, voter_id], we need to determine if any voter has voted more than once. For example, if the input is [[5, 1], [5, 0], [5, 4], [5, 3], ...
Read More