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 23 of 377
Program to find elements from list which have occurred at least k times in Python
Sometimes we need to find elements from a list that appear a certain number of times or more. Python provides several approaches to solve this problem using frequency counting techniques. So, if the input is like nums = [2, 5, 6, 2, 6, 1, 3, 6, 3, 8, 2, 5, 9, 3, 5, 1] k = 3, then the output will be [2, 5, 6, 3] Using Counter from collections The Counter class provides an efficient way to count element frequencies ? from collections import Counter def solve(nums, k): c ...
Read MorePython program to validate email address
Email validation is a common requirement in web applications. We need to check whether an email address follows the correct format and character rules. Python's re module provides powerful regular expression capabilities for this task. Email Validation Rules A valid email must follow these conditions ? Format must be username@company.domain Username can contain letters, numbers, dashes, and underscores Company name can contain letters and numbers only Domain can contain lowercase letters only Domain extension length must be 1-3 characters ...
Read MoreProgram to find number of ways we can get a number which is sum of nth power of unique numbers in Python
Suppose we have a number x and another number n. We have to find the number of ways we can get x as the sum of nth power of some unique numbers. So, if the input is like x = 100, n = 2, then the output will be 3 because the possible solutions are: 6² + 8² = 36 + 64 = 100 10² = 100 1² + 3² + 4² + 5² + 7² = 1 + 9 + 16 + 25 + 49 = 100 Algorithm To solve this problem, we ...
Read MorePython program to sort string in custom order
Sorting strings in a custom order requires defining a special key function. In this problem, we need to sort an alphanumeric string with specific priority rules for different character types. Sorting Rules The custom sorting follows this priority order: All sorted lowercase letters come first All sorted uppercase letters come second All sorted odd digits come third All sorted even digits come last For example, if the input is "HeLlo1234", the output will be "eloHL1324". Custom Key Function We create a key function that assigns priority codes to different character types ? ...
Read MorePython program to sort table based on given attribute index
Suppose we have a 2D list containing information about athletes. This information includes rank, age, and height. Each row contains data for different athletes. We need to sort the table based on a given attribute index k. So, if the input is like ? Rank Age Height 1 25 190 2 35 180 3 33 185 4 26 175 5 35 180 And k = 1 (sort by age), then the output will be ? ...
Read MoreProgram to perform prefix compression from two strings in Python
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 MoreProgram to perform string compression in Python
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 MoreProgram to swap string characters pairwise in Python
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 MorePython program to define class for complex number objects
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 MorePython program to print number triangle
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