Python Articles - Page 715 of 1048

Check if the array has an element which is equal to product of remaining elements in Python

Arnab Chakraborty
Updated on 15-Jan-2021 06:27:06

163 Views

Suppose we have an array called nums we have to check whether the array contains an element whose value is same as product of all other elements.So, if the input is like nums = [3, 2, 24, 4, 1], then the output will be True, 24 = (3*2*4*1).To solve this, we will follow these steps −mul := 1for i in range 0 to size of nums - 1, domul := mul * nums[i]for i in range 0 to size of nums - 1, doif nums[i] is same as (mul / nums[i]), thenreturn Truereturn FalseLet us see the following implementation to ... Read More

Check if the array can be sorted using swaps between given indices only in Python

Arnab Chakraborty
Updated on 15-Jan-2021 06:25:56

192 Views

Suppose we have an array called nums with unique values from range [0, n – 1]. This array is unsorted. We also have another array of pairs where each pair contains indices where the elements of the array can be swapped. We can swap multiple number of times. We have to check whether we can arrange the array in sorted order using these swaps or not.So, if the input is like nums = [6, 1, 7, 3, 0, 5, 4, 2] pairs = [(0, 4), (6, 0), (2, 7)], then the output will be True, as we can swap index ... Read More

Check if sums of i-th row and i-th column are same in matrix in Python

Arnab Chakraborty
Updated on 15-Jan-2021 06:22:35

241 Views

Suppose we have a 2D matrix. We have to check whether the sum of i-th row is same as the sum of i-th column or not.So, if the input is like23451064214671567then the output will be True, as the sum of first row and column is (2 + 3 + 4 + 5) = 14 and (2 + 10 + 1 + 1) = 14.To solve this, we will follow these steps −row := row count of matcol := column count of mattotal_row := 0, total_col := 0for i in range 0 to row - 1, dototal_row := 0, total_col := ... Read More

Check if sum of divisors of two numbers are same in Python

Arnab Chakraborty
Updated on 15-Jan-2021 06:21:13

307 Views

Suppose we have two numbers p and q. We have to check whether the sum of all divisors of these tow numbers are same or not.So, if the input is like p = 559, q = 703, then the output will be True the divisors of 559 is 1, 13, 43 and 703 is 1, 19, 37. The sum of the divisors are 57.To solve this, we will follow these steps −Define a function divSum() . This will take ntotal := 1i := 2while i * i

Check if suffix and prefix of a string are palindromes in Python

Arnab Chakraborty
Updated on 15-Jan-2021 06:20:34

359 Views

Suppose we have a string s, we have to check whether the string palindromes as its prefix and suffix substrings or not.So, if the input is like s = "levelishighforracecar", then the output will be True as there are palindrome prefix and suffix: "level" and "racecar" respectively.To solve this, we will follow these steps −l := size of sfor i in range 2 to l + 2, doif substring of s up to index i is palindrome, thencome out from loopif i is same as(l + 1) , thenreturn Falsefor i in range 2 to l + 2, doif substring ... Read More

Check if subarray with given product exists in an array in Python

Arnab Chakraborty
Updated on 15-Jan-2021 06:18:42

646 Views

Suppose we have an array called nums and this contains positive and negative numbers. We have another value k. We have to check whether any subarray whose product is k is present in the array or not.So, if the input is like nums = [-2, -1, 1, 3, 5, 8], k = 6, then the output will be True as the subarray is [-2, -1, 3]To solve this, we will follow these steps −minimum := nums[0], maximum := nums[0]prod_max := nums[0]for i in range 1 to size of nums - 1, doif nums[i] < 0, thenswap maximum and minimummaximum := ... Read More

Check if strings are rotations of each other or not in Python

Arnab Chakraborty
Updated on 15-Jan-2021 06:17:37

823 Views

Suppose we have two strings s and t, we have to check whether t is a rotation of s or not.So, if the input is like s = "hello", t = "llohe", then the output will be True.To solve this, we will follow these steps −if size of s is not same as size of t, thenreturn Falsetemp := s concatenate with s againif count of t in temp > 0, thenreturn Truereturn FalseLet us see the following implementation to get better understanding −Example CodeLive Demodef solve(s, t):    if len(s) != len(t):       return False     ... Read More

Check if string follows order of characters defined by a pattern or not in Python

Arnab Chakraborty
Updated on 15-Jan-2021 06:16:58

676 Views

Suppose we have a string s and another string t as pattern, we have to check whether characters in s follows the same order as determined by characters present in t. Here we have no duplicate characters in the pattern.So, if the input is like s = "hello world" t = "hw", then the output will be True.To solve this, we will follow these steps −if size of s < size of t, thenreturn Falsefor i in range 0 to size of t - 2, dox := t[i], y := t[i + 1]right := last index of x in sleft ... Read More

Check if right triangle possible from given area and hypotenuse in Python

Arnab Chakraborty
Updated on 15-Jan-2021 06:15:15

265 Views

Suppose we have the hypotenuse and area of a right angle triangle, we have to find the base and height of this triangle. If it is not possible return False.So, if the input is like hypo = 10, area = 24, then the output will be (6, 8).To solve this, we will follow these steps −hypo_sq := hypo * hypos := square root of (hypo_sq / 2.0)maxArea := calculate area of triangle using base s and hypotenuse hypoif area > maxArea, thenreturn Falseleft := 0.0, right := swhile |right - left| > 0.000001, dobase := (left + right) / 2.0if ... Read More

Check if reversing a sub array make the array sorted in Python

Arnab Chakraborty
Updated on 15-Jan-2021 06:13:40

368 Views

Suppose we have an array called nums with unique elements. We have to check whether the array will be sorted or not after reversing one sub-array of it. If the array is already sorted, then also return true.So, if the input is like nums = [4, 6, 27, 25, 15, 9, 37, 42], then the output will be True because if we reverse [9, 15, 25, 27], then the array will be sorted.To solve this, we will follow these steps −n := size of numsif array has only one element then return Truei := 1for i in range 1 to ... Read More

Advertisements