Programming Articles

Page 20 of 2547

Python - Percentage similarity of lists

Adeeba Khan
Adeeba Khan
Updated on 27-Mar-2026 1K+ Views

Measuring the similarity between two lists is a common task in Python applications like data analysis, text processing, and recommendation systems. This article explores two methods to calculate percentage similarity between lists based on shared elements. List similarity is determined by analyzing overlap or shared elements between lists. This provides a numerical assessment of how much two lists have in common, expressed as a percentage. The choice of method depends on whether you need to handle duplicates and how you want to calculate the similarity ratio. Method 1: Using Set Intersection This approach uses Python's set data ...

Read More

Python - Number of positions where Substrings Match of Length K

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 27-Mar-2026 283 Views

In string processing, we often need to find how many times a substring of specific length appears at different positions in a string. This tutorial demonstrates how to count the number of positions where substrings of length K match a given pattern using Python. Understanding the Problem Given an input string, we need to find how many positions contain a specific substring of length K. For example, if we search for "aaab" (K=4) in the string "aaabddhaaabnsnsaaabkskd", we need to count all occurrences at different positions. Input String: aaabddhaaabnsnsaaabkskd Pattern: aaab (K=4) ...

Read More

Python - Nth smallest Greater than K

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 27-Mar-2026 240 Views

The task of finding the Nth smallest element greater than K is a common problem in data analysis. Python provides several efficient approaches using filtering and sorting techniques to solve this challenge. Understanding the Problem We need to find the Nth smallest number that is greater than a given value K from a list. For example, if we have numbers [2, 6, 4, 7, 8, 10, 15, 9], K=5, and N=3, we first filter numbers greater than 5: [6, 7, 8, 10, 15, 9], then sort them: [6, 7, 8, 9, 10, 15], and return the 3rd element: ...

Read More

Python - Non-overlapping Random Ranges

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 27-Mar-2026 253 Views

Generating non-overlapping random ranges is useful in data analysis, sampling, and testing scenarios. Python's random module provides tools to create ranges that don't intersect with each other. Understanding the Problem Given a start value, end value, and number of ranges needed, we generate random ranges that don't overlap. For example, with start=1, end=50, and 3 ranges, we might get [(5, 8), (15, 22), (35, 40)]. Method 1: Simple Range Generation This approach generates random ranges but doesn't guarantee they won't overlap ? import random def simple_ranges(start, end, num_ranges): ranges ...

Read More

Python - Non-Overlapping occurrences of N Repeated K character

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 27-Mar-2026 198 Views

In this article, we'll find the non-overlapping occurrences of N repeated K characters using Python. This is a common string processing problem where we need to count how many times a specific character appears consecutively a given number of times. Understanding the Problem Given a string, we need to find non-overlapping occurrences where character K appears exactly N consecutive times. For example, in string "AABBCCAAA", if K="A" and N=2, we look for "AA" patterns that don't overlap. String: AABBCCAAA Looking for K='A' repeated N=2 times AA ...

Read More

Python - Non-None elements indices

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 27-Mar-2026 219 Views

The problem at hand is to get the indices of non-None elements in a given input list. This is useful when working with datasets containing missing or empty values represented as None. Understanding the Problem Given a list containing some None values, we need to find the indices of all non-None elements. For example, given the list [1, None, 5, None, 8, 9], we should return [0, 2, 4, 5] as these are the positions of non-None values. Method 1: Using a For Loop The basic approach iterates through the list and checks each element ? ...

Read More

Python - Nested Records List from Lists

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 27-Mar-2026 242 Views

The problem at hand is that we have to create an algorithm for getting nested records lists from given multiple lists with the help of Python. Sometimes we need to combine given lists for a reason in real life applications, so this problem will be helpful to solve those scenarios. Understanding the Logic In this problem we will be given two or more lists and we have to combine them to form nested records lists by applying different approaches. We will explore three methods: using the zip() function, combining loops with zip(), and using user−defined functions. Method ...

Read More

Python - Nested List to single value Tuple

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 27-Mar-2026 281 Views

In Python, converting a nested list to a single-value tuple means extracting all elements from sublists and wrapping each element in its own tuple. This operation is useful in data processing and competitive programming scenarios. Understanding the Problem We need to convert a nested list like [[1, 2], [3, 4]] into single-value tuples like [(1, ), (2, ), (3, ), (4, )]. Each element becomes a tuple containing only that single value. Method 1: Using reduce() Function The reduce() function from functools can flatten the nested list, then we create single-value tuples ? from ...

Read More

Python - Nearest K Sort

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 27-Mar-2026 188 Views

The nearest K sort problem requires sorting a list of elements based on their absolute difference from a given value K. Elements with smaller differences appear first in the sorted result. Understanding the Problem We need to sort list items by their increasing distance from value K. For each element, we calculate |element - K| and arrange elements based on minimum difference first. Nearest K Sort Example (K = 10) Original: 10 4 ...

Read More

Python - Multiplication across Like Keys Value list elements

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 27-Mar-2026 164 Views

In Python, when working with a list of dictionaries, you often need to perform operations across dictionaries with similar keys. One common task is multiplying values for the same keys across all dictionaries in the list. Understanding the Problem Given a list of dictionaries with identical keys, we need to multiply the values for each key across all dictionaries. For example, if we have three dictionaries with key 'A', we multiply all the values associated with 'A' together. Algorithm Step 1 − Initialize an empty dictionary to store the multiplication results Step 2 − Iterate ...

Read More
Showing 191–200 of 25,466 articles
« Prev 1 18 19 20 21 22 2547 Next »
Advertisements