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 Asif Rahaman
Page 2 of 6
Python - Lambda Function to Check if a value is in a List
Lambda functions in Python are nameless functions which are useful when we want to combine multiple expressions in a single line. The lambda function is convenient when we are sure that we won't be reusing the code anywhere else in the program. In this article we will understand how to check if a value exists in a list using the lambda function combined with various built-in methods. Using The filter() Method The filter() method in Python is a built-in function which creates new elements in the list depending upon certain filter conditions. It returns a filter object which ...
Read MorePython - Kth Valid String
Finding the kth valid string is a common programming problem where we need to identify the kth element from a list that meets specific string validation criteria. In this article, we'll explore multiple approaches using iteration, filter methods, list comprehensions, and the Pandas library. Understanding The Problem Statement Given a list of elements and a value k, we need to find the kth valid string. A valid string typically contains only alphabetic characters and is not empty ? data = ["", "orange", "75", "apple", "123abc", "hello"] k = 2 # Valid strings: "orange", "apple", "hello" ...
Read MorePython - Kth Index Tuple List Mean
Finding the mean of elements at the Kth index across multiple tuples is a common data analysis task in Python. Given a list of tuples and an index K, we calculate the average of all elements at position K across the tuples. Understanding The Problem Statement Our input contains a list of tuples and the value of K representing the index position. tuples_list = [(1, 2, 3), (4, 5, 6), (7, 8, 9)] k = 1 We need to find the mean of all elements at index K=1: From tuple (1, 2, 3): ...
Read MorePython - Kth digit Sum
Finding the kth digit sum means calculating the sum of all digits at the kth position from each number in a list. This concept is useful in data validation, financial analysis, and pattern recognition. In this article, we will explore multiple approaches to find the kth digit sum using loops, list comprehensions, lambda functions, and libraries like NumPy and functools. Understanding The Problem Statement Suppose we have the following inputs − numbers = [123, 456, 789] k = 0 We need to find the kth digit sum. At index k=0, the digits are: 1 ...
Read MorePython - K Summation from Two lists
K summation from two lists means finding all pairs of elements where one element comes from the first list, another from the second list, and their sum equals a target value k. Python offers several approaches to solve this problem efficiently. Using Brute Force Method The brute force approach checks every possible combination of elements from both lists ? def k_sum_naive(list1, list2, k): result = [] for num1 in list1: for num2 in list2: ...
Read MorePython - K Maximum Elements with Index in List
Finding the k maximum elements with their indices from a list is a common programming task in Python. This article explores multiple approaches using built-in functions, libraries like NumPy and Pandas, and different algorithms to solve this problem efficiently. Using Brute Force Method The brute force approach repeatedly finds the maximum element, stores its value and index, then replaces it to find the next maximum ? def k_max_elements(data, k): data_copy = data.copy() # Work with copy to preserve original result = [] ...
Read MorePython - K Matrix Initialization
Matrix is a popular data representation technique in mathematics, machine modeling, etc. They are designed to deal with linear functions. Matrix initialization is a process to fill up the elements (rows and columns) of the matrix with either random or some predetermined values. After initialization there should be no undefined entries in the matrix. Initializing the matrix is one of the essential tasks in several fields, like competitive programming, machine and deep learning algorithms. In this article, we will learn how to initialize the matrix using various methods like loops, NumPy arrays, etc. Using Custom Logic with Lists ...
Read MorePython - K length decimal Places
In Python, controlling decimal precision is essential for mathematical calculations and data formatting. This article explores various methods to format decimal numbers to k decimal places using built-in functions and external libraries. Using round() Method The round() function is Python's built-in method for controlling decimal precision. It follows mathematical rounding rules. Syntax round(number, k) Where number is the decimal number and k is the number of decimal places. Example def round_off(number, k): rounded = round(number, k) return rounded ...
Read MorePython - K length consecutive characters
Consecutive characters are those characters that appear one after the other. K-length consecutive characters mean the same character appearing k times consecutively. In this article, we will explore several methods to find such patterns using brute force, regular expressions, sliding windows, and NumPy arrays. Using the Brute Force Method The brute force approach checks every possible substring of length k to see if all characters are the same − Initialize an empty list to store results Iterate through the string, checking n-k+1 positions Extract k-length substrings and check if all characters are identical Use a set ...
Read MorePython - K length Combinations from given characters
K length combinations from given characters mean the combinations of characters that we can create with the given characters, which are exactly of length k. In this article, we will explore several methods to achieve this, like recursion, map and lambda function, itertools library, etc. While recursion and lambda functions are custom functions, the itertools provide the in-built method to generate the combinations. Using Recursion Recursion is a programming technique where we break a large problem into smaller chunks. We need to define a base case to stop the recursion and prevent infinite loops. Example In ...
Read More