Program to perform prefix compression from two strings in Python

Arnab Chakraborty
Updated on 26-Mar-2026 15:42:49

406 Views

Suppose we have two strings s and t (both contain lowercase English letters). We need to find a list of three pairs, where each pair is in the form (l, k) where k is a string and l is its length. The three pairs represent: the longest common prefix of both strings, the remaining part of string s, and the remaining part of string t. So, if the input is like s = "science" and t = "school", then the output will be [(2, 'sc'), (5, 'ience'), (4, 'hool')] Algorithm To solve this, we will follow these ... Read More

Program to perform string compression in Python

Arnab Chakraborty
Updated on 26-Mar-2026 15:42:31

21K+ Views

String compression using run-length encoding replaces consecutive repeated characters with the character followed by its count. For example, 'bbbb' becomes 'b4'. Single characters remain unchanged without a count. So, if the input is like s = "abbbaaaaaaccdaaab", then the output will be "ab3a6c2da3b". Algorithm To solve this, we will follow these steps − Initialize an empty result string and counter set to 1 Iterate through the string starting from index 1 If current character matches previous character, increment counter ... Read More

Program to swap string characters pairwise in Python

Arnab Chakraborty
Updated on 26-Mar-2026 15:42:15

15K+ Views

Swapping string characters pairwise means exchanging adjacent characters at positions (0, 1), (2, 3), (4, 5), and so on. This is useful for simple text transformations and encoding operations. Problem Statement Given a string, swap all odd positioned elements with even positioned elements to create a pairwise swapped permutation. For example, if the input is s = "programming", the output will be "rpgoarmmnig". Algorithm To solve this problem, we follow these steps − Convert the string to a list of characters Iterate through the string ... Read More

Python program to define class for complex number objects

Arnab Chakraborty
Updated on 26-Mar-2026 15:41:59

5K+ Views

Complex numbers are mathematical objects with real and imaginary parts, typically written as a + bi. Python allows us to create a custom Complex class to handle complex number operations like addition, subtraction, multiplication, division, and calculating modulus. Operations Overview Our Complex class will support the following operations ? Addition: (a + bi) + (c + di) = (a + c) + (b + d)i Subtraction: (a + bi) - (c + di) = (a - c) + (b - d)i Multiplication: (a + bi) × (c ... Read More

Python program to print number triangle

Arnab Chakraborty
Updated on 26-Mar-2026 15:41:36

1K+ Views

Suppose we have a number n. We have to print a triangle with n rows where each line i contains the digit i repeated i times. So, if the input is like n = 5, then the output will be ? 1 22 333 4444 55555 Approach To solve this, we will follow these steps ? For i in range 1 to n+1, do Display (integer part of (10^i)/9*i) Go to next line Mathematical Formula The formula (10**i)//9*i works because: ... Read More

Python program to find probability of getting letter 'a' in some letters and k sized combinations

Arnab Chakraborty
Updated on 26-Mar-2026 15:41:19

526 Views

Suppose we have an array with n different English letters and a value k. We can select k different indices with uniform distribution and need to find the probability that at least one of the k indices selected will contain the letter 'a'. For example, if we have letters = ['a', 'c', 'a', 'b', 'l', 'a', 'b', 'z'] and k = 2, we need to find all possible combinations of 2 letters and count how many contain at least one 'a'. Algorithm To solve this problem, we follow these steps − Initialize ... Read More

Python program to check whether we can pile up cubes or not

Arnab Chakraborty
Updated on 26-Mar-2026 15:41:01

399 Views

Suppose we have an array nums containing sizes of n different cubes placed horizontally. We need to make a vertical pile of cubes following this rule: If the ith cube is on top of the jth cube, then the side length of the jth cube must be greater than or equal to the side length of the ith cube. When making the vertical pile, we can only take cubes from the left side or right side, but not from the middle. We need to check whether we can pile them up successfully or not. Problem ... Read More

Program to compute gcd of two numbers recursively in Python

Arnab Chakraborty
Updated on 26-Mar-2026 15:40:41

8K+ Views

Finding the Greatest Common Divisor (GCD) of two numbers is a fundamental mathematical operation. The Euclidean algorithm provides an efficient recursive approach by repeatedly applying the principle that GCD(a, b) = GCD(b, a mod b). So, if the input is like a = 25, b = 45, then the output will be 5. Algorithm Steps To solve this recursively, we will follow these steps − Define a function gcd(). This will take a, b if a is same as b, then ... Read More

Python program to find top three mostly occurred letters from company name

Arnab Chakraborty
Updated on 26-Mar-2026 15:40:25

676 Views

Sometimes we need to find the most frequently occurring characters in a company name. This program identifies the top three most common letters and displays them with their frequencies following specific sorting rules. Problem Requirements Given a company name as a string, we need to find the three most common characters by following these rules ? Pick the most frequent three letters Sort them in descending order by frequency If frequencies are equal, sort alphabetically For example, if the input is s = "TUTORIALSPOINT", the output should be [[3, 'T'], [2, 'I'], [2, 'O']]. ... Read More

Program to find area of a polygon in Python

Arnab Chakraborty
Updated on 26-Mar-2026 15:40:03

3K+ Views

Suppose we have a list of ordered points that represents a simple polygon on a 2D plane. We need to find the area of this polygon using the Shoelace formula (also known as the surveyor's formula). So, if the input is like points = [(0, 0), (0, 5), (3, 5), (3, 0)], then the output will be 15. ... Read More

Advertisements