Found 10784 Articles for Python

Valid Anagram in Python

Arnab Chakraborty
Updated on 28-Apr-2020 10:33:32

493 Views

Anagrams are basically all permutations of a given string or pattern. This pattern searching algorithm is slightly different. In this case, not only the exact pattern is searched, it searches all possible arrangements of the given pattern in the text. So if the inputs are “ANAGRAM” and “NAAGARM”, then they are anagram, but “cat” and “fat” are not an anagramTo solve this, we will convert the string into a list of characters, then sort them, if two sorted lists are same then they are anagram.Example (Python)Let us see the following implementation to get a better understanding − Live Democlass Solution(object):   ... Read More

Delete Node in a Linked List in Python

Arnab Chakraborty
Updated on 28-Apr-2020 10:31:08

3K+ Views

Suppose we have a linked list with few elements. Our task is to write a function that will delete the given node from the list. So if the list is like 1 → 3 → 5 → 7 → 9, and after deleting 3, it will be 1 → 5 → 7 → 9.Consider we have the pointer ‘node’ to point that node to be deleted, we have to perform these operations to delete the node −node.val = node.next.valnode.next = node.next.nextExample (Python)Let us see the following implementation to get a better understanding − Live Democlass ListNode:    def __init__(self, data, next ... Read More

Invert Binary Tree in Python

Arnab Chakraborty
Updated on 28-Apr-2020 10:26:40

786 Views

Suppose we have a binary tree. our task is to create an inverted binary tree. So if the tree is like below −The inverted tree will be likeTo solve this, we will use a recursive approachif the root is null, then returnswap the left and right pointersrecursively solve left subtree and right subtreeExample (Python)Let us see the following implementation to get a better understanding − Live Democlass TreeNode:    def __init__(self, data, left = None, right = None):       self.data = data       self.left = left       self.right = right def make_tree(elements):    Tree = ... Read More

Contains Duplicate in Python

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

864 Views

Suppose we have a list of numbers. We have to check whether the list is holding some duplicate elements or not. So if the list is like [1, 5, 6, 2, 1, 3], then it will return 1 as there are two 1s, but if the list is [1, 2, 3, 4], then it will be false, as there is no duplicate present.To solve this, we will follow this approach −We know that the set data structure only holds unique data. But the list can fold duplicate contents. So if we convert the list into the set, its size will ... Read More

Reverse Linked List in Python

Arnab Chakraborty
Updated on 28-Apr-2020 09:57:13

341 Views

Suppose we have a linked list, we have to reverse it. So if the list is like 1 → 3 → 5 → 7, then the new reversed list will be 7 → 5 → 3 → 1To solve this, we will follow this approach −Define one procedure to perform list reversal in a recursive way as to solve(head, back)if the head is not present, then return headtemp := head.nexthead.next := backback = headif temp is empty, then return headhead = tempreturn solve(head, back)ExampleLet us see the following implementation to get a better understanding − Live Democlass ListNode:    def __init__(self, ... Read More

Count Primes in Python

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

4K+ Views

Suppose we have a limit n. We have to count the number of primes present in the range 2 to n. So if n = 10, the result will be 4. As there are four primes before 10, they are 2, 3, 5, 7.To solve this, we will follow this approach −count = 0take one array prime = of size n + 1, and fill it with Falsefor i = 0 to n, doif prime[i] = false, thenincrease count by 1set j = 2while j * i

House Robber in Python

Arnab Chakraborty
Updated on 28-Apr-2020 09:50:44

880 Views

Suppose there is a city, and each house in the city has a certain amount. One robber wants to rob the money in one single night. The city has one security system, that is as if two consecutive houses are broken on the same night, then it will automatically call the police. So we have to find how the maximum amount the robber can rob?One array is provided, at index i, the A[i] is the amount that is present in i-th house. Suppose the array is like: A = [2, 7, 10, 3, 1], then the result will be 13. ... Read More

Number of 1 Bits in Python

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

7K+ Views

Suppose we have an unsigned number n. We have to find the number of 1s in a binary representation of this number. This is also known as Hamming Weight. So if the number is like 000000101101, then the result will be 4.To solve this, we will use these steps −Take the number and convert it into a binary stringset count = 0for each character e in a binary stringif the character is ‘1’, then increase count by 1return countExampleLet us see the following implementation to get a better understanding − Live Democlass Solution(object):    def hammingWeight(self, n):       """ ... Read More

Rotate Array in Python

Arnab Chakraborty
Updated on 28-Apr-2020 09:45:51

587 Views

Suppose we have an array A. We have to rotate right it k steps. So if the array is A = [5, 7, 3, 6, 8, 1, 5, 4], and k = 3, then the output will be [1, 5, 4, 5, 7, 3, 6, 8]. The steps are like[4, 5, 7, 3, 6, 8, 1, 5][5, 4, 5, 7, 3, 6, 8, 1][1, 5, 4, 5, 7, 3, 6, 8]To solve this, we will follow these steps.let n is the size of the arrayk = k mod nA = subarray of A from n – k to end + ... Read More

Single Number in Python

Arnab Chakraborty
Updated on 28-Apr-2020 09:36:56

1K+ Views

Suppose we have an array A. In this array there are many numbers that occur twice. Only one element can be found a single time. We have to find that element from that array. Suppose A = [1, 1, 5, 3, 2, 5, 2], then the output will be 3. As there is each number twice, we can perform XOR to cancel out that element. because we know y XOR y = 0To solve this, we will follow these steps.Take one variable res = 0for each element e in array A, preform res = res XOR ereturn resExampleLet us see ... Read More

Advertisements