Found 10784 Articles for Python

Binary Gap in Python

Arnab Chakraborty
Updated on 04-Jul-2020 10:17:08

2K+ Views

Suppose we have a positive integer N, we have to find the longest distance between two consecutive 1's in the binary representation of N. If there are no two-consecutive 1's, then return 0.So, if the input is like 22, then the output will be 2, as 22 in binary is 10110. There are three ones In the binary representation of 22, and two consecutive pairs of 1's. The first consecutive pair of 1's have distance 2, then the second consecutive pair of 1's have distance 1. Answer will be the largest of these two distances, which is 2.To solve this, ... Read More

Lemonade Change in Python

Arnab Chakraborty
Updated on 04-Jul-2020 10:16:30

608 Views

Suppose there is a lemonade stand, each lemonade costs $5. Now customers are standing in a queue to buy from the store, and order one at a time.Each customer can buy only one lemonade and pay with either a $5, $10, or $20 bill. We have to must provide the correct change to each customer, so that the net transaction is that the customer pays $5. And at first, we have no change in hand.We have to check whether we can provide every customer with correct change.So, if the input is like [5, 5, 5, 10, 20], then the output ... Read More

Buddy Strings in Python

Arnab Chakraborty
Updated on 04-Jul-2020 10:13:42

541 Views

Suppose we have two strings A and B of lowercase letters; we have to check whether we can swap two letters in A so that the result equals to B or not.So, if the input is like A = "ba", B = "ab", then the output will be True.To solve this, we will follow these steps −if size of A is not same as size of B, thenreturn Falseotherwise when A and B has any element that are not common, thenreturn Falseotherwise when A is same as B and all characters are distinct in A, thenreturn Falseotherwise, count:= 0for i ... Read More

Rectangle Overlap in Python

Arnab Chakraborty
Updated on 04-Jul-2020 10:10:13

4K+ Views

Suppose there is a rectangle that is represented as a list [x1, y1, x2, y2], where (x1, y1) is the coordinates of its bottom-left corner, and (x2, y2) is the coordinates of its top-right corner. Now two rectangles overlap if the area of their intersection is positive. So, we can understand that two rectangles that only touch at the corner or edges do not overlap.If we have two (axis-aligned) rectangles, we have to check whether they overlap or not.So, if the input is like R1 = [0, 0, 2, 2], R2 = [1, 1, 3, 3], then the output will ... Read More

Flipping an Image in Python

Arnab Chakraborty
Updated on 04-Jul-2020 10:08:07

454 Views

Suppose we have a binary matrix A, this is the representation of an image, we want to flip the image horizontally, after that invert it, and finally return the resulting image. To flip the image horizontally each row of the image will be reversed. And to invert the image each 0 will be replaced by 1, and each 1 will be replaced by 0.So, if the input is like110101000then the output will be100010111To solve this, we will follow these steps −result:= a new listfor each row i in A, doReverse:= reverse row ifor j in range 0 to size of ... Read More

Positions of Large Groups in Python

Arnab Chakraborty
Updated on 04-Jul-2020 10:07:04

139 Views

Suppose there is a string S of lowercase letters, these letters form consecutive groups of the same character. So, when a string like S is like "abbxxxxzyy" has the groups "a", "bb", "xxxx", "z" and "yy". A group will be a large group when it has 3 or more characters. We would like the starting and ending positions of every large group.So, if the input is like "abcdddeeeeaabbbcd", then the output will be [[3, 5], [6, 9], [12, 14]]To solve this, we will follow these steps −ans := a new listcsum := 0for each a, b in make group of ... Read More

Largest Triangle Area in Python

Arnab Chakraborty
Updated on 04-Jul-2020 10:05:57

535 Views

Suppose we have a list of points on a plane. We have to find the area of the largest triangle that can be formed by any 3 of the points.So, if the input is like [[0, 0], [0, 1], [1, 0], [0, 2], [2, 0]], then the output will be 2To solve this, we will follow these steps −res := 0N := size of points listfor i in range 0 to N - 2, dofor j in range i + 1 to N - 1, dofor k in range i + 2 to N, do(x1, y1) := points[i], (x2, y2) ... Read More

Number of Lines To Write String in Python

Arnab Chakraborty
Updated on 04-Jul-2020 10:04:30

372 Views

Suppose we have a string S and we have to write the letters of that given string, from left to right into lines. Here each line has maximum width 100 units, and if writing a letter would cause the width of the line to exceed 100 units, that will be written on the next line. We also have an array widths, here widths[0] is the width of 'a', widths[1] is the width of 'b'and so on.We have to find the answers of two questions −How many lines have at least one character from SWhat is the width used by the ... Read More

Unique Morse Code Words in Python

Arnab Chakraborty
Updated on 04-Jul-2020 10:04:01

422 Views

Suppose we have a list of words, here each word can be written as a concatenation of the Morse code of each letter. For example, the word "cba" can be written as "-.-..--...", this is the concatenation "-.-." | "-..." | ".-"). This kind of concatenation is called the transformation of a word.We know that International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows: "a" maps to ".-", "b" maps to "-...", "c" maps to "-.-.", and so on.Here is the list of all 26 letters of the ... Read More

Prime Number of Set Bits in Binary Representation in Python

Arnab Chakraborty
Updated on 04-Jul-2020 10:02:34

214 Views

Suppose we have two integers L and R, we have to find the count of numbers in the range [L, R] (inclusive) having a prime number of set bits in their binary form.So, if the input is like L = 6 and R = 10, then the output will be 4, as there are 4 numbers 6(110), 7(111), 9(1001), 10(1010), all have prime number of set bits.To solve this, we will follow these steps −count := 0for j in range L to R, doif set bit count of j is in [2, 3, 5, 7, 11, 13, 17, 19], thencount ... Read More

Advertisements