Count Pairs for Consecutive Elements in Python

Arnab Chakraborty
Updated on 12-Oct-2021 08:05:34

584 Views

Suppose we have a numeric string s contains few digits. The digits may occur multiple times. We have to return some pairs (digit, count) represents which digit has occurred consecutively how many times in s. To solve this problem we can use the groupby() function that comes under itertools library. This will return one iterator object inside that each item will be at first place and another groupby objects at the second place. We have to count number of groupby objects for each pair.So, if the input is like s = "11522226551", then the output will be [(1, 2), (5, ... Read More

Count Distinct Words and Their Frequency in Python

Arnab Chakraborty
Updated on 12-Oct-2021 08:02:11

885 Views

Suppose we have a list of words. These words may occur multiple times. We have to show the frequencies of these words and count how many distinct words are there.So, if the input is like words = ["Book", "Sound", "Language", "Computer", "Book", "Language"], then the output will be (4, '2 1 2 1') because there are four distinct words, the first and third words have occurred twice.To solve this, we will follow these steps −d:= an OrderedDict to store items based on insert orderfor each w in words, doif w is in d, thend[w] := d[w] + 1otherwise, d[w] := ... Read More

Find Perimeter of a Polygon in Python

Arnab Chakraborty
Updated on 12-Oct-2021 08:02:08

2K+ Views

Suppose we have a list of ordered points represents a simple polygon endpoint on a 2D plane. We have to find the perimeter of this polygon.So, if the input is like points = [(0, 0), (0, 5), (3, 5), (3, 0)], then the output will be 16 becausetwo sides are of length 3 and two sides are of length 5, so 2*5 + 2*3 = 16.To solve this, we will follow these steps −Define a function getInfo() . This will take x1, y1, x2, y2return square root of ((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)) which is Euclidean distancebetween (x1, y1) and (x2, y2)From the main ... Read More

Find Happiness by Checking Participation of Elements into Sets in Python

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

347 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

Check Domain and Range Forming Function in Python

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

508 Views

Suppose we have a list of data say x, represents a domain and a list of data y (size of y is same as size of x), represents a range. We have to check whether x -> y is a function or not. Here we are considering all elements in x and y are positive.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 for each x, the corresponding y is its square value here, so this is a function.To solve this, we ... Read More

Find Angle Between Mid Point and Base of Right Angled Triangle in Python

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

Find Difference Between Two Timestamps in Python

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

Split String into K Distinct Partitions in Python

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

585 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

Find Score and Winner of Minion Game in Python

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

471 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

Update List Items by Their Absolute Values in Python

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

252 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

Advertisements