Python Articles

Page 656 of 855

Find substrings that contain all vowels in Python

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

Sometimes we need to find all substrings within a string that contain all vowels (a, e, i, o, u) at least once and contain no consonants. This is useful for text processing and pattern matching tasks. Problem Statement Given a string in lowercase alphabets, we need to find all substrings that: Contain all five vowels at least once Contain no consonants For example, if the input is "helloworldaeiouaieuonicestring", the output will be substrings like 'aeiou', 'aeioua', 'aeiouai', etc. Algorithm We'll use a nested loop approach with these steps: For each ...

Read More

Find subsequences with maximum Bitwise AND and Bitwise OR in Python

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

When working with arrays and bitwise operations, we often need to find subsequences that maximize the sum of bitwise AND and OR operations. The key insight is that the maximum AND value comes from the largest single element, while the maximum OR value comes from combining all elements. Problem Understanding Given an array of n elements, we need to find two subsequences (possibly different) such that the sum of the bitwise AND of the first subsequence and bitwise OR of the second subsequence is maximized. For example, with array A = [4, 6, 7, 2], the maximum ...

Read More

Find sub-arrays from given two arrays such that they have equal sum in Python

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

When working with two arrays, you may need to find sub-arrays that have equal sums. This problem involves finding contiguous elements from each array where the sum of elements in one sub-array equals the sum in another sub-array. Given two arrays P and Q of size N containing numbers 1 to N, we need to find sub-arrays with equal sums and return their indices. If no solution exists, return -1. Example For arrays P = [2, 3, 4, 5, 6] and Q = [9, 3, 2, 6, 5], the solution is: P[0..2] = 2 + 3 ...

Read More

Find Shortest distance from a guard in a Bankin Python

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

Suppose we have a matrix filled with three letters 'O', 'G', and 'W' where 'O' represents open space, 'G' represents guards, and 'W' represents walls in a bank. We need to replace all O's with their shortest distance from the nearest guard, without going through walls. In the output matrix, guards become 0 and walls become -1. Problem Example If the input matrix is: OOOOG OOOWO OWOOO GWWWO OOOOG The output will be: 33210 233-11 1-1432 0-1-1-11 12210 Algorithm Approach We use multi-source BFS (Breadth-First Search) to solve this ...

Read More

Find same contacts in a list of contacts in Python

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

Finding duplicate contacts in a list is a common problem where we need to group contacts that belong to the same person. Two contacts are considered the same if they share any common field: username, email, or phone number. This problem can be solved using graph theory with Depth First Search (DFS). We create an adjacency matrix where contacts are nodes, and edges connect contacts that share any field. Problem Statement Given a list of contacts with three fields each (username, email, phone), we need to ? A contact can store username, email and phone ...

Read More

Find multiplication of sums of data of leaves at same levels in Python

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

Binary trees often require level-by-level processing to solve complex problems. In this problem, we need to find the sum of leaf nodes at each level and multiply all these sums together. Problem Statement Given a binary tree, we need to perform the following operations ? For each level, find sum of all leaves if there are leaves at this level. Otherwise ignore it. Find multiplication of all sums and return it. For example, if we have the following tree structure ? ...

Read More

Find pairs with given sum such that pair elements lie in different BSTs in Python

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

Finding pairs with a given sum from two different Binary Search Trees is a classic problem that combines BST traversal with the two-pointer technique. The key insight is to use the sorted property of BST in-order traversals. Problem Statement Given two Binary Search Trees and a target sum, find all pairs where one element comes from the first BST and another from the second BST, and their sum equals the target. For example, with sum = 12: BST 1 ...

Read More

Find pairs with given sum such that elements of pair are in different rows in Python

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

Finding pairs with a given sum from different rows in a matrix is a common problem in data structures. We need to find all pairs where one element comes from one row and the other from a different row, and their sum equals the target value. So, if the input matrix is ? 2 4 3 5 6 9 8 7 10 11 14 12 13 1 15 16 And sum = 13, then the output will be [(4, 9), (5, 8), (2, 11), (3, ...

Read More

Python - Write multiple files data to master file

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 25-Mar-2026 659 Views

When working with multiple data files, you often need to combine them into a single master file. Python provides several approaches to merge multiple files, from basic file operations to using pandas for structured data. Basic File Operations Approach This method reads multiple text files and writes their content to a master file using standard file operations ? import os # Create sample data files os.makedirs('data_files', exist_ok=True) # Create sample files with open('data_files/file1.txt', 'w') as f: f.write('John, Developer, 50000') f.write('Alice, Designer, 45000') with open('data_files/file2.txt', 'w') ...

Read More

Python - Working with .docx module

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 25-Mar-2026 10K+ Views

Word documents contain formatted text wrapped within three object levels. Lowest level − Run objects, Middle level − Paragraph objects and Highest level − Document object. So, we cannot work with these documents using normal text editors. But we can manipulate these word documents in Python using the python-docx module. Installation The first step is to install this third-party module python-docx. You can use pip ? pip install python-docx Important: After installation, import docx NOT python-docx. Use docx.Document class to start working with the word document. Creating a Basic Word Document ...

Read More
Showing 6551–6560 of 8,546 articles
« Prev 1 654 655 656 657 658 855 Next »
Advertisements