Found 10476 Articles for Python

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

Arnab Chakraborty
Updated on 12-Oct-2021 07:56:58

334 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 2 because ... Read More

Program to find ex in an efficient way in Python

Arnab Chakraborty
Updated on 12-Oct-2021 14:38:19

121 Views

Suppose we have a number n. We have to find $e^{x}$ efficiently, without using library functions. The formula for $e^{x}$ is like$$e^{x} = 1 + x + \frac{x^2}{2!} + \frac{x^3}{3!} + ...$$So, if the input is like x = 5, then the output will be 148.4131 because e^x = 1 + 5 + (5^2/2!) + (5^3/3!) + ... = 148.4131...To solve this, we will follow these steps −fact := 1res := 1n := 20 it can be large for precise resultsnume := xfor i in range 1 to n, dores := res + nume/factnume := nume * xfact := fact ... Read More

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

Arnab Chakraborty
Updated on 12-Oct-2021 07:54:39

2K+ Views

Suppose we have two sides of a right angled triangle, these sides are AB and BC. Consider the midpoint of hypotenuse AC is M. We have to find the angle between M and BC.So, if the input is like ab = 6 bc = 4, then the output will be 56.309932474020215 because arc_tan of ab/bc is 0.9828 but in degrees it is 56.31.To solve this, we will follow these steps −ans := arc-tan(ab/bc)return ans in degreesExampleLet us see the following implementation to get better understandingfrom math import atan, pi def solve(ab, bc):    def deg(rad):       return 180/pi ... Read More

Python program to find difference between two timestamps

Arnab Chakraborty
Updated on 12-Oct-2021 07:49:03

3K+ Views

Suppose we have two times in this format "Day dd Mon yyyy hh:mm:ss +/-xxxx", where Day is three letter day whose first letter is in uppercase. Mon is the name of month in three letters and finally + or - xxxx represents the timezone for example +0530 indicates it is 5 hours 30 minutes more than GMT (other formats like dd, hh, mm, ss are self-explanatory). We have to find absolute difference between two timestamps in seconds.To solve this using python we will use the datetime library. There is a function called strptime() this will convert string formatted date to ... Read More

Python program to split string into k distinct partitions

Arnab Chakraborty
Updated on 12-Oct-2021 07:42:07

571 Views

Suppose we have a string s and and a value k. The value of k is factor of the length of s, say the length is n. We can split s into n/k different substrings called t_i of size k. Then use these t_i to make u_i such thatThe characters present in u_i are subsequence of characters in t_iAny repeat characters will be removed from these string such that frequency of each character in u_i is 1We have to find these u_i stringsSo, if the input is like s = "MMPQMMMRM" k = 3, then the output will be ["MP", ... Read More

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

Arnab Chakraborty
Updated on 12-Oct-2021 07:39:25

463 Views

Suppose there are two players Amal and Bimal. They are playing a game. The game rules are as follows −Both players have a same string s.Both of them have to make substrings using the letters of s.Bimal has to make words starting with consonants.Amal has to make words starting with vowels.The game will end when both players have made all possible substrings.Now the scoring criteria is like: a player gains 1 point for each occurrence of the substring in the string s. We have to find winner of this game and his score.So, if the input is like s = ... Read More

Program to update list items by their absolute values in Python

Arnab Chakraborty
Updated on 12-Oct-2021 07:33:50

245 Views

Suppose we have a list of numbers called nums with positive and negative numbers. We have to update this list so that the final list will only hold the absolute value of each element.So, if the input is like nums = [5, -7, -6, 4, 6, -9, 3, -6, -2], then the output will be [5, 7, 6, 4, 6, 9, 3, 6, 2]To solve this, we will follow these steps −Solve this by map and list operationsdefine one anonymous function say l, that takes x as argument and returns abs(x)using map() method convert each element e from nums to ... Read More

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

Arnab Chakraborty
Updated on 12-Oct-2021 07:31:13

761 Views

Suppose we have a list nums. We have to find the length of this list but without using any length(), size() or len() type of functions.So, if the input is like nums = [5, 7, 6, 4, 6, 9, 3, 6, 2], then the output will be 9.To solve this, we will follow these steps −Solve this by map and list operationsx := a list which contains all elements in numsconvert all elements in x to 1find sum of x by using sum() methodIn this example we have used the map() method to convert all into 1 by defining an ... Read More

Program to find sum of odd elements from list in Python

SaiKrishna Tavva
Updated on 04-Oct-2024 15:40:27

7K+ Views

In Python you can find the sum of all odd elements in an existing List 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 a new list ... Read More

Program to reverse a list by list slicing in Python

Arnab Chakraborty
Updated on 12-Oct-2021 07:21:58

254 Views

Suppose we have a list of n elements called nums. We have to reverse this list by list slicing operations.So, if the input is like nums = [5, 7, 6, 4, 6, 9, 3, 6, 2], then the output will be [2, 6, 3, 9, 6, 4, 6, 7, 5]To solve this, we will follow these steps −list slicing takes at most three parameters separated by colon. First one is start, second one is end and third one is stephere as we start from 0 we do not pass first parameter, as we end at n, we also not providing ... Read More

Advertisements