Server Side Programming Articles

Page 530 of 2109

Index into an Infinite String in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 535 Views

Sometimes we need to extract a substring from an infinite repetition of a given string. Python's modulo operator makes this straightforward by cycling through the original string's indices. Given a string s and two integers i and j where i < j, we need to find the substring from an infinite string formed by repeating s forever, using indices [i, j). Example Problem If s = "programmer", i = 4, and j = 8, the infinite string looks like: "programmerprogrammerprogrammer..." 0123456789012345678901234567890... We need the substring from index 4 to 7 (excluding 8), ...

Read More

Count Frequency of Highest Frequent Elements in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 2K+ Views

Finding the frequency of the most common element in a list is a common programming task. Python provides several approaches, from basic nested loops to efficient built-in methods using collections.Counter. So, if the input is like [1, 5, 8, 5, 6, 3, 2, 45, 7, 5, 8, 7, 1, 4, 6, 8, 9, 10], then the output will be 3 as the number 5 occurs three times and is the most frequent element. Using Nested Loops (Basic Approach) This approach compares each element with all other elements to count occurrences ? def count_max_frequency(nums): ...

Read More

Insert 5 to Make Number Largest in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 591 Views

Given a number n, we need to find the maximum number we can create by inserting the digit '5' at any position in the number. This problem involves string manipulation and finding the optimal position to maximize the resulting number. So, if the input is like n = 826, then the output will be 8526 (inserting '5' after the first digit creates the largest possible number). Algorithm Steps To solve this problem, we follow these steps ? Convert the number n to a string for easy manipulation ...

Read More

Guess Nearest Square Root in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 655 Views

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 More

Group Integers in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 992 Views

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 More

Greatest common divisors in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 333 Views

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 More

Generate a list of Primes less than n in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 5K+ Views

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 More

Number of Programmer Worked on given Time in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 137 Views

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 More

Justify Frame Width in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 149 Views

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 More

Flip and Invert Matrix in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 876 Views

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 More
Showing 5291–5300 of 21,090 articles
« Prev 1 528 529 530 531 532 2109 Next »
Advertisements