Python Articles - Page 423 of 1048

Program to find minimum one bit operations to make integers zero in Python

Arnab Chakraborty
Updated on 07-Oct-2021 07:20:00

368 Views

Suppose we have a number n, we have to transform it into 0 using the following operations any number of times βˆ’Select the rightmost bit in the binary representation of n.Change the ith bit in the binary representation of n when the (i-1)th bit is set to 1 and the (i-2)th through 0th bits are set to 0.So finally we have to find the minimum number of operations required to transform n into 0.So, if the input is like n = 6, then the output will be 4 because initially 6 = "110", then convert it to "010" by second ... Read More

Program to find out how many transfer requests can be satisfied in Python

Arnab Chakraborty
Updated on 07-Oct-2021 07:13:27

129 Views

Suppose, there are n number of hostel rooms numbered from 0 to n-1. The students in the hostel rooms want to transfer to another room, and they place several requests to do that. No hostel seat remains vacant, a transfer request is only taken care of if another student takes the place of the student willing to transfer. So, given the requests, we have to find out how many requests can be satisfied.So, if the input is like n = 3, requests = [[0, 2], [1, 0], [2, 1]], then the output will be 3.The student in room 0 transfers ... Read More

Program to find out the shortest path to reach the goal in Python

Arnab Chakraborty
Updated on 06-Oct-2021 13:53:47

446 Views

Suppose, we are given a grid where the cells contain various symbols such as 'X', 'O', '*' and '#' and the symbols have various meanings.'#' is the goal cell that we want to reach.'O' is a free cell via which we can travel to the goal cell.'*' is our position in the cell.'X' is a blocked cell, via which we cannot travel.We have to find out the number of moves required to reach the goal cell from our current position in the grid. If the goal is not reachable, we return -1. The grid is given as input to the ... Read More

Program to find largest submatrix with rearrangements in Python

Arnab Chakraborty
Updated on 06-Oct-2021 13:39:57

343 Views

Suppose we have an m x n binary matrix, we can rearrange the columns of the matrix in any order. We have to find the area of the largest submatrix within matrix where every element of the submatrix is 1 after performing some reordering task.So, if the input is like101111001then the output will be 4 because, after column swapping we are getting matrix like110111010here maximum submatrix is of square sized with four 1's.To solve this, we will follow these steps βˆ’row := number of rows of matrix, col := number of columns of matrixfor j in range 0 to col ... Read More

Program to find tuple with same product in Python

Arnab Chakraborty
Updated on 06-Oct-2021 13:27:38

536 Views

Suppose we have an array nums with unique positive values, we have to find the number of tuples (a, b, c, d) such that a*b = c*d where a, b, c, and d are elements of nums, and all elements a, b, c and d are distinct.So, if the input is like nums = [2, 3, 4, 6], then the output will be 8 because we can get tuples like (2, 6, 3, 4), (2, 6, 4, 3), (6, 2, 3, 4), (6, 2, 4, 3), (3, 4, 2, 6), (4, 3, 2, 6), (3, 4, 6, 2), (4, 3, ... Read More

Python program to find list of triplets for which i+j+k is not same as n

Arnab Chakraborty
Updated on 06-Oct-2021 13:28:34

227 Views

Suppose we have three numbers i, j and k and another number n. We shall have to find the list of all triplets (i, j, k) for which i+j+k not same as n. We shall have to solve this problem using list comprehension strategy.So, if the input is like i = 1, j = 1, z = 2 and n = 3, then the output will be [[0, 0, 0], [0, 0, 1], [0, 0, 2], [0, 1, 0], [0, 1, 1], [1, 0, 0], [1, 0, 1], [1, 1, 0], [1, 1, 2]]To solve this, we will follow these ... Read More

Program to minimize hamming distance after swap operations in Python

Arnab Chakraborty
Updated on 06-Oct-2021 13:21:07

340 Views

Suppose we have two integer arrays, src and tgt, both are of same length. We also have an array allowedSwaps where allowedSwaps[i] contains a pair (ai, bi) indicates that we can swap the elements at index ai with element index bi of the array src. (We can swap elements at a specific pair of indices as many times as we want in any order). As we know the Hamming distance of two arrays of the same length, src and tgt, is the number of positions where the elements are different. We have to find the minimum Hamming distance of src ... Read More

Python program to check a number n is weird or not

Arnab Chakraborty
Updated on 06-Oct-2021 13:25:28

1K+ Views

Suppose we have a number n. We shall have to check whether n is weird or not. Here a number is weird when βˆ’ 1. The number is odd 2. The number is not in range 2 to 5 3. The number is even and in range 6 to 20So, if the input is like n = 18, then the output will be Weird because it is even and in range 6 to 20.To solve this, we will follow these steps βˆ’if n is odd, thenreturn "Weird"otherwise when (n > 1 and n < 6) or n > 20, thenreturn ... Read More

Program to find kpr sum for all queries for a given list of numbers in Python

Arnab Chakraborty
Updated on 06-Oct-2021 13:32:02

350 Views

Suppose we have a list of numbers nums. We also have a list of queries where queries[i] contains three elements [k, p, r], for each query we shall have to find kpr_sum. The formula for kpr_sum is like below.$$\mathrm{{π‘˜π‘π‘Ÿ}\_{π‘ π‘’π‘š} =\sum_{\substack{𝑖=𝑃}}^{π‘…βˆ’1}\sum_{\substack{𝑗=𝑖+1}}^{𝑅}(𝐾 βŠ•(𝐴[𝑖]βŠ•π΄[𝑗]))}$$If the sum is too large, then return sum modulo 10^9+7.So, if the input is like nums = [1, 2, 3] queries = [[1, 1, 3], [2, 1, 3]], then the output will be [5, 4] because for the first element it is (1 XOR (1 XOR 2)) + (1 XOR (1 XOR 3)) + (1 XOR (2 XOR 3)) ... Read More

Program to swap nodes in a linked list in Python

Arnab Chakraborty
Updated on 06-Oct-2021 13:14:20

1K+ Views

Suppose we have a list L and another value k. We have to swap kth node from start and kth node from end and return the final list at end.So, if the input is like L = [1, 5, 6, 7, 1, 6, 3, 9, 12] k = 3, then the output will be [1, 5, 3, 7, 1, 6, 6, 9, 12], the 3rd node from start is 6 and from end is 3, so they are swapped.To solve this, we will follow these steps βˆ’temp := Lfor i in range 0 to k-2, dotemp := next of tempfirstNode ... Read More

Advertisements