Found 10784 Articles for Python

Parsing A Boolean Expression in Python

Arnab Chakraborty
Updated on 04-Jun-2020 11:18:09

928 Views

Suppose we have a boolean expression, we have to find the result after evaluating that expression.An expression can either be −"t", evaluating to True;"f", evaluating to False;"!(expression)", evaluating to the logical NOT of the inner expression;"&(expr1, expr2, ...)", evaluating to the logical AND of 2 or more inner expressions;"|(expr1, expr2, ...)", evaluating to the logical OR of 2 or more inner expressions;So, if the input is like "|(!(t), &(t, f, t))", then the output will be fasle, this is because !(t) is false, then &(t, f, t) is also false, so the OR of all false values will be false.To ... Read More

Convert two lists into a dictionary in Python

Pradeep Elance
Updated on 04-Jun-2020 11:15:54

4K+ Views

While a Python list contains a series of values a dictionary on the other hand contains a pair of values which are called key-value pairs. In this article we will take two lists and mark them together to create a Python dictionary.With for and removeWe create two nested for loops. In the inner loop will assign one of the list as key for the dictionary while keep removing the values from the list which is outer for loop.Example Live DemolistK = ["Mon", "Tue", "Wed"] listV = [3, 6, 5] # Given lists print("List of K : ", listK) print("list of V ... Read More

Convert string to DateTime and vice-versa in Python

Pradeep Elance
Updated on 04-Jun-2020 11:13:41

196 Views

Python has extensive date and time manipulation capabilities.In this article we'll see in how is string with proper format can we converted to a datetime and the vice versa.With strptimeThis strptime function from datetime module can do the conversion from string to datetime by taking appropriate format specifiers.Example Live Demoimport datetime dt_str = 'September 19 2019 21:02:23 PM' #Given date time print("Given date time: ", dt_str) #Type check print("Data Type: ", type(dt_str)) #Format dtformat = '%B %d %Y %H:%M:%S %p' datetime_val = datetime.datetime.strptime(dt_str, dtformat) print("After converting to date time: ", datetime_val) #Type check print("Data type: ", type(datetime_val)) # Reverting to string ... Read More

Convert string enclosed list to list in Python

Pradeep Elance
Updated on 04-Jun-2020 11:11:47

517 Views

We may sometime get data which contains strings but the structure of the data inside the stream is a Python list. In this article we will convert string enclosed list to an actual Python list which can be further used in data manipulation.With evalWe know the eval function will give us the actual result which is supplied to it as parameter. So so we supplied the given string to the eval function and get back the Python list.Example Live DemostringA = "['Mon', 2, 'Tue', 4, 'Wed', 3]" # Given string print("Given string : ", stringA) # Type check print(type(stringA)) # using ... Read More

Escape a Large Maze Python

Arnab Chakraborty
Updated on 04-Jun-2020 11:00:48

323 Views

Suppose we have a grid, there are 1 million rows and 1 million columns, we also have one list of blocked cells. Now we will start at the source square and want to reach the target square. In each move, we can walk to a up, down, left, right adjacent square in the grid that isn't in the given list of blocked cells.We have to check whether it is possible to reach the target square through a sequence of moves or not.So, if the input is like blocked = [[0, 1], [1, 0]], source = [0, 0], target = [0, ... Read More

Grid Illumination in Python

Arnab Chakraborty
Updated on 04-Jun-2020 10:49:36

337 Views

Suppose we have a N x N grid of cells, in each cell (x, y) there is a lamp. Initially, some of the lamps are on. The lamps[i] is the location of the i-th lamp that is on. Each lamp that is on glows every square on its x-axis, y-axis, and both diagonals. Now for the i-th query i.e. queries[i] = (x, y), the answer to the query is 1 if the cell (x, y) is glowed, otherwise 0. After each query (x, y), we turn off any lamps that are at cell (x, y) or are adjacent 8-directionally. Return ... Read More

Smallest Good Base in Python

Arnab Chakraborty
Updated on 01-Jun-2020 11:23:44

226 Views

Suppose we have an integer n, we call k >= 2 as a good base of n, when all digits of n base k are 1. So if the number n is given as string, we have to return smallest good base of n as string. So if the number is say 121, then the answer will be 3, as 121 in base 3 is 11111.To solve this, we will follow these steps −Define a method called getSum(), this will take x and lengthset mainSum := 0 and temp := 1for i in range 0 to length – 1 −mainSum ... Read More

Strong Password Checker in Python

Arnab Chakraborty
Updated on 01-Jun-2020 10:53:31

5K+ Views

Suppose we have a string, password. We have to find out minimum changes required to make the password strong. So the password has some following criteria −It must be at least 6 character long and at most 20-character longIt must contain at least one lowercase letter, at least one uppercase letter, and at least one numeric character.It must not contain three repeating characters in a row like …aaa…, …PPP…, …888….So if the input is like "aa26bbb", so we need at least one change, as there is no uppercase letter, and there is three b’s in a row, so we can ... Read More

Word Search II in Python

Arnab Chakraborty
Updated on 26-May-2020 14:03:03

511 Views

Suppose we have a 2D board and a list of words. So from the dictionary, we have to find all words in the board. Here each word must be constructed from letters of sequentially adjacent cell, where the adjacent cells are those horizontally or vertically neighboring. We have to keep in mind that the same letter cell may not be used more than once in a word.So if the input is like −To solve this, we will follow these steps −make an array resultDefine a method called solve(), this will take board, d, i, j swhen either i or j ... Read More

Binary Tree Postorder Traversal in Python

Arnab Chakraborty
Updated on 26-May-2020 13:40:45

868 Views

Suppose we have a binary tree. We have to find the post order traversal of this tree using the iterative approach. So if the tree is like −Then the output will be: [9, 15, 7, 10, -10]To solve this, we will follow these steps −if root is null, then return empty arraycreate an array retstack := define a stack with pair [root, 0]while stack is not empty −node := top of stack, then delete element from stack.if second value of node pair is 0, thencurrent := first value of node pairinsert a pair (current, 1) into stackif right of current ... Read More

Advertisements