Python Articles

Page 660 of 855

Final state of the string after modification in Python

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

This problem simulates boxes being pushed in different directions until they reach a stable final state. Given a string where 'R' represents a box pushed right, 'L' represents a box pushed left, and '.' represents empty space, we need to find the final configuration after all movements stop. Problem Understanding Each box marked 'R' pushes subsequent boxes to the right, while boxes marked 'L' push preceding boxes to the left. The simulation continues until no more movement is possible ? Algorithm Approach We use a two-pass algorithm to calculate the net force on each position ? ...

Read More

LFU Cache in Python

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

A Least Frequently Used (LFU) cache is a data structure that removes the least frequently accessed item when the cache reaches its maximum capacity. Python's collections.OrderedDict helps track both frequency and insertion order for efficient implementation. LFU Cache Operations The LFU cache supports two main operations: get(key) – Returns the value if the key exists, otherwise returns -1 put(key, value) – Inserts or updates a key-value pair When capacity is reached, the cache removes the least frequently used element before inserting a new one. Implementation Strategy The implementation uses two data structures: ...

Read More

Longest Increasing Path in a Matrix in Python

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

Given a matrix, we need to find the length of the longest increasing path. From each cell, we can move in four directions − left, right, up, or down. We cannot move diagonally or outside the matrix boundaries. This problem can be solved efficiently using dynamic programming with memoization and depth-first search (DFS). Problem Example Consider this matrix ? 9 9 4 6 6 8 2 1 1 The longest increasing path is 1 → 6 → 8 → 9 with length 4. Algorithm Approach ...

Read More

Optimize Water Distribution in a Village in Python

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

The water distribution optimization problem is a classic application of graph theory and the Minimum Spanning Tree (MST) algorithm. Given n houses, we can either build wells directly in houses or connect them via pipes. The goal is to minimize the total cost to supply water to all houses. The problem can be modeled as finding the MST of a graph where: Each house is a node Building a well is represented as connecting to a virtual node (node 0) Pipes between houses are edges with their respective costs Algorithm Approach We use Kruskal's algorithm ...

Read More

String Transforms Into Another String in Python

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

Sometimes we need to check if we can transform one string into another by converting all occurrences of specific characters. This problem involves character mapping and transformation rules to determine if str1 can be converted to str2. In one conversion, we can convert all occurrences of one character in str1 to any other lowercase English character. The key constraint is that we need at least one "free" character (not used in str2) to perform intermediate conversions. Problem Example Given str1 = "aabcc" and str2 = "ccdee", we can transform str1 to str2 ? Convert 'c' ...

Read More

Parallel Courses in Python

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

The parallel courses problem involves finding the minimum number of semesters needed to complete N courses with prerequisite dependencies. This is essentially a topological sorting problem that can be solved using BFS (Breadth-First Search). Problem Understanding Given N courses labeled 1 to N and a relations array where relations[i] = [X, Y] means course X must be completed before course Y. We need to find the minimum semesters required to complete all courses, or return -1 if impossible due to circular dependencies. Algorithm Approach We use topological sorting with BFS to process courses level by level ...

Read More

Divide Array Into Increasing Sequences in Python

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

Suppose we have a non-decreasing array of positive integers called nums and an integer K. We need to determine if this array can be divided into one or more disjoint increasing subsequences, each of length at least K. For example, if the input is nums = [1, 2, 2, 3, 3, 4, 4] and K = 3, the output will be True because this array can be divided into two subsequences: [1, 2, 3, 4] and [2, 3, 4], both with lengths at least 3. Algorithm Approach The key insight is that we need to find the ...

Read More

time.process_time() function in Python

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 2K+ Views

The time.process_time() function in Python returns the sum of system and user CPU time consumed by the current process. Unlike time.time(), it excludes time spent sleeping and focuses only on actual processing time. Syntax time.process_time() This function returns a float value representing the CPU time in seconds. Basic Example Here's how to get the current process time ? import time # Get current process time current_time = time.process_time() print(f"Current process time: {current_time} seconds") Current process time: 0.015625 seconds Measuring Execution Time The most common ...

Read More

time.perf_counter() function in Python

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 4K+ Views

In this tutorial, we are going to learn about the time.perf_counter() method in Python. This function is part of the time module and provides the highest available resolution to measure a short duration. The method time.perf_counter() returns a float value representing time in seconds. It includes time elapsed during sleep and is system-wide, making it ideal for measuring elapsed time in code execution. Basic Usage Let's start with a simple example to see how perf_counter() works ? import time # Get current performance counter value print("Current time:", time.perf_counter()) Current time: 263.3530349 ...

Read More

The most occurring number in a string using Regex in python

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 289 Views

In this tutorial, we will write a regex that finds the most occurring number in a string using Python. We'll use the re module for pattern matching and collections.Counter for frequency counting. Steps to Find the Most Occurring Number Import the re and collections modules Initialize the string containing numbers Find all numbers using regex and store them in a list Find the most occurring number using Counter from collections module Example Here's how to implement this solution ? # importing the modules import re import collections # initializing the string ...

Read More
Showing 6591–6600 of 8,546 articles
« Prev 1 658 659 660 661 662 855 Next »
Advertisements