Found 10476 Articles for Python

Program to evaluate s-expression as string in Python

Arnab Chakraborty
Updated on 22-Dec-2020 08:47:31

533 Views

Suppose we have a string s as S-expression. We have to evaluate that S-expression and return result as integer. As we know that the s-expression is an expression which is either one number, or a recursive expression wrapped in parentheses like (+ (- 3 2) (* 3 3)), which indicates (3 - 2) + (3 * 3) = 10. Here valid operators are +, -, *, and /.So, if the input is like s = "(- (+ 3 2) 2)", then the output will be 3, as ((3 + 2) - 2) = 3.To solve this, we will follow these ... Read More

Program to check regular expression pattern is matching with string or not in Python

Arnab Chakraborty
Updated on 22-Dec-2020 08:41:32

571 Views

Suppose we have a string s and a regular expression pattern. We have to check whether the given pattern matches with given string or not. In the regular expression, there are few rules −. (period) which matches any single character* (asterisk) which matches zero or more of the preceding element.So, if the input is like pattern = "h.l*o" s = "hello", then the output will be True, as We have ra and then a single characterTo solve this, we will follow these steps −n := size of sm := size of pDefine a function dp() . This will take i, ... Read More

Program to count number of unique paths that includes given edges in Python

Arnab Chakraborty
Updated on 22-Dec-2020 08:36:38

355 Views

Suppose we have a list of edges in the form (u, v) and these are representing a tree. For each edge we have to find the total number of unique paths that includes said edge, in the same order as given in the input.So, if the input is like edges = [[0, 1], [0, 2], [1, 3], [1, 4]]then the output will be [6, 4, 4, 4].To solve this, we will follow these steps −adj := adjacency list from given edgescount := an empty mapDefine a function dfs() . This will take x, parentcount[x] := 1for each nb in adj[x], ... Read More

Program to count number of points that lie on a line in Python

Arnab Chakraborty
Updated on 22-Dec-2020 08:35:06

683 Views

Suppose we have a list of coordinates. Each coordinate has two values x and y, representing a point on the Cartesian plane. Now find the maximum number of points that lie on some line.So, if the input is like coordinates = [[6, 2], [8, 3], [10, 4], [1, 1], [2, 2], [6, 6], [7, 7]], then the output will be 4, as the points are [1, 1], [2, 2], [6, 6], [7, 7]] that lies on a line.To solve this, we will follow these steps −res := 0for i in range 0 to size of points list, do(x1, y1) := ... Read More

Program to find minimum cost to pick up gold in given two locations in Python

Arnab Chakraborty
Updated on 22-Dec-2020 08:29:11

152 Views

Suppose we have a 2d matrix and some other values like row, col, erow0, ecol0, erow1, and ecol1. If our current position is matrix [row, col] and we want to pick up gold that is at matrix [erow0, ecol0] and matrix [erow1, ecol1]. We can go up, down, left, and right but when we are at a cell (r, c), we have to pay cost matrix [r, c], although if we land at a cell more than once, we do not need to pay cost for that cell again. We have to find the minimum cost to pick up gold ... Read More

Program to find size of each partition of a list where each letter appears at most one piece in Python

Arnab Chakraborty
Updated on 22-Dec-2020 08:24:27

216 Views

Suppose we have a lowercase string s, we can partition s into as many pieces as possible such that each letter appears in at most one piece and find the sizes of the partitions as a list.So, if the input is like s = "momoplaykae", then the output will be [4, 1, 1, 4, 1], as the string is split into ["momo", "p", "l", "ayka", "e"].To solve this, we will follow these steps −count := a map that contains characters in s and their occurrencesout := a new list, stk := an empty stacklength := 0for each char in s, ... Read More

Program to count number of paths with cost k from start to end point in Python

Arnab Chakraborty
Updated on 22-Dec-2020 08:22:21

309 Views

Suppose we have a 2d binary matrix and another value k. Now starting from the top-left cell, we have to go to the bottom right cell. In one step, we can only go down, or right. Now the score of a path is the sum of the values on the cells on the path. We have to find the number of paths from the start cell to end cell with score k. If there are huge possible ways then return result mod 10^9+7.So, if the input is like001101010K = 2, then the output will be 4, as the paths with ... Read More

Program to find minimum cost to paint fences with k different colors in Python

Arnab Chakraborty
Updated on 22-Dec-2020 08:20:48

307 Views

Suppose we want to paint a row of N fences with K different colors. We want to minimize the cost while ensuring that no two neighboring fences are of the same color. So if we have an N x K matrix where the nth row and kth column represents the cost to paint the nth fence with kth color, we have to find the minimum cost which achieves this goal.So, if the input is like645327345544then the output will be 14, as we can select the following color indices (starting from the first fence) − 5 → 2 → 3 → ... Read More

Program to get maximum value of power of a list by rearranging elements in Python

Arnab Chakraborty
Updated on 22-Dec-2020 06:44:36

658 Views

Suppose we have a list nums of N positive numbers. Now we can select any single value from the list, and move (not swap) it to any position. We can also not move any to position at all. So we have to find what is the maximum possible final power of the list? As we know the power of a list is the sum of (index + 1) * value_at_index over all indices i.$$\displaystyle\sum\limits_{i=0}^{n-1} (i+1)\times list[i]$$So, if the input is like nums = [6, 2, 3], then the output will be 26, as we can move the 6 to the ... Read More

Program to find length of longest increasing subsequence with at least k odd values in Python

Arnab Chakraborty
Updated on 22-Dec-2020 06:40:36

246 Views

Suppose we have a list of numbers called nums and another value k, we have to find the size of the longest increasing subsequence with at least k odd elements.So, if the input is like nums = [12, 14, 16, 5, 7, 8] k = 2, then the output will be 3, as the longest increasing subsequence with at least 2 odd values is [5, 7, 8].To solve this, we will follow these steps −best := 0Define a function dp() . This will take i, j, odd, takenif odd >= k, thenbest := maximum of best and takenif j is ... Read More

Advertisements