Found 10476 Articles for Python

Program to check whether robot is moving inside a bounded box or not in Python

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

290 Views

Suppose we have a string s, that represents the moves of a robot. Robot is currently at (0, 0) position and it is facing north. The move string s may have these characters"F" to move forward direction, one unit"L" to rotate 90 degrees’ left"R" to rotate 90 degrees’ rightSo if the robot repeatedly makes the moves in s in order, we have to check whether there is some box in the plane in which the robot never leaves or not.So, if the input is like s = "FFRFRFFRF", then the output will be True, because robot moves 2 units towards ... Read More

Program to find bitwise AND of range of numbers in given range in Python

Arnab Chakraborty
Updated on 16-Oct-2021 10:07:42

290 Views

Suppose we have two values start and end, we have to find the bitwise AND of all numbers in the range [start, end] (both inclusive).So, if the input is like start = 8 end = 12, then the output will be 8 is 1000 in binary and 12 is 1100 in binary, so 1000 AND 1001 AND 1010 AND 1011 AND 1100 is 1000 which is 8.To solve this, we will follow these steps −n := end - start + 1x := 0for b in range 31 to 0, decrease by 1, doif 2^b < n, thencome out from loopif ... Read More

Program to find number of sublists with sum k in a binary list in Python

Arnab Chakraborty
Updated on 16-Oct-2021 10:05:13

208 Views

Suppose we have a binary list with 0s or 1s. We also have another input called k, we have to find the number of sublists whose sum is same as k.So, if the input is like nums = [1, 0, 0, 1, 1, 1, 0, 1] k = 3, then the output will be 8 because the sublists are [1, 0, 0, 1, 1], [0, 0, 1, 1, 1], [0, 0, 1, 1, 1, 0], [0, 1, 1, 1], [0, 1, 1, 1, 0], [1, 1, 1], [1, 1, 1, 0] [1, 1, 0, 1].To solve this, we will follow ... Read More

Program to find list showing total distance required to move all balls to current position in Python

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

130 Views

Suppose we have a binary list called nums containing only 0s and 1s where a 0 indicates empty cell and 1 indicates the cell is filled by a ball. We have to find a new list of say L, whose size is also same like nums size, where L[i] is set to the total distance required to move all the balls to L[i]. Here the distance to move a ball from index j to index i is |j - i|.So, if the input is like nums = [1, 1, 0, 1], then the output will be [4, 3, 4, 5], ... Read More

Program to balance the direction string so that each direction occurred quarter times in Python

Arnab Chakraborty
Updated on 16-Oct-2021 09:59:26

450 Views

Suppose we have a string s with four directions "N", "S", "W" and "E" for North, South, West and East respectively. We have to find the size of the shortest substring we can update such that each of the four directions occur n/4 times each, where n is the size of string s.So, if the input is like s = "NNSWWESN", then the output will be 1, here n is 8, so 8/4 is 2, so if we change last N to E, all directions will be there twice.To solve this, we will follow these steps −n := size of ... Read More

Program to check how many queries finds valid arithmetic sequence in Python

Arnab Chakraborty
Updated on 16-Oct-2021 09:55:51

262 Views

Suppose we have a list of numbers called nums and also have list of queries. Where each query element contains [i, j]. So this query is asking whether the sublist of nums from [i, j] (both inclusive), is an arithmetic sequence or not. So finally we have to find the count of queries that return true.So, if the input is like nums = [2, 4, 6, 8, 7, 6, 5, 2] queries = [[3, 4], [0, 3], [2, 4]], then the output will be 2, because [2, 4, 6, 8] is an arithmetic sequence, so query [0, 3] is true. ... Read More

Program to find maximum number of people we can make happy in Python

Arnab Chakraborty
Updated on 14-Oct-2021 12:50:52

566 Views

Suppose we have a list customers and another list mood, these two are of same length, we also have another integer k. Now on each minute i, customers[i] number of people come to the store and when mood[i] = 1, it indicates the customers are happy and when mood[i] = 0, then they are sad. We can set a sublist of size k of mood to 1s, finally we have to find the maximum number of people we can make happy.So, if the input is like customers = [2, 3, 6, 6, 3] mood = [1, 1, 0, 0, 0] ... Read More

Program to check string is palindrome or not with equivalent pairs in Python

Arnab Chakraborty
Updated on 14-Oct-2021 12:45:51

185 Views

Suppose we have a lowercase alphabet string called s and also have a list of pairs called 'pairs'. Each element in pairs has two strings [a, b] where the character 'a' and 'b' are considered same. If there are two pairs like [a, b] and [b, c], then we can say a and b are equivalent also b and c are equivalent, so a and c are also equivalent. And any value a or b is equivalent to itself. We have to check whether s is a palindrome or not with the given equivalence relations.So, if the input is like ... Read More

Program to check whether there is any pair of words which are almost same in Python

Arnab Chakraborty
Updated on 14-Oct-2021 12:38:43

476 Views

Suppose we have a list of lowercase strings called words where each word is of same length. We have to check whether there are two strings that differ only in one character.So, if the input is like words = ["seed", "pick", "lick", "root", "live"], then the output will be True, as "pick" and "lick" are almost same.To solve this, we will follow these steps −s := a new setfor each word in words, dofor each index i and word w in word, doif substring of word[from index 0 to i - 1] concatenate "*" concatenate word[from index i + 1 ... Read More

Program to find probability of getting assigned seat for the last person in an airplane after seat shuffling in Python

Arnab Chakraborty
Updated on 14-Oct-2021 12:34:31

215 Views

Suppose we have an integer n, that is representing the number of seats in an airplane. Now consider the first passenger has lost his ticket, so he picks a random seat. Now everyone else has their ticket but if their seat is already taken, they will also select an available seat randomly. We have to find the probability that the last person gets their assigned seat.So, if the input is like n = 5, then the output will be 0.5, the answer is always constant when there is more than one person, because either they have got the correct seat ... Read More

Advertisements