Programming Articles

Page 146 of 2547

Python program to count pairs for consecutive elements

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

When working with numeric strings, you often need to count how many times each digit appears consecutively. Python's itertools.groupby() function is perfect for this task as it groups consecutive identical elements together. For example, if the input is s = "11522226551", the output should be [(1, 2), (5, 1), (2, 4), (6, 1), (5, 2), (1, 1)] because digit 1 appears twice consecutively at the beginning, then single 5, then four 2s, and so on. Algorithm To solve this problem, we will follow these steps ? Use groupby() function to group consecutive identical digits Create ...

Read More

Python program to count distinct words and count frequency of them

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

When working with text data, we often need to count how many distinct words appear and track their frequencies. Python provides several approaches to solve this problem efficiently. In this example, we have a list of words where some words may appear multiple times. We need to return the count of distinct words and their frequencies in order of first appearance. Problem Example Given the input: words = ["Book", "Sound", "Language", "Computer", "Book", "Language"] The expected output is (4, '2 1 2 1') because: There are 4 distinct words ...

Read More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 548 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 409 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 165 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 622 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 506 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 284 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
Showing 1451–1460 of 25,469 articles
« Prev 1 144 145 146 147 148 2547 Next »
Advertisements