Found 10784 Articles for Python

Program to sort an array based on the parity values in Python

Arnab Chakraborty
Updated on 21-Oct-2020 11:07:37

203 Views

Suppose, we have an array A with few integers. We have to sort the numbers as even then odd. So put the even numbers at first, then the odd numbers. So if the array is like A = [1, 5, 6, 8, 7, 2, 3], then the result will be like [6, 8, 2, 1, 5, 7, 3]To solve this, we will follow these steps −set i := 0 and j := 0while j < size of arrif arr[j] is even, thenswap arr[i] and arr[j], increase i by 1increase j by 1return arrLet us see the following implementation to get ... Read More

Program to find smallest intersecting element of each row in a matrix in Python

Arnab Chakraborty
Updated on 21-Oct-2020 11:06:05

117 Views

Suppose we have a 2D matrix where each row is sorted in ascending order. We have to find the smallest number that exists in every row. If there's no such result, then return −1.So, if the input is like23551010135then the output will be 5To solve this, we will follow these steps −if matrix is empty, thenreturn −1first := a new set from first row of matrixfor each row in matrix, dofirst := Intersect first a set of elements of rowif first is empty, thenreturn −1return minimum of firstLet us see the following implementation to get better understanding −Example Live Democlass Solution: ... Read More

Program to find sibling value of a binary tree node in Python

Arnab Chakraborty
Updated on 21-Oct-2020 11:03:46

543 Views

Suppose we have a value k and a binary search tree, here each node is either a leaf or contains 2 children. We have to find the node containing the value k, and return its sibling's value.So, if the input is likek = 4., then the output will be 10.To solve this, we will follow these steps −Define a function util() . This will take root, k, ansif left of root is not null and right of root is not null, thenreturnif k > value of root, thenif value of right of root is same as k, theninsert value of ... Read More

Program to split a set into equal sum sets where elements in first set are smaller than second in Python

Arnab Chakraborty
Updated on 21-Oct-2020 11:00:47

705 Views

Suppose we have a list of numbers called nums, we have to check whether we can divide the list into two groups A and B such that: The sum of A and the sum of B are same. Here every number in A is strictly smaller than every number in B.So, if the input is like nums = [3, 4, 5, 12], then the output will be True, as we can have A = [3, 4, 5] and B = [12] and both have sum 12.To solve this, we will follow these steps −sort the list numstotal := sum of ... Read More

Program to count total number of set bits of all numbers in range 0 to n in Python

Arnab Chakraborty
Updated on 21-Oct-2020 10:58:45

128 Views

Suppose we have a number num. For each numbers i in the range 0 ≤ i ≤ num we have to calculate the number of 1's in their binary counterpart and return them as a list. So if the number is 5, then the numbers are [0, 1, 2, 3, 4, 5], and number of 1s in these numbers are [0, 1, 1, 2, 1, 2], so it will return 7.To solve this, we will follow these steps −res := an array which holds num + 1 number of 0soffset := 0for i in range 1 to num + 1if ... Read More

Program to count minimum number of animals which have no predator in Python

Arnab Chakraborty
Updated on 21-Oct-2020 10:56:48

741 Views

Suppose we have a list of numbers called nums where nums[i] shows the predator of the ith animal and if there is no predator, it will hold −1. We have to find the smallest number of groups of animals such that no animal is in the same group with its direct or indirect predator.So, if the input is like nums = [1, 2, −1, 4, 5, −1], then the output will be 3, as we can have the groups like: [0, 3], [1, 4], [2, 5].To solve this, we will follow these steps −if A is empty, thenreturn 0adj := ... Read More

Program to separate persons where no enemies can stay in same group in Python

Arnab Chakraborty
Updated on 21-Oct-2020 10:52:05

378 Views

Suppose we have a number n and a 2D matrix called enemies. Here n indicates there is n people labeled from [0, n - 1]. Now each row in enemies contains [a, b] which means that a and b are enemies. We have to check whether it is possible to partition the n people into two groups such that no two people that are enemies are in the same group.So, if the input is like n = 4, enemies = [[0, 3], [3, 2]], then the output will be True, as we can have these two groups [0, 1, 2] ... Read More

Program to find in which interval how many tasks are worked on in Python

Arnab Chakraborty
Updated on 21-Oct-2020 10:36:40

72 Views

Suppose we have a list of intervals where each interval is like [start, end) and we also have a list of strings called types. Now for a given i, the intervals[i] shows the times someone worked on the job types[i] from [start, end). Two intervals of the same type never overlap or touch. So we have to find a sorted merged list where each item has [start, end, num_types], indicates from start to end, num_types number of tasks were being worked on.So, if the input is like intervals = [ [0, 3], [5, 7], [0, 7] ] types = ["problem ... Read More

Program to rotate square matrix by 90 degrees counterclockwise in Python

Arnab Chakraborty
Updated on 21-Oct-2020 10:32:49

2K+ Views

Suppose we have a square matrix, we have to rotate it 90 degrees counter-clockwise.147258369then the output will be789456123To solve this, we will follow these steps −if matrix is empty, thenreturn a blank listn := row count of matrixfor each row in matrix, doreverse the rowfor i in range 0 to n−1, dofor j in range 0 to i−1, doswap matrix[i, j] and matrix[j, i]return matrixLet us see the following implementation to get better understanding −Example Live Democlass Solution:    def solve(self, matrix):       if not matrix or not matrix[0]:          return []       n ... Read More

Program to check robot can reach target by keep moving on visited spots in Python

Arnab Chakraborty
Updated on 21-Oct-2020 10:28:38

178 Views

Suppose we have a robot, that is currently sitting in at position (0, 0) (Cartesian plane). If we have list of its moves that it can make, containing N(North), S(South), W(West), and E(East). However, if the robot reaches a spot it has been in before, it will continue moving in the same direction until it reaches a new unvisited spot. We have to check whether after its moves it will end at (x, y) coordinate or not.So, if the input is likemoves = ['N', 'N', 'E', 'N', 'W', 'S'], coord = [0, -1], then the output will be True, as ... Read More

Advertisements