Found 10784 Articles for Python

Valid Palindrome in Python

Arnab Chakraborty
Updated on 28-Apr-2020 09:35:05

785 Views

Suppose we have a string with alphanumeric values and symbols. There are lower case and uppercase letters as well. We have to check whether the string is forming a palindrome or not by considering only the lowercase letters (uppercases will be converted into lower case), other symbols like a comma, space will be ignored.Suppose the string is like “A Man, a Plan, a Canal: Panama”, then by considering these rules, it will be “amanaplanacanalpanama”. This is a palindrome.To solve this, follow these steps −define x = “”read each character c in str −if c is lowercase letter or number, then ... Read More

Best Time to Buy and Sell Stock II in Python

Arnab Chakraborty
Updated on 28-Apr-2020 09:32:06

825 Views

Suppose we have an array A, here A[i] is indicating the price of a given stock on day i. We have to find the maximum profit. We can complete as many transactions as we like. (Transaction means to buy and sell stocks). But we have to keep in mind that we may not engage in multiple transactions at the same time. So we have to sell the stock before buying the new one.Suppose the array is like A = [7, 1, 5, 3, 6, 4], then the result will be 7. As we can see, if we buy on day ... Read More

Best Time to Buy and Sell Stock in Python

Arnab Chakraborty
Updated on 28-Apr-2020 09:29:33

374 Views

Suppose we have an array A, here A[i] is indicating the price of a given stock on day i. We have to find the maximum profit. We can complete at most one transaction. (Transaction means to buy and sell stocks). But we have to keep in mind that we may not engage in multiple transactions at the same time. So we have to sell the stock before buying the new one.Suppose the array is like A = [7, 1, 5, 3, 6, 4], then the result will be 5. As we can see, if we buy on day 2 (index ... Read More

Path Sum in Python

Arnab Chakraborty
Updated on 28-Apr-2020 09:24:19

525 Views

Suppose we have one tree and a sum. We have to find one path such that if we follow that path, we will get the sum that will be matched with the given sum. Suppose the tree is like [0, -3, 9, -10, null, 5] and the sum is 14, then there is a path 0 → 9 → 5To solve this, we will follow these steps.If the root is null, then return Falseif left and right subtree are empty, then return true when sum – root.val = 0, otherwise falsereturn solve(root.left, sum – root.val) or solve(root.right, sum – root.val)Let ... Read More

Convert Sorted Array to Binary Search Tree in Python

Arnab Chakraborty
Updated on 28-Apr-2020 09:18:12

2K+ Views

Suppose we have one sorted array A. We have to generate one height-balanced binary search. In this problem, a height-balanced binary tree is actually a binary tree in which the depth of the two subtrees of every node never differs by more than 1. Suppose the array is like [-10, -3, 0, 5, 9]. So one possible output will be like: [0, -3, 9, -10, null, 5]To solve this, we will follow these steps.If A is empty, then return Nullfind the mid element, and make it rootDivide the array into two sub-arrays, left part of the mid element, and right ... Read More

Maximum Depth of Binary Tree in Python

Arnab Chakraborty
Updated on 28-Apr-2020 09:10:53

2K+ Views

Suppose we have one binary tree. We have to find the maximum depth of that tree. The maximum depth of a tree is the maximum number of nodes that are traversed to reach the leaf from the root using the longest path. Suppose the tree is like below. The depth will be 3 here.To solve this, we will follow these steps.Here we will use the recursive approach. The method is solve(root, depth = 0)if the root is empty, then return depthotherwise return max of solve(left, depth + 1) and solve(left, depth + 1)Let us see the following implementation to get ... Read More

Symmetric Tree in Python

Arnab Chakraborty
Updated on 28-Apr-2020 16:05:44

530 Views

Suppose we have one binary tree. We have to check whether the tree is a symmetric tree or not. A tree will be said to be symmetric if it is the same when we take the mirror image of it. From these two trees, the first one is symmetric, but the second one is not.To solve this, we will follow these steps.We will call following steps recursively. The function will be solve(root, root)if the node1 and node2 are empty, then return trueif either node1 or node2 is empty, then return falsereturn true when node1.val = node2.val and solve(node1.left, node2.right) and ... Read More

Merge Sorted Array in Python

Arnab Chakraborty
Updated on 28-Apr-2020 16:05:12

2K+ Views

Suppose we have two sorted arrays A and B. We have to merge them and form only one sorted array C. The size of lists may different.For an example, suppose A = [1, 2, 4, 7] and B = [1, 3, 4, 5, 6, 8], then merged list C will be [1, 1, 2, 3, 4, 4, 5, 6, 7, 8]To solve this, follow these steps −define i := 0, j := 0 and end := length of A – 1while end >= 0 and not A[end], end := end – 1while j < length of Bif i > end ... Read More

Sqrt(x) in Python

Arnab Chakraborty
Updated on 28-Apr-2020 16:03:55

398 Views

Suppose we have a number x, and x is a non-negative number. We have to find the square root of x without using any library functions. So we have to create our own function to evaluate sqrt(x). In this function, the decimal digit of the output will be truncated.Suppose the value of x is 4, then the result will be 2 if the x is 8, then the result will be also 2, as sqrt(8) is 2.82842. But we will take only the integer part.To solve this, follow these steps −initialize l = 1, and h = x + 1, ... Read More

Plus One in Python

Arnab Chakraborty
Updated on 28-Apr-2020 16:02:33

2K+ Views

Suppose we have an array of integers, say A. A will hold n elements, and they are non-negative. The whole array A is representing one large number. So if A = [5, 3, 2, 4] is given, it indicates the number 5324. We have to take that array A, then increase the number by 1, and again return the number like an array as given. So after increasing A will be [5, 3, 2, 5]To solve this, we will follow these steps.Take the array and append each character into a string to make it stringthen convert the string to an ... Read More

Advertisements