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
Python Articles
Page 656 of 855
LFU Cache in Python
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 MoreLongest Increasing Path in a Matrix in Python
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 MoreOptimize Water Distribution in a Village in Python
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 MoreString Transforms Into Another String in Python
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 MoreParallel Courses in Python
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 MoreDivide Array Into Increasing Sequences in Python
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 Moretime.process_time() function in Python
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 Moretime.perf_counter() function in Python
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 MoreThe most occurring number in a string using Regex in python
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 MoreTaking multiple inputs from user in Python
In this tutorial, we are going to learn how to take multiple inputs from the user in Python. The data entered by the user will be in the string format. So, we can use the split() method to divide the user entered data. Taking Multiple String Inputs Let's take multiple strings from the user using split() ? # taking the input from the user strings = input("Enter multiple names space-separated:- ") # splitting the data strings = strings.split() # printing the data print(strings) The output of the above code is ? ...
Read More