Found 10784 Articles for Python

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

319 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

Complement of Base 10 Integer in Python

Arnab Chakraborty
Updated on 28-Apr-2020 17:23:52

1K+ Views

Suppose we have a number in decimal number system. We have to get the complement of the number in binary form, then again change it to decimal and return the result. So if the number is 20, then the binary form will be 10100, the complement will be 01011, this is 11 in decimalTo solve this, we will follow these steps −s := binary string of the number nsum := 0 and num := 1for each element i in s in reverse directionif i = ‘b’, then return sumotherwise when i = ‘0’, then sum := sum + numnum := ... Read More

Add to Array-Form of Integer in Python

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

390 Views

Suppose we have a number in array form. So if the number is say 534, then it is stored like [5, 3, 4]. We have to add another value k with the array form of the number. So the final number will be another array of digits.To solve this, we will follow these steps −Take each number and make them string, then concatenate the stringconvert the string into integer, then add the numberThen convert it into string again, and make an array by taking each digit form the string.ExampleLet us see the following implementation to get better understanding − Live Democlass ... 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

Reverse Only Letters in Python

Arnab Chakraborty
Updated on 28-Apr-2020 17:15:22

2K+ Views

Suppose we have a string S, we have to find the reversed string where all characters that are not a letter will not change their positions and all letters reverse their positions. So if the given string is "a-bC-dEf-ghIj", then output will be "j-Ih-gfE-dCba"To solve this, we will follow these steps −We will use the regular expression library to solve thisif S is empty, then return Sstr := an empty string, index1 := 0 and index2 := length of S – 1while index1 < length of Sif index2 >= 0 and S[index1] is alphabet and S[index2] is alphabetstr := str ... Read More

Fair Candy Swap in Python

Arnab Chakraborty
Updated on 28-Apr-2020 17:08:15

389 Views

Suppose A and B are two friends. They have candy bars of different sizes. Here A[i] is the size of the i-th bar of candy owned by A, and B[j] is the size of the j-th bar of candy owned by B.Since they are friends, they want to exchange one candy bar each so that after the exchange, both A and B have the same total amount of candy. (The total amount of candy a person has is the sum of the sizes of candy bars they have.) We have to return an integer array suppose ans, where ans[0] is ... Read More

Goat Latin in Python

Arnab Chakraborty
Updated on 28-Apr-2020 16:55:38

427 Views

Suppose we have a set of strings (Sentence), in that set there are few words. Each words are consists of lowercase and uppercase letters. Our task is to convert the sentence into Goat-Latin form. The Goat Latin is similar to the Pig Latin. There are some few conditions.If the word starts with a vowel, then append ‘ma’ with the wordIt the word starts with a consonant, then remove that from beginning, and append it at the end, then add ‘ma’ at the end.Add one letter ‘a’ to the end of each word per its word index in the sentence, starting ... Read More

Rotate String in Python

Arnab Chakraborty
Updated on 28-Apr-2020 16:53:02

422 Views

Suppose we have two strings, A and B. We will rotate the string A and check whether it matches with B at any position of rotating, if so then return true, otherwise false. For example, if A = 'abcde', and B = 'bcdea' So answer will be true, as A can be converted to B after rotating it.To solve this, we will follow these steps −When both A and B are empty, then return true, when both are of different length then return falseA := concatenate A after Ai := 0, and j := 0while i < length of Aif ... Read More

Advertisements