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 Arnab Chakraborty
Page 25 of 377
Python program to find angle between mid-point and base of a right angled triangle
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 MorePython program to find difference between two timestamps
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 MorePython program to split string into k distinct partitions
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 MorePython program to find score and name of winner of minion game
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 MoreProgram to update list items by their absolute values in Python
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 MoreProgram to find length of a list without using built-in length() function in Python
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 MoreProgram to reverse a list by list slicing in Python
List slicing in Python provides a simple and efficient way to reverse a list using the slice notation [::-1]. This approach creates a new list with elements in reverse order without modifying the original list. Understanding List Slicing Syntax List slicing takes three parameters separated by colons: [start:end:step] start − Starting index (default: 0) end − Ending index (default: length of list) step − Step size (default: 1) For reversing, we use [::-1] where: Empty start and end means include entire list -1 step means move backwards through the list ...
Read MoreProgram to create a list with n elements from 1 to n in Python
Creating a list with n elements from 1 to n is a common task in Python. There are several efficient approaches using list comprehension, range() with list(), and other methods. So, if the input is like n = 5, then the output will be [1, 2, 3, 4, 5] Using List Comprehension List comprehension provides a concise way to create lists by iterating over a range ? def solve(n): return [i for i in range(1, n+1)] n = 5 result = solve(n) print(result) [1, 2, 3, 4, ...
Read MorePython program to find better divisor of a number
Finding the "better divisor" of a number involves comparing divisors based on their digit sum. A divisor with a higher digit sum is considered better, and if digit sums are equal, the smaller number wins. Problem Definition Given a number n, we need to find the best divisor based on these criteria ? The divisor with the highest sum of digits is better If digit sums are equal, the smaller divisor is better Example For n = 180, the divisors are [1, 2, 3, 4, 5, 6, 9, 10, 12, 15, 18, 20, ...
Read MorePython program to get average heights of distinct entries
Suppose we have a set of heights where there may be some duplicate entries. We need to find the average of distinct entries from these heights. So, if the input is like heights = [96, 25, 83, 96, 33, 83, 24, 25], then the output will be 52.2 because the unique elements are [96, 25, 83, 33, 24], so sum is 96 + 25 + 83 + 33 + 24 = 261, average is 261/5 = 52.2. Algorithm To solve this problem, we will follow these steps ? h_set := a set from heights to ...
Read More