Found 10783 Articles for Python

Find the cordinates of the fourth vertex of a rectangle with given 3 vertices in Python

Arnab Chakraborty
Updated on 19-Aug-2020 11:38:04

602 Views

Suppose we have a grid of size Q * P, this grid contains exactly three asterisk '*' and every other cell there is dot '.', where '*' is for a vertex of a rectangle. We have to find the coordinates of the missing vertex. Here we will consider 1-based indexing.So, if the input is like grid = [ ".*.", "...", "*.*" ], then the output will be [1, 3], this is the missing coordinate.To solve this, we will follow these steps −p := number of rowsq := number of columnsrow := make a map for all row number, and associated ... Read More

Find the character in first string that is present at minimum index in second string in Python

Arnab Chakraborty
Updated on 19-Aug-2020 11:31:23

259 Views

Suppose we have a string str and another string patt, we have to find determine the character in patt that is present at the minimum index of str. If no character patt1 is present in str1 then return -1.So, if the input is like str = "helloworld" and patt = "wor", then the output will be 'o' as 'o' is present at minimum index in strTo solve this, we will follow these steps −for i in range 0 to size of patt, dofor j in range 0 to size of Str, doif patt[i] is same as Str[j] and j < ... Read More

Find sum of all elements in a matrix except the elements in row and-or column of given cell in Python

Arnab Chakraborty
Updated on 19-Aug-2020 11:30:03

783 Views

Suppose we have a 2D matrix and a set of cell indexes. Cell indices are represented as (i, j) where i is row and j is column, now, for every given cell index (i, j), we have to find the sums of all matrix elements excluding the elements present in ith row and/or jth column.So, if the input is like223457643cell indices = [(0, 0), (1, 1), (0, 1)], then the output will be [19, 14, 20]To solve this, we will follow these steps −n := size of ind_arrans := a new listfor i in range 0 to n, doSum := ... Read More

Find substrings that contain all vowels in Python

Arnab Chakraborty
Updated on 19-Aug-2020 11:28:19

691 Views

Suppose we have a string in lowercase alphabets, we have to find substrings that contain all the vowels at least once and there exist no consonants in that substrings.So, if the input is like "helloworldaeiouaieuonicestring", then the output will be ['aeiou', 'aeioua', 'aeiouai', 'aeiouaiu', 'eioua', 'eiouai', 'eiouaiu']To solve this, we will follow these steps −n := size of sfor i in range 0 to n, domy_map := a new mapfor j in range i to n, doif s[j] not vowel, thencome out from the loopmy_map[s[j]] := 1if size of my_map is same as 5, thendisplay s[from index i to j ... Read More

Find subsequences with maximum Bitwise AND and Bitwise OR in Python

Arnab Chakraborty
Updated on 19-Aug-2020 11:26:24

628 Views

Suppose we have an array of n elements, we have to show the maximum sum by choosing two subsequences of the array (they may or may not be different) so that the sum of bit-wise AND operation of all elements of the first subsequence and bit-wise OR operation of all the elements of the second subsequence is maximum.So, if the input is like A = {4, 6, 7, 2}, then the output will be 14 as we are getting maximum AND value by selecting 7 only and maximum OR value by selecting all (4 | 6 | 7 | 2) ... Read More

Find sub-arrays from given two arrays such that they have equal sum in Python

Arnab Chakraborty
Updated on 19-Aug-2020 11:24:34

301 Views

Suppose we have two arrays P and Q whose size are N, they are holding numbers 1 to N. We have to find sub-arrays from the given arrays so that they have equal sum. Finally return the indices of such sub-arrays. If there is no solution, then return -1.So, if the input is like P = [2, 3, 4, 5, 6], Q = [9, 3, 2, 6, 5], then the output will be Indices in first array : 0, 1, 2 and indices in second array: 0, so P[0..2] = 2 + 3 + 4 = 9 and Q[0] = ... Read More

Find Shortest distance from a guard in a Bankin Python

Arnab Chakraborty
Updated on 19-Aug-2020 11:19:35

98 Views

Suppose we have a matrix that is filled with three letters 'O', 'G', and 'W' where 'O' is representing open space, 'G' is representing guards and 'W' is representing walls in a bank, we have to replace all of the O's in the matrix with respect of their shortest distance from one guard, we cannot go through any walls. In the output matrix guards are replaced with 0 and walls are replaced by -1.So, if the input is likeOOOOGOOOWOOWOOOGWWWOOOOOGthen the output will be33210233-111-14320-1-1-1112210To solve this, we will follow these steps −M := 5N := 5dir_row := [-1, 0, 1, 0]dir_col ... Read More

Find same contacts in a list of contacts in Python

Arnab Chakraborty
Updated on 19-Aug-2020 11:16:45

150 Views

Suppose we have a list of contacts holding username, email and phone number in any order, we have to find the same contacts (When same person have many different contacts) and return the same contacts together. We have to keep in mind that −A contact can store username, email and phone fields according to any order.Two contacts are same if they have either same username or same email or same phone number.So, if the input is like Contacts = [{"Amal", "amal@gmail.com", "+915264"}, { "Bimal", "bimal321@yahoo.com", "+1234567"}, { "Amal123", "+1234567", "amal_new@gmail.com"}, { "AmalAnother", "+962547", "amal_new@gmail.com"}], then the output will be [0, ... Read More

Find multiplication of sums of data of leaves at same levels in Python

Arnab Chakraborty
Updated on 19-Aug-2020 11:12:34

63 Views

Suppose we have a Binary Tree. We have to perform following operations −For each level, find sum of all leaves if there are leaves at this level. Otherwise ignore it.Find multiplication of all sums and return it.So, if the input is likethen the output will be 270. First two levels have no leaves. Third level has single leaf 9. Last level has four leaves 2, 12, 5 and 11. So result is 9 * (2 + 12 + 5 + 11) = 270To solve this, we will follow these steps −if root is null, thenreturn 0res := 1que := a ... Read More

Find pairs with given sum such that pair elements lie in different BSTs in Python

Arnab Chakraborty
Updated on 19-Aug-2020 11:03:13

68 Views

Suppose we have two given Binary Search Trees and another sum is given; we have to find pairs with respect of given sum so that each pair elements must be in different BSTs.So, if the input is like sum = 12then the output will be [(6, 6), (7, 5), (9, 3)]To solve this, we will follow these steps −Define a function solve() . This will take trav1, trav2, Sumleft := 0right := size of trav2 - 1res := a new listwhile left < size of trav1 and right >= 0, doif trav1[left] + trav2[right] is same as Sum, theninsert (trav1[left], ... Read More

Advertisements