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
Articles by Nikitasha Shrivastava
Page 2 of 17
Python - Numeric Sort in Mixed Pair String List
Sometimes we need to sort lists containing mixed data types, particularly strings that include numeric values. Python provides several approaches to perform numeric sorting on mixed pair string lists where each element contains both text and numbers. Understanding the Problem We have a list where each element is a string containing a name and a number (like "Daniel 4", "Oliver 7"). The goal is to sort this list based on the numeric values rather than alphabetically. For example ? Input List: ['Daniel 4', 'Oliver 7', 'Jack 3', 'Henry 9'] ...
Read MorePython - N Random Tuples list
The problem statement is to generate N random tuples using Python's random module. This is useful in applications that require random data generation, simulations, or testing scenarios. Understanding the Problem We need to generate a specified number of tuples, where each tuple contains random integers within a given range. Here's what we need ? Number of tuples = 5 Minimum value = 0 Maximum value = 10 Generated list = [(4, 1), (2, 10), (5, 3), (6, 3), (1, 7)] Algorithm Step 1 − Import the random module for generating random integers. ...
Read MorePython - Number of positions where Substrings Match of Length K
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 MorePython - Nth smallest Greater than K
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 MorePython - Non-overlapping Random Ranges
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 MorePython - Non-Overlapping occurrences of N Repeated K character
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 MorePython - Non-None elements indices
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 MorePython - Nested Records List from Lists
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 MorePython - Nested List to single value Tuple
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 MorePython - Nearest K Sort
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