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 by Arnab Chakraborty
Page 124 of 377
Program to encode a string in normal form to its run-length form in Python
Run-length encoding is a simple compression technique that replaces consecutive identical characters with a count followed by the character. For example, "AAABBC" becomes "3A2B1C". So, if the input is like s = "BBBBAAADDCBB", then the output will be "4B3A2D1C2B". Algorithm To solve this, we will follow these steps − Initialize an empty result string Set tmp to the first character and count to 1 Iterate through the string starting from index 1 If current character differs from tmp, append ...
Read MoreProgram to decode a run-length form of string into normal form in Python
Suppose we have a string s. The s is a run-length encoded string, we have to find the decoded version of it. As we know, run-length encoding is a fast and simple method of encoding strings. The idea is as follows − The repeated successive elements (characters) as a single count and character. For example, if the string is like "BBBBAAADDCBB" would be encoded as "4B3A2D1C2B". So, if the input is like s = "4B3A2D1C2B", then the output will be "BBBBAAADDCBB" Algorithm To solve this, we will follow these steps ? ...
Read MoreProgram to find how many distinct rotation groups are there for a list of words in Python
Suppose we have a rotation group for a string that holds all of its unique rotations. If the input is like "567" then this can be rotated to "675" and "756" and they are all in the same rotation group. Now if we have a list of strings words, we have to group each word by their rotation group, and find the total number of groups. So, if the input is like words = ["xyz", "ab", "ba", "c", "yzx"], then the output will be 3, as there are three rotation groups − ["xyz", "yzx"], ["ab", "ba"], ["c"]. Algorithm ...
Read MoreProgram to check whether every rotation of a number is prime or not in Python
In this problem, we need to check if all possible rotations of a given number are prime numbers. A rotation means moving digits from the front to the back or vice versa. So, if the input is like n = 13, then the output will be True, as 13 is prime and its rotation 31 is also prime. Algorithm To solve this, we will follow these steps − Convert the number to string format For each possible rotation of the number: Check if the current rotation is prime If any rotation is not prime, ...
Read MoreLatin Square in Python
A Latin square is a square matrix where each row and column contains every number from 1 to n exactly once. Let's examine the pattern in different sized Latin squares. Understanding the Pattern Here are examples of Latin squares of different sizes ? 1 2 2 1 1 2 3 3 1 2 2 3 1 1 2 3 4 4 1 2 3 3 4 1 2 2 3 4 1 The key pattern is: the last number of the previous row becomes the first element of the next row. This ...
Read MoreLargest product of contiguous digits in Python
Finding the largest product of contiguous digits is a common programming problem. Given a number and k, we need to find k consecutive digits that produce the maximum product. Problem Statement Given two numbers num and k, find the largest product of k contiguous digits in num. The number is guaranteed to have at least k digits. For example, if num = 52689762 and k = 4, the output should be 3024 because the largest product of 4 consecutive digits is 8×9×7×6 = 3024. Algorithm Steps To solve this problem, we follow these steps: ...
Read MoreLargest Gap in Python
In this problem, we need to find the largest difference between two consecutive numbers in a sorted list. This is commonly known as finding the "largest gap" in a dataset. For example, if we have the list [5, 2, 3, 9, 10, 11], after sorting it becomes [2, 3, 5, 9, 10, 11]. The consecutive differences are: 1, 2, 4, 1, 1. The largest gap is 4 (between 5 and 9). Algorithm Steps To solve this problem, we follow these steps: Sort the input list in ascending order Calculate the difference between each pair of ...
Read MoreKnights' Attack in Python
The knight attack problem checks if any two knights on a chessboard can attack each other. A knight moves in an L-shape: two squares in one direction and one square perpendicular, or vice versa. Problem Understanding Given a binary matrix where 0 represents an empty cell and 1 represents a knight, we need to determine if any two knights are attacking each other. For the input matrix: 00000 01000 00010 The output is True because the knight at position (1, 1) can attack the knight at position (2, 3). Knight ...
Read MoreK and -K in Python
In Python, finding the largest number k where both k and -k exist in a list is a common programming problem. This means we need to find pairs of numbers that are additive inverses of each other. So, if the input is like [-5, 2, 9, -6, 5, -9], then the output will be 9 because both 9 and -9 exist in the list. Algorithm Approach To solve this problem, we follow these steps: Create L1: a list of non-negative elements (≥ 0) from nums Create L2: a list of non-positive elements (≤ 0) from ...
Read MoreRemove One to Make Average k in Python
Suppose we have a list of numbers called nums and an integer k, we have to check whether we can remove exactly one element from the list to make the average equal to exactly k. Now we have to keep in mind that, there are some constraints − 2 ≤ n ≤ 1, 000 where n is number of elements of nums list nums[i], k ≤ 1, 000, 000 So, if the input is like [5, 3, 2, 4, 6, 10], k = 4, then the output will ...
Read More