Python Articles - Page 880 of 1048

Day of the Year in Python

Arnab Chakraborty
Updated on 29-Apr-2020 07:54:48

4K+ Views

Suppose, we have a date in the format “YYYY-MM-DD”. We have to return the day number of the year. So if the date is “2019-02-10”, then this is 41st day of the year.To solve this, we will follow these steps −Suppose D is an array of day count like [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]Convert the date into list of year, month and dayif the year is leap year then set date D[2] = 29Add up the day count up to the month mm – 1. and day count after that.ExampleLet us see ... Read More

Number of Equivalent Domino Pairs in Python

Arnab Chakraborty
Updated on 29-Apr-2020 07:52:45

326 Views

Suppose we have a list of dominos. Each domino has two numbers. Two dominos D[i] = [a, b] and D[j] = [c, d] will be same if a = c and b = d, or a = d and b = c. So one domino can be reversed. We have to return number of pairs (i, j) for which 0

Relative Sort Array in Python

Arnab Chakraborty
Updated on 18-May-2020 05:54:15

770 Views

Suppose we have two arrays arr1 and arr2, the elements of arr2 are unique, and all elements in arr2 are also present in arr1. We have to sort the elements of arr1 in such a way that the relative ordering of items in arr1 are the same as in arr2. If there are some elements that are not present in arr2, they should be placed at the end of arr1 in ascending order. So if the arr1 is like [2, 3, 1, 3, 2, 4, 6, 7, 9, 2, 19], and arr2 is like [2, 1, 4, 3, 9, 6], ... Read More

Distribute Candies to People in Python

Arnab Chakraborty
Updated on 29-Apr-2020 07:45:56

1K+ Views

Suppose we want to distribute some number of candies to a row of n people in the following way −We then give 1 candy to the first people, 2 candies to the second people, and so on until we give n candies to the last people.After that, we go back to the start of the row again, give n + 1 candies to the first people, n + 2 candies to the second people, and so on until we give 2 * n candies to the last people.We will repeat this process until we run out of candies. The last ... Read More

Occurrences After Bigram in Python

Arnab Chakraborty
Updated on 28-Apr-2020 17:45:32

177 Views

Suppose there are words given. These are first and second, consider occurrences in some text of the form "first second third", here second comes immediately after the first, and third comes immediately after the second.For each such cases, add "third" into the answer, and show the answer. So if the text is like “lina is a good girl she is a good singer”, first = “a”, second = “good”, the answer will be [girl, singer]To solve this, we will follow these steps −text := split the string by spacesres is an empty listfor i := 0 to size of text ... Read More

Last Stone Weight in Python

Arnab Chakraborty
Updated on 28-Apr-2020 17:36:07

1K+ Views

Suppose we have some rocks, each rock has a positive integer weight. In each turn, we will take two heaviest rocks and smash them together. consider the stones have weights x and y and x 1:          stones.sort()          s1,s2=stones[-1],stones[-2]          if s1==s2:             stones.pop(-1)             stones.pop(-1)          else:             s1 = abs(s1-s2)             stones.pop(-1)             stones[-1] = s1       if len(stones):          return stones[-1]       return 0 ob1 = Solution() print(ob1.lastStoneWeight([2,7,4,1,6,1]))Input[2,7,4,1,6,1]Output1

Partition Array Into Three Parts With Equal Sum in Python

Arnab Chakraborty
Updated on 28-Apr-2020 17:30:30

470 Views

Suppose we have an array A of integers, our output will be true if and only if we can partition the array into three non-empty parts whose sum is equal.Formally, we can partition the array if we can find the indexes i+1 < j with (A[0] + A[1] + ... + A[i] is same as A[i+1] + A[i+2] + ... + A[j-1] and A[j] + A[j-1] + ... + A[A.length - 1])So if the input is [0, 2, 1, -6, 6, -7, 9, 1, 2, 0, 1], then the output will be true. Three arrays will be [0, 2, 1], ... Read More

Pairs of Songs With Total Durations Divisible by 60 in Python

Arnab Chakraborty
Updated on 28-Apr-2020 17:27:04

2K+ Views

Suppose we have a list of songs, the i-th song has a duration of time[i] seconds. We have to find the number of pairs of songs for which their total time in seconds is divisible by 60.So if the time array is like [30, 20, 150, 100, 40], then the answer will be 3. Three pairs will be (3, 150), (20, 100), (20, 40) for all cases the total duration is divisible by 60.To solve this, we will follow these steps −Take a map rem to store remainders. Set ans := 0for all elements i in time −if i is ... Read More

Sum of Even Numbers After Queries in Python

Arnab Chakraborty
Updated on 28-Apr-2020 17:19:35

3K+ Views

Suppose we have an array of integers called A, and an array queries. For the i-th query value = queries[i][0] and index = queries[i][1], we will add value to A[index]. Then, the answer of the i-th query is the sum of the even values of A. We have to find the answer to all queries. We will find an array, that should have answer[i] as the answer to the i-th query. So if the array is like [1, 2, 3, 4], and the query array is like [[1, 0], [-3, 1], [-4, 0], [2, 3]], then the answer array will ... Read More

Jewels and Stones in Python

Arnab Chakraborty
Updated on 28-Apr-2020 16:46:58

744 Views

Suppose we have a string J that indicates some letters that are considered as Jewel, and another string S, that represents some stones that we have. Our task is to find how many of stones in S is also jewel. The letters in J and S are case sensitive. So if the J = “aZc”, and S = “catTableZebraPicnic” then there are 7 jewels.To solve this we will convert the string into a list of characters. If the character in J present in S, then increase the count.ExampleLet us see the following implementation to get better understanding − Live Democlass Solution(object): ... Read More

Advertisements