Found 10476 Articles for Python

Program to group the 1s with minimum number of swaps in a given string in Python

Arnab Chakraborty
Updated on 16-Oct-2021 10:57:45

322 Views

Suppose we are given a binary string input_str that contains 0s and 1s. Our task is to group the 0s and 1 by swapping the 1s in the given string. We have to perform a minimum number of swap operations, and we have to return that value. One thing to be kept in mind, we can swap the adjacent values only.So, if the input is like input_str = 10110101, then the output will be 4The swap will be like following −10110101->01110101->01111001->01111010->01111100The total number of swaps: 4.To solve this, we will follow these steps −one := a new list containing the ... Read More

Program to find dot product of run length encoded vectors in Python

Arnab Chakraborty
Updated on 16-Oct-2021 10:48:22

421 Views

Suppose we have two lists nums1 and nums2. Each of these two list are representing a vector in run-length encoded form. So as an example, a vector [1, 1, 1, 2, 2, 2, 2] is represented as [3, 1, 4, 2]. (because there are 3 ones and 4 twos). So we have to find the dot product of these two vectors. (The dot product is the sum of element wise multiplication of items present in two vectors).So, if the input is like nums1 = [2, 7, 5, 3] nums2 = [3, 5, 4, 2], then the output will be 109 ... Read More

Program to check there is any common reachable node in a graph or not in Python

Arnab Chakraborty
Updated on 16-Oct-2021 10:44:54

348 Views

Suppose we have an edge list of a directed graph, there are n nodes and node names are 0 to n- 1. We also have two integer values a and b. We have to check whether there is any node c such that we can go from c to a and also c to b.So, if the input is likeAnd a = 2, b = 3, then the output will be True, because here c = 0, so we have route from 0 to 2 and also 0 to 3.To solve this, we will follow these steps −Define a function ... Read More

Program to find maximum coins we can get from disappearing coins matrix in Python

Arnab Chakraborty
Updated on 16-Oct-2021 10:41:14

206 Views

Suppose we have a 2D matrix where each cell matrix[r, c] represents the number of coins present in that cell. When we pick up coins from matrix[r, c], all the coins on row (r - 1) and (r + 1) will disappear, as well as the coins at the two cells matrix[r, c + 1] and matrix[r, c - 1]. We have to find the maximum number of coins we can collect.So, if the input is like28761010425923then the output will be 26 because we can pick cells with the coins 8, 6, and 9 and 3, so total is 26.To ... Read More

Program to check all tasks can be executed using given server cores or not in Python

Arnab Chakraborty
Updated on 16-Oct-2021 10:36:57

162 Views

Suppose we have two lists, they are cores and tasks. The cores[i] indicates number of cores available in the ith server. And tasks[i] indicates the number of cores needed to execute that task. Each task must be run in only one server. And a server may have multiple tasks to run. We have to check whether it's possible to run all the tasks with the given cores or not.So, if the input is like cores = [10, 7] tasks = [7, 3, 2, 2, 1], then the output will be True, because we can put tasks[0] and tasks[1] into first ... Read More

Program to check there is any forward path in circular cyclic list or not in Python

Arnab Chakraborty
Updated on 16-Oct-2021 10:34:29

285 Views

Suppose we have a circular list called nums. So the first and the last elements are neighbors. So starting from any index say i, we can move nums[i] number of steps forward if nums[i] is a positive value, otherwise backwards if it's a negative value. We have to check whether there is a cycle whose length is greater than one such that the path only goes forwards or only goes backwards.So, if the input is like nums = [-1, 2, -1, 1, 2], then the output will be True, because there is a forward path [1 -> 3 -> 4 ... Read More

Program to count number of characters in each bracket depth in Python

Arnab Chakraborty
Updated on 16-Oct-2021 10:28:57

545 Views

Suppose we have a string s which consists of only three characters "X", "(", and ")". The string has balanced brackets and in between some "X"s are there along with possibly nested brackets may also there recursively. We have to find the number of "X"s at each depth of brackets in s, starting from the shallowest depth to the deepest depth.So, if the input is like s = "(XXX(X(XX))XX)", then the output will be [5, 1, 2]To solve this, we will follow these steps −depth := -1out := a new listfor each c in s, doif c is same as ... Read More

Program to get final string after shifting characters with given number of positions in Python

Arnab Chakraborty
Updated on 16-Oct-2021 10:21:11

668 Views

Suppose we have a lowercase string s and another list of integers called shifts whose length is same as the length of s. Here each element in shifts[i] indicates it to shift the first i + 1 letters of s by shifts[i] positions. If shifting crosses 'z' it will be wrap up to 'a'. We have to find the resulting string after applying shifts to s.So, if the input is like s = "tomato" shifts = [2, 5, 2, 3, 7, 4], then the output will be "qjcoes" so, after shifting first character 2 places, it will be 't' to ... Read More

Program to count number of horizontal brick pattern can be made from set of bricks in Python

Arnab Chakraborty
Updated on 16-Oct-2021 10:16:54

266 Views

Suppose we have a list of numbers called bricks and two other values width and height. Each element in bricks[i] represents a brick whose length is bricks[i] units and width is 1 unit. We have to find the number of ways to lay the bricks such that we get full layout of bricks with the given width and height. We can reuse the bricks but can only be laid horizontally.So, if the input is like bricks = [2, 1] width = 3 height = 2, then the output will be 9 because −To solve this, we will follow these steps ... Read More

Program to find number of pairs where elements square is within the given range in Python

Arnab Chakraborty
Updated on 16-Oct-2021 10:14:18

215 Views

Suppose we have two list of numbers nums1 and nums2. And also have two numbers lower and upper. We have to find the number of pairs (i, j) such that lower ≤ nums1[i]^2 + nums2[j]^2 ≤ upper.So, if the input is like nums1 = [5, 3, 2] nums2 = [8, 12, 6] lower = 10 upper = 50, then the output will be 2 because the pairs are like (1, 2) and (2, 2)10

Advertisements