Found 10784 Articles for Python

Minimum Add to Make Parentheses Valid in Python

Arnab Chakraborty
Updated on 30-Apr-2020 07:38:19

314 Views

Suppose we have a string S of '(' and ')' parentheses, we add the minimum number of parentheses at any positions, so that the resulting parentheses string is valid. A parentheses string is valid if and only if −It is the empty stringIt can be written as XY (X concatenated with Y), where X and Y are valid stringsIt can be written as (A), where A is a valid string.So if the string is like "()))((", then we need to add 4 more parentheses to make the string valid.To solve this, we will follow these steps −if S is empty, ... Read More

Find and Replace Pattern in Python

Arnab Chakraborty
Updated on 30-Apr-2020 07:29:54

132 Views

Suppose we have a list of words and a pattern, and we have to find which words in words matches the pattern. Here a word matches the pattern if there exists a permutation of letters p so that after replacing every letter x in the pattern with p(x), we get the target word. We have to find a list of the words in words that match the given pattern.So for example, if the input is like ["abc", "deq", "mee", "aqq", "dkd", "ccc"] and pattern is “abb”, then the output will be [“mee”, “aqq”], here mee and aqq are matching the ... Read More

Construct Binary Tree from Preorder and Postorder Traversal in Python

Arnab Chakraborty
Updated on 30-Apr-2020 06:14:19

387 Views

Suppose we have two traversal sequences Preorder and Postorder, we have to generate the binary tree from these two sequences. So if the sequences are [1, 2, 4, 5, 3, 6, 7], [4, 5, 2, 6, 7, 3, 1], then the output will beTo solve this, we will follow these steps −ans := make a tree node by taking value pre[0], stack := empty stack, and insert ansi := 1 and j := 0while i < length of pre and j < length of postif stack top value = post[j], then increase j by 1, pop from stack, and go ... Read More

Decoded String at Index in Python

Arnab Chakraborty
Updated on 30-Apr-2020 06:09:03

137 Views

Suppose one encoded string S is given. We have to find and write the decoded string to a tape, here the encoded string is read one character at a time and the following steps are performed −If the character read is a letter, that letter is simply written onto the tape.If the character read is a digit, the entire current tape is repeatedly written digit – 1 more times in total.Now if some encoded string S, and an index K is given, find and return the K-th letter (starting indices from 1) in the decoded string.So if the string is ... Read More

Kth Smallest Element in a Sorted Matrix in Python

Arnab Chakraborty
Updated on 30-Apr-2020 05:54:09

315 Views

Suppose we have a n x n matrix where each of the rows and columns are sorted in increasing order, we have to find the kth smallest element in the matrix. Note that it is the kth smallest element in the sorted order, not the kth unique element. So if the input is like [[1, 5, 9], [10, 11, 13], [12, 13, 15]], if k = 8, then the output will be 13.To solve this, we will follow these steps −define one method called checkVal() and the arguments are matrix and valuei := 0, j := length of matrix[0] – ... Read More

Surrounded Regions in Python

Arnab Chakraborty
Updated on 04-May-2020 06:35:05

208 Views

Suppose we have a 2D board containing X and O. Capture all regions surrounded by X. A region is captured by changing all Os into Xs in that surrounded region.XXXXXOOXXXOXXOXXAfter running the output will beXXXXXXXXXXXXXOXXTo solve this, we will follow these steps −If board is not present, then return blank boardfor i in range 0 to number of rows – 1 −if board[i, 0] = ‘O’, then make_one(board, i, 0)if board[i, length of row - 1] = ‘O’, then make_one(board, i, length of row – 1)for i in range 0 to number of cols – 1 −if board[0, i] = ... Read More

Search in Rotated Sorted Array II in Python

Arnab Chakraborty
Updated on 04-May-2020 06:19:34

240 Views

Consider we have an array sorted in ascending order. That is rotated at some pivot unknown to us beforehand. For example, if the array is like [0, 0, 1, 2, 2, 5, 6], this might become [2, 5, 6, 0, 0, 1, 2]. We have a target value to search. If that is found in the array, then return true, otherwise return false. So if the array is like [2, 5, 6, 0, 0, 1, 2], and target is 0, then the output will be 0Let us see the steps −low := 0 and high := size of arraywhile low ... Read More

Minimum Path Sum in Python

Arnab Chakraborty
Updated on 04-May-2020 06:07:35

606 Views

Suppose we have a m x n matrix filled with non-negative integers, find a path from top left corner to bottom right corner which minimizes the sum of all numbers along its path. Movements can only be either down or right at any point in time. So for example, if the matrix is like below131151421The output will be 7, the path will be 1, 3, 1, 1, 1, this will minimize the sumLet us see the steps −a := number of rows, b := number of columnsi := a – 1, j := b – 1while j >= 0matrix[a, j] ... Read More

Calculating Wind Chill Factor(WCF) or Wind Chill Index(WCI) in Python Program

Hafeezul Kareem
Updated on 24-Apr-2020 12:40:14

349 Views

In this tutorial, we are going to learn how to calculate Wind Chill Index in Python. We have the formula to calculate the WCI and it's straightforward. We are going to use the following formula to calculate the WCI.Twc(WCI) = 13.12 + 0.6215Ta – 11.37v+0.16 + 0.3965Tav+0.16whereTwc = Wind Chill Index (Based on Celsius temperature scale)Ta = Air Temperature (in degree Celsius)v = Wind Speed (in miles per hour)We are going to use the math module function wherever we need them. Using the math module function decreases the execution time of a program.Follow the below steps to complete the program.Import the math moduleInitialize the ... Read More

Different Methods to find Prime Number in Python Program

Hafeezul Kareem
Updated on 24-Apr-2020 12:39:11

1K+ Views

In this tutorial, we are going to explore different methods to find whether a given number is valid or not. Let's start without further due.Method-1It's a general method to find prime numbers.If the number is less than or equal to one, return False.If the number is divisible by any number, then the function will return False.After the loop, return True.Example Live Demo# checking for prime def is_prime(n):    if n

Advertisements