Found 10784 Articles for Python

Jewels and Stones in Python

Arnab Chakraborty
Updated on 28-Apr-2020 16:46:58

547 Views

Suppose we have a string J that indicates some letters that are considered as Jewel, and another string S, that represents some stones that we have. Our task is to find how many of stones in S is also jewel. The letters in J and S are case sensitive. So if the J = “aZc”, and S = “catTableZebraPicnic” then there are 7 jewels.To solve this we will convert the string into a list of characters. If the character in J present in S, then increase the count.ExampleLet us see the following implementation to get better understanding − Live Democlass Solution(object): ... Read More

Array Partition I in Python

Sunidhi Bansal
Updated on 03-Nov-2022 06:30:54

2K+ Views

We are given the array, let's say arr[] of 2n integers. We have to make the pair group of the integer elements like(a1, b1), (a2, b2)....(an, bn) which makes the sum of min(ai, bi) for all elements in the array as large as possible. The task is to find the maximum sum of pairs For example arr[] = [1, 2, 3, 4] and output is 4 the maximum sum of pairs is 4. The all possible are − (1, 2) and (3, 4) -> min(1, 2) + min(3, 4) = 1 + 3 = 4. (1, 4) and (2, 3) ... Read More

Diameter of Binary Tree in Python

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

391 Views

Suppose we have a binary tree; we have to compute the length of the diameter of the tree. The diameter of a binary tree is actually the length of the longest path between any two nodes in a tree. This path not necessarily pass through the root. So if the tree is like below, then the diameter will be 3.as the length of the path [4, 2, 1, 3] or [5, 2, 1, 3] is 3To solve this, we will follow these steps −We will use the dfs to find the diameter, set answer := 0call the dfs function with ... Read More

Intersection of Two Arrays II in Python

Arnab Chakraborty
Updated on 28-Apr-2020 16:31:34

4K+ Views

Suppose we have two arrays A and B, there are few elements in these array. We have to find the intersection of them. So if A = [1, 4, 5, 3, 6], and B = [2, 3, 5, 7, 9], then intersection will be [3, 5]To solve this, we will follow these steps −Take two arrays A and Bif length of A is smaller than length of B, then swap themcalculate the frequency of elements in the array and store them into mfor each element e in B, if e is present in m, and frequency is non-zero, decrease frequency ... Read More

Reverse Vowels of a String in Python

Arnab Chakraborty
Updated on 28-Apr-2020 16:27:41

4K+ Views

Suppose we have a lowercase string. Our task is to reverse the vowels present in the string. So if the string is “hello”, then the string after vowel reversal will be “holle”. For string “programming”, it will be “prigrammong”To solve this, we will follow these steps −Take the string and make a list of vowels, and store their indices as wellreverse the vowel listset idx := 0for i := 0 to length of given string – 1if i is in index list −put vowels[i] into final stringidx := idx + 1otherwise put string[i] into final stringreturn the list as a ... Read More

First Bad Version in Python

Arnab Chakraborty
Updated on 28-Apr-2020 16:22:26

855 Views

Suppose in a company, one product manager is leading a team who develops a new product. Suppose latest version fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version will be bad. So we have an array A with n elements [1, 2, … n] and we have to find the first bad version from this array.Consider we have a function isBadVersion(version_id), this will return whether the version is bad or not. For an example, suppose n = 5, and version = 4 is first bad version. So if ... Read More

Lowest Common Ancestor of a Binary Search Tree in Python

Arnab Chakraborty
Updated on 28-Apr-2020 16:19:10

531 Views

Suppose we have a binary search tree. we have to find the Lowest common ancestor nodes of two given nodes. The LCA of two nodes p and q is actually as the lowest node in tree that has both p and q as decedent. So if the binary tree is like [6, 2, 8, 0, 4, 7, 9, null, null, 3, 5]. The tree will be like −Here LCA of 2 and 8 is 6To solve this, we will follow these steps −If the tree is empty, then return nullif p and q both are same as root, then return ... Read More

Happy Number in Python

Arnab Chakraborty
Updated on 28-Apr-2020 16:16:36

2K+ Views

Here we will see how to detect a number n is one Happy number or not. So the happy number is a number, where starting with any positive integers replace the number by the sum of squares of its digits, this process will be repeated until it becomes 1, otherwise it will loop endlessly in a cycle. Those numbers, when the 1 has found, they will be happy number.Suppose the number is 19, the output will be true as the number is happy number. As we can see from 19, we will get12 + 92 = 8282 + 22 = ... Read More

Reverse Bits in C++

Arnab Chakraborty
Updated on 28-Apr-2020 16:14:13

5K+ Views

Suppose we have one unsigned number x, and we can easily find the binary representation of it (32bit unsigned integer). Our task is to reverse the bits. So if the binary representation is like 00000000000000000000001001110100, then reversed bits will be 00101110010000000000000000000000. So we have to return the actual number after reversing the bitsTo solve this, we will follow these steps −Suppose n is the given numberlet answer := 0for i := 31 down to 0:answer := answer OR (n AND i), and shift it to the left i timesn := n after right shifting 1 bitreturn answerExampleLet us see the ... Read More

Intersection of Two Linked Lists in Python

Arnab Chakraborty
Updated on 28-Apr-2020 16:11:47

870 Views

Suppose we have two linked lists A and B, there are few elements in these linked lists. We have to return the reference of the intersection points. The inputs are intersectionVal = 8, A = [4, 1, 8, 4, 5], B = [5, 0, 1, 8, 4, 5], skipA = 2 and skipB = 3, these are used to skip 2 elements from A and skip 3 elements from B.To solve this, we will follow these steps −Define a map called dwhile headA is not nulld[headA] := 1headA := next of headAwhile headB is not nullif headB in dreturn headBheadB ... Read More

Advertisements