Found 10784 Articles for Python

Powerful Integers in Python

Arnab Chakraborty
Updated on 06-Jul-2020 09:17:24

182 Views

Suppose we have two positive integers x and y, we can say an integer is powerful if it is equal to x^i + y^j for some integers i >= 0 and j >= 0. We have to find a list of all-powerful integers that have value less than or equal to bound.So, if the input is like x = 2 and y = 3 and the bound is 10, then the output will be [2, 3, 4, 5, 7, 9, 10], as 2 = 2^0 + 3^0 3 = 2^1 + 3^0 4 = 2^0 + 3^1 5 = 2^1 ... Read More

Delete Columns to Make Sorted in Python

Arnab Chakraborty
Updated on 06-Jul-2020 09:13:15

198 Views

Suppose we have an array of N lowercase letter strings, the array name is A, all strings are of same length. Now, we can choose any set of deletion indices, and for each string, we delete all the characters in those indices.As an example, if we have an array A like ["abcdef", "uvwxyz"] and deletion indices are {0, 2, 3}, then the final array after deletions will be ["bef", "vyz"], and the remaining columns of A are ["b", "v"], ["e", "y"], and ["f", "z"].Suppose we chose a set of deletion indices D like after deletions, each remaining column in A ... Read More

DI String Match in Python

Arnab Chakraborty
Updated on 06-Jul-2020 09:12:21

204 Views

Suppose we have a string S that only contains "I" (to denote increase) or "D" (to denote decrease), let N = size of S. We have to return any permutation A of [0, 1, ..., N] such that for all i in range 0, ..., N-1 −If S[i] is "I", then A[i] < A[i+1]Otherwise when S[i] is "D", then A[i] > A[i+1]So, if the input is like "IDID", then the output will be [0, 4, 1, 3, 2]To solve this, we will follow these steps −A := a list from 0 to N where N is the size of S.res ... Read More

Valid Mountain Array in Python

Arnab Chakraborty
Updated on 06-Jul-2020 09:10:41

766 Views

Suppose we have an array A of integers; we have to check whether it is a valid mountain array or not. We know that A is a mountain array if and only if it satisfies the following situations − size of A >= 3There exists some index i in A such that −A[0] < A[1] < ... A[i-1] < A[i]A[i] > A[i+1] > ... > A[A.length - 1]So, if the input is like [0, 3, 2, 1], then the output will be True.To solve this, we will follow these steps −if size of A < 3, thenreturn Falsei := 1while ... Read More

Reorder Data in Log Files in Python

Arnab Chakraborty
Updated on 06-Jul-2020 09:10:04

319 Views

Suppose we have an array of logs. In that array each entry is a space delimited string of words. The first word in each log is an alphanumeric identifier. Then, there are different types of strings like below −Each word after the id will consist only of lowercase letters;Each word after the id will consist only of digits.We will call these two types of logs as letter-logs and digit-logs respectively. And ti is guaranteed that each log has at least one word after its id.We have to reorder the logs so that all of the letter-logs stays before any digit-log. ... Read More

Number of Recent Calls in Python

Arnab Chakraborty
Updated on 04-Jul-2020 10:27:37

282 Views

Suppose we want to write a class called RecentCounter to count recent requests. This class has only one method: ping(t), where t is representing some time in milliseconds. This will return the number of pings that have been made from 3000 milliseconds ago until now. Any ping with time in [t - 3000, t] will count, including the current ping. And it is guaranteed that every call to ping uses a strictly larger value of t than before.So, if the input is like Call ping four times ping(1), ping(100), ping(3001), ping(3002), then the output will be 1, 2, 3, 3 ... Read More

Smallest Range I in Python

Arnab Chakraborty
Updated on 04-Jul-2020 10:24:36

336 Views

Suppose we have an array A of integers, now for each integer A[i] we can choose any x with range [-K to K] then add x to A[i]. Now after this process, we have some array B. We have to find the smallest possible difference between the maximum value of B and the minimum value of B.So, if the input is like A = [0,10], K = 2, then the output will be 6, as B = [2,8]To solve this, we will follow these steps −MAX := (maximum of A) - KMIN := (minimum of A) + Kdifference := MAX - MINif difference

Groups of Special-Equivalent Strings in Python

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

225 Views

Suppose we have an array of strings called A. One move onto S consists of swapping any two even indexed characters of S, or any two odd indexed characters of S.Now, two strings S and T are special-equivalent if after any number of moves onto S, S is same as T. So, if S = "zzxy" and T = "xyzz" are special-equivalent because we may make the moves like "zzxy" to "xzzy" to "xyzz" that swap S[0] and S[2], then S[1] and S[3].Now, a group of special-equivalent strings from A is a non-empty subset of A such that −In every ... Read More

Projection Area of 3D Shapes

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

401 Views

Suppose there is a N x N grid, we place some 1 x 1 x 1 cubes that are axis-aligned with the x, y, and z. Here each value v = grid[i][j] is showing a tower of v cubes placed on top of grid cell (i, j). We view the projection of these cubes onto the xy, yz, and zx planes. Here, we are viewing the projection when looking at the cubes from the top, the front, and the side view. We have to find the total area of all three projections.So, if the input is like [[1, 2], [3, ... Read More

Walking Robot Simulation in Python

Arnab Chakraborty
Updated on 04-Jul-2020 10:19:23

752 Views

Suppose there is a robot on an infinite grid that starts at point (0, 0). It is facing towards north direction. Now the robot can receive one of three possible types of commands −-2 to turn left 90 degrees-1 to turn right 90 degreesany value from 1 to 9 to move forward x unitsThere are some grid squares that are obstacles.We also have another array called obstacle, this indicates the i-th obstacle is at grid point (obstacles[i][0], obstacles[i][1]), If the robot wants to move onto them, the robot stays on the previous grid square instead.We have to find the square ... Read More

Advertisements