Programming Articles

Page 144 of 2547

Program to remove duplicate characters from a given string in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 8K+ Views

When working with strings in Python, we often need to remove duplicate characters while preserving the order of first occurrence. This is useful in data cleaning, text processing, and algorithm problems. We can solve this using an ordered dictionary to maintain the insertion order of characters. The dictionary tracks which characters we've seen, and we can join the keys to get our result string. So, if the input is like s = "bbabcaaccdbaabababc", then the output will be "bacd". Algorithm Create an ordered dictionary to store characters in insertion order For each character c in ...

Read More

Program to rotate a string of size n, n times to left in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 754 Views

Suppose we have a string s of size n. We need to find all rotated strings by rotating them 1 place, 2 places, up to n places to the left. So, if the input is like s = "hello", then the output will be ['elloh', 'llohe', 'lohel', 'ohell', 'hello'] Approach To solve this, we will follow these steps − Create an empty result list Get the length of the string For each rotation from 1 to n places: Move the first character to the end Add the rotated string to the result ...

Read More

Python program to check credit card number is valid or not

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 4K+ Views

Credit card numbers must follow specific formatting rules to be considered valid. We need to check if a credit card number meets all the required criteria including format, length, and digit patterns. Credit Card Validation Rules A valid credit card number must satisfy these conditions ? Start with 4, 5, or 6 Be exactly 16 digits long Contain only numeric digits May have digits grouped in four sets separated by hyphens Must not use spaces, underscores, or other separators Must not have 4 or more consecutive identical digits Implementation Here's a complete solution ...

Read More

Python program to find factorial of a large number

Akshitha Mote
Akshitha Mote
Updated on 26-Mar-2026 914 Views

Before going to the solution let's understand about factorial. The factorial is a product of all the integers smaller than or equal to n. The mathematical representation is n! = n × (n-1) × (n-2) × ... × 1. For example, 4! = 4 × 3 × 2 × 1 = 24. In this article, let's explore different ways to find the factorial of a large number in Python. Various Methods Following are multiple ways to find the factorial of a large number in Python − Using 'math' Module ...

Read More

Python program to find product of rational numbers using reduce function

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 545 Views

The reduce() function in Python applies a function cumulatively to items in a sequence, reducing them to a single value. When working with rational numbers (fractions), we can use reduce() to find their product efficiently. For example, if we have fractions = [(5, 3), (2, 8), (6, 9), (5, 12), (7, 2)], the product will be 5/3 * 2/8 * 6/9 * 5/12 * 7/2 = (5*2*6*5*7)/(3*8*9*12*2) = 2100/5184 = 175/432 (in reduced form). Syntax from functools import reduce reduce(function, iterable) Algorithm Steps Create a list of Fraction objects ...

Read More

Program to find elements from list which have occurred at least k times in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 374 Views

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 More

Python program to validate email address

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 15K+ Views

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 More

Program to find number of ways we can get a number which is sum of nth power of unique numbers in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 373 Views

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 More

Python program to sort string in custom order

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 978 Views

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 More

Python program to sort table based on given attribute index

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 468 Views

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 More
Showing 1431–1440 of 25,469 articles
« Prev 1 142 143 144 145 146 2547 Next »
Advertisements