Python Articles

Page 664 of 855

Longest Consecutive Sequence in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 5K+ Views

The Longest Consecutive Sequence problem asks us to find the length of the longest sequence of consecutive integers in an unsorted array. For example, in the array [100, 4, 250, 1, 3, 2], the longest consecutive sequence is [1, 2, 3, 4] with length 4. Algorithm Approach We use a set-based approach for O(n) time complexity ? Convert the array to a set for O(1) lookup operations For each number, check if it's the start of a sequence (i.e., number-1 is not in the set) If it's a sequence start, count consecutive numbers and track the ...

Read More

Binary Tree Maximum Path Sum in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 1K+ Views

The maximum path sum problem asks us to find the path in a binary tree with the largest sum of node values. A path can start and end at any nodes and must follow parent-child connections. The path doesn't need to pass through the root. -10 9 10 15 7 ...

Read More

Wildcard Matching in Python

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

Wildcard pattern matching allows you to match strings using special characters. In wildcard matching, '?' matches any single character and '*' matches zero or more characters. This is useful for file pattern matching, search operations, and text processing. Wildcard Characters '?' − Matches exactly one character '*' − Matches zero or more characters Dynamic Programming Approach We can solve wildcard matching using dynamic programming. The idea is to build a 2D table where dp[i][j] represents whether the first i characters of the string match the first j characters of the pattern. Algorithm Steps ...

Read More

Trapping Rain Water in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 1K+ Views

The trapping rain water problem is a classic algorithmic challenge where we calculate how much water can be trapped after raining on an elevation map represented by an array of heights. Each element represents the height of a bar with width 1. ...

Read More

First Missing Positive in Python

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

The First Missing Positive problem asks us to find the smallest missing positive integer from an unsorted array. For example, given the array [4, -3, 1, -1], the result is 2 since 1 is present but 2 is missing. Algorithm Approach We use a cyclic sort approach to solve this efficiently: Add a 0 at the beginning to handle 1-based indexing Place each positive number at its correct index position Scan the array to find the first missing positive Example Implementation Here's the complete solution using cyclic sort ? class Solution: ...

Read More

Longest Valid Parentheses in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 2K+ Views

Finding the longest valid parentheses substring is a common problem that can be solved efficiently using a stack-based approach. Given a string containing only '(' and ')' characters, we need to find the length of the longest valid (well-formed) parentheses substring. For example, in the string "))(())())", the longest valid parentheses substring is "(())())" with length 6. Algorithm Approach We use a stack to track indices of unmatched parentheses ? Initialize a stack with −1 to handle edge cases For each character, if it's '(', push its index onto the stack If it's ')', check ...

Read More

Merge k Sorted Lists in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 1K+ Views

Merging k sorted lists is a classic algorithm problem. Given multiple sorted linked lists, we need to combine them into a single sorted list. Python's heapq module provides an efficient solution using a min-heap data structure. Problem Understanding Given k sorted linked lists like [1, 4, 5], [1, 3, 4], [2, 6], we need to merge them into one sorted list [1, 1, 2, 3, 4, 4, 5, 6]. Algorithm Steps Create a min-heap to store the smallest elements from each list Add the first node of each non-empty list to the heap Repeatedly extract ...

Read More

Insert the string at the beginning of all items in a list in Python

Mohd Mohtashim
Mohd Mohtashim
Updated on 25-Mar-2026 360 Views

In this tutorial, we'll learn how to insert a string at the beginning of all items in a list in Python. For example, if we have a string "Tutorials_Point" and a list containing elements like "1", "2", "3", we need to add "Tutorials_Point" in front of each element to get "Tutorials_Point1", "Tutorials_Point2", "Tutorials_Point3". Using List Comprehension with format() The most straightforward approach is using list comprehension with string formatting − sample_list = [1, 2, 3] result = ['Tutorials_Point{0}'.format(i) for i in sample_list] print(result) ['Tutorials_Point1', 'Tutorials_Point2', 'Tutorials_Point3'] Using map() with format() ...

Read More

Python - Insert list in another list

Mohd Mohtashim
Mohd Mohtashim
Updated on 25-Mar-2026 389 Views

When working with lists in Python, you often need to insert one list into another. Python provides several methods to accomplish this: append(), extend(), insert(), and list concatenation with + operator. Using append() Method The append() method adds the entire second list as a single element ? first_list = [1, 2, 3, 4, 5] second_list = [6, 7, 8, 9, 10] first_list.append(second_list) print("Using append():", first_list) Using append(): [1, 2, 3, 4, 5, [6, 7, 8, 9, 10]] Using extend() Method The extend() method adds each element of the second ...

Read More

Python - Increasing alternate element pattern in list

Mohd Mohtashim
Mohd Mohtashim
Updated on 25-Mar-2026 208 Views

This article demonstrates how to create an increasing alternate element pattern in a list where each original element is followed by a string of asterisks that increases in length. We'll use list comprehension with enumerate() to achieve this pattern efficiently. Understanding the Pattern The increasing alternate element pattern takes a list like [1, 2, 3] and transforms it to [1, '*', 2, '**', 3, '***']. Each element is followed by asterisks equal to its position (1-indexed). Using List Comprehension with enumerate() The enumerate() function adds a counter to each element, starting from 1. We use nested ...

Read More
Showing 6631–6640 of 8,549 articles
« Prev 1 662 663 664 665 666 855 Next »
Advertisements