Articles on Trending Technologies

Technical articles with clear explanations and examples

Program to implement run length string decoding iterator class in Python

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

Run-length encoding compresses data by representing consecutive identical characters as a count followed by the character. This article implements an iterator class that decodes such strings character by character using next() and hasnext() methods. Problem Understanding Given a run-length encoded string like "2b1a", we need to create an iterator that can decode it step by step. The string "2b1a" represents "bba" (2 b's followed by 1 a). Iterator Methods next() − Returns the next character in the decoded sequence hasnext() − Checks if more characters are available Algorithm Steps The solution follows ...

Read More

Program to count operations to remove consecutive identical bits in Python

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

Suppose we have a binary string s, now let us consider an operation where we select a bit and flip its value from 0 to 1 or vice-versa. We have to find the minimum number of operations needed to get a string with no three identical consecutive bits. So, if the input is like s = "10011100", then the output will be 1, because we can flip 1 to 0 the bit at index 4 to make the string "10010100" where there are no three consecutive identical bits. Algorithm To solve this, we will follow these steps ...

Read More

Program to define data structure that supports rate limiting checking for user in Python

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

Rate limiting is a technique used to control the frequency of requests from users. We need to design a data structure that tracks user requests and determines if a new request should be allowed based on an expiration time window. The data structure should support checking whether a user's request fails based on their previous successful request. A request fails (returns True) only when the user had a successful request within the expire time window. Problem Understanding Given an expire time, we need to: Track the last successful request timestamp for each user Check if a ...

Read More

Program to define data structure that supports range sum in Python

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

Suppose we want to develop a data structure that can build up with a list of integers, and there is a function to find sum of elements from index i to index j-1 whenever we require in an efficient way. There are two functions: Constructor that constructs a new instance with the integer array. get_sum(i, j) returns the sum of integers of array elements from starting index i and ending index j-1. So, if the input is like array = [5, 2, 3, 6, 4, 7, 8, 9, ...

Read More

Python Pandas - Calculate the left slice bound that corresponds to given label

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 131 Views

In Pandas, the get_slice_bound() method calculates the slice bound position for a given label. To find the left slice bound, set the side parameter to 'left'. Syntax index.get_slice_bound(label, side='left', kind='loc') Parameters label − The value to find the slice bound for side − Either 'left' or 'right' to specify which bound kind − Either 'loc' or 'getitem' for different indexing behaviors Creating a Pandas Index First, let's create a Pandas Index with numeric values − import pandas as pd # Creating Pandas index index = pd.Index([10, 20, ...

Read More

Python Pandas - Calculate the right slice bound that corresponds to given label

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 225 Views

To calculate the right slice bound that corresponds to a given label in Pandas, use the index.get_slice_bounds() method. Set the side parameter to right to get the right boundary index position. What is get_slice_bounds()? The get_slice_bounds() method returns the integer position that corresponds to a label in the index. When side='right', it returns the right boundary position for slicing operations. Syntax index.get_slice_bounds(label, side='right', kind='getitem') Parameters label − The label to find the slice bound for side − Either 'left' or 'right' to specify which boundary kind − The kind of slicing ...

Read More

Python Pandas - Get integer location for requested label and find the nearest index value if no exact match

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 280 Views

The get_loc() method in Pandas returns the integer location of a label in an index. When an exact match isn't found, you can use the method="nearest" parameter to find the closest index value. Syntax index.get_loc(key, method=None, tolerance=None) Parameters key − The label to find method − Method to use for inexact matches ('nearest', 'pad', 'backfill') tolerance − Maximum distance for inexact matches Creating a Pandas Index First, let's create a Pandas index with some values ? import pandas as pd # Creating Pandas index index = pd.Index([10, ...

Read More

Python Pandas - Get integer location for requested label and find the previous index value if no exact match

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 191 Views

To get the integer location for a requested label and find the previous index value if no exact match, use the get_loc() method with the method parameter set to "ffill" (forward fill). Syntax index.get_loc(key, method='ffill') Creating a Pandas Index First, let's create a Pandas Index with some integer values − import pandas as pd # Creating Pandas index index = pd.Index([10, 20, 30, 40, 50, 60, 70]) print("Pandas Index...", index) Pandas Index... Int64Index([10, 20, 30, 40, 50, 60, 70], dtype='int64') Finding Exact Matches When the ...

Read More

Python Pandas - Get integer location for requested label

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 508 Views

To get the integer location for a requested label in Pandas, use the index.get_loc() method. This method returns the zero-based position of a label within the index. Syntax index.get_loc(key) Parameters: key − The label for which to find the integer location Basic Example Let's create a Pandas index and find integer locations for specific labels ? import pandas as pd # Create Pandas index object index = pd.Index(list('pqrstuvwxyz')) # Display the Pandas index print("Pandas Index...") print(index) # Get integer location from the given index print("Integer location ...

Read More

Python Pandas - Getting values from a specific level in Multiindex

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 6K+ Views

To get values from a specific level in MultiIndex, use the get_level_values() method in Pandas. This method extracts all values from a particular level of a hierarchical index structure. Creating a MultiIndex First, let's create a MultiIndex with multiple levels using from_arrays() ? import pandas as pd # Create a multi-index with 4 levels multiIndex = pd.MultiIndex.from_arrays([[5, 10], [15, 20], [25, 30], [35, 40]], ...

Read More
Showing 2951–2960 of 61,297 articles
« Prev 1 294 295 296 297 298 6130 Next »
Advertisements