Python Articles

Page 315 of 855

Program to check whether domain and range are forming function or not in Python

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

Suppose we have a list of data x that represents a domain and a list of data y (same size as x) that represents a range. We need to check whether the mapping x → y forms a valid function. In mathematics, a function is a relation where each input (domain element) maps to exactly one output (range element). So, if the input is like x = [1, 3, 2, 6, 5] y = [1, 9, 4, 36, 25], then the output will be True, because each element in x maps to exactly one corresponding element in y (in ...

Read More

Python program to find happiness by checking participation of elements into sets

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

Suppose we have an array nums with n different integers. We also have two disjoint sets A and B. We have one happiness parameter which is set to 0 initially. We go through each integer i in nums. If i is in A then add happiness by 1 and if i is in B decrease it by 1. We have to finally find the final happiness value. So, if the input is like nums = [1, 2, 5, 8, 6, 3], A = {5, 8, 9, 7, 3}, B = {2, 4, 12, 15}, then the output will be ...

Read More

Program to find ex in an efficient way in Python

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

The mathematical constant e raised to the power x (ex) can be calculated efficiently using the Taylor series expansion without library functions. The formula for ex is: ex = 1 + x + x²/2! + x³/3! + x⁴/4! + ... For example, if x = 5, then e5 ≈ 148.4131 because e5 = 1 + 5 + (5²/2!) + (5³/3!) + ... = 148.4131... Algorithm To solve this efficiently, we follow these steps ? Initialize factorial = 1, result = 1, and numerator = x Set n = 20 (number of terms for precision) ...

Read More

Python program to find angle between mid-point and base of a right angled triangle

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 2K+ Views

When we have a right-angled triangle with sides AB and BC, we can find the angle between the midpoint M of the hypotenuse AC and the base BC using trigonometry. This angle can be calculated using the arctangent function. A B C M θ ...

Read More

Python program to find difference between two timestamps

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 3K+ Views

When working with timestamps in different timezones, you often need to calculate the time difference between them. Python's datetime library provides powerful tools to parse timestamp strings and compute differences accurately. The timestamp format "Day dd Mon yyyy hh:mm:ss +/-xxxx" includes timezone information, where +/-xxxx represents the offset from GMT (e.g., +0530 means 5 hours 30 minutes ahead of GMT). Understanding Format Specifiers The strptime() function uses format specifiers to parse timestamp strings − %a − Day in three letter format (Thu, Fri, etc.) %d − Day in numeric format (01-31) %b − Month in ...

Read More

Python program to split string into k distinct partitions

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

When working with strings, we sometimes need to split them into equal-sized partitions and remove duplicate characters from each partition. This creates k distinct partitions where each partition contains unique characters only. Problem Statement Given a string s and a value k (where k is a factor of the string length), we need to: Split the string into n/k substrings of size k Remove duplicate characters from each substring Maintain the original character order within each partition For example, with s = "MMPQMMMRM" and k = 3, we get partitions ["MMP", "QMM", "MRM"] which ...

Read More

Python program to find score and name of winner of minion game

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

The Minion Game is a string-based competition between two players where they create substrings based on whether they start with vowels or consonants. Let's explore how to determine the winner and their score. Game Rules The game follows these rules ? Both players have the same string s Amal creates substrings starting with vowels (A, E, I, O, U) Bimal creates substrings starting with consonants Each player scores 1 point for every occurrence of their substring in the original string The player with the highest total score wins Example Breakdown For the string ...

Read More

Program to update list items by their absolute values in Python

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

Sometimes we need to convert negative numbers in a list to their absolute values while keeping positive numbers unchanged. Python provides several approaches to update list items with their absolute values. Using map() with Lambda Function The map() function applies a lambda function to each element in the list ? def solve(nums): return list(map(lambda x: abs(x), nums)) nums = [5, -7, -6, 4, 6, -9, 3, -6, -2] result = solve(nums) print("Original list:", nums) print("Absolute values:", result) Original list: [5, -7, -6, 4, 6, -9, 3, -6, -2] ...

Read More

Program to find length of a list without using built-in length() function in Python

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

Finding the length of a list without using built-in functions like len() is a common programming exercise. We can achieve this using several creative approaches including loops, mapping, and recursive methods. So, if the input is like nums = [5, 7, 6, 4, 6, 9, 3, 6, 2], then the output will be 9. Method 1: Using map() and sum() We can map each element to 1 and then sum the results ? def find_length_map(nums): return sum(map(lambda x: 1, nums)) nums = [5, 7, 6, 4, 6, 9, 3, 6, ...

Read More

Program to find sum of odd elements from list in Python

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 26-Mar-2026 8K+ Views

In Python, you can find the sum of all odd elements in an existing List using one of the following ways: Using List Comprehension Using a Loop Using filter() Function Using List Comprehension The List Comprehension method allows us to create ...

Read More
Showing 3141–3150 of 8,546 articles
« Prev 1 313 314 315 316 317 855 Next »
Advertisements