Found 10784 Articles for Python

Min Stack in Python

Arnab Chakraborty
Updated on 28-Apr-2020 13:27:30

2K+ Views

Here we will see how to make a stack, that can perform push, pop, top, and retrieve the min element in constant time. So the functions will be push(x), pop(), top() and getMin()To solve this, we will follow these steps −Initialize the stack by min element as infinityFor push operation push(x)if x < min, then update min := x, push x into stackFor pop operation pop()t := top elementdelete t from stackif t is min, then min := top element of the stackFor top operation top()simply return the top elementfor getMin operation getMin()return the min elementExampleLet us see the following ... Read More

Linked List Cycle in Python

Arnab Chakraborty
Updated on 28-Apr-2020 13:22:38

2K+ Views

Consider we have a linked list, and we have to check whether there is any cycle or not. To represent the cycle in the given linked list, we will use one integer pointer called pos. This pos represents a position in the linked list where tail is connected. So if pos is -1, then there is no cycle present in the linked list. For example, the linked list is like [5, 3, 2, 0, -4, 7], and pos = 1. So there is a cycle, and tail is connected to the second node.To solve this, we will follow these steps ... Read More

Hamming Distance in Python

Arnab Chakraborty
Updated on 28-Apr-2020 11:05:39

3K+ Views

Consider we have two integers. We have to find the Hamming distance of them. The hamming distance is the number of bit different bit count between two numbers. So if the numbers are 7 and 15, they are 0111 and 1111 in binary, here the MSb is different, so the Hamming distance is 1.To solve this, we will follow these steps −For i = 31 down to 0b1 = right shift of x (i AND 1 time)b2 = right shift of y (i AND 1 time)if b1 = b2, then answer := answer + 0, otherwise answer := answer + ... Read More

Fizz Buzz in Python

Arnab Chakraborty
Updated on 28-Apr-2020 11:03:59

4K+ Views

Suppose we have a number n. We have to display a string representation of all numbers from 1 to n, but there are some constraints.If the number is divisible by 3, write Fizz instead of the numberIf the number is divisible by 5, write Buzz instead of the numberIf the number is divisible by 3 and 5 both, write FizzBuzz instead of the numberTo solve this, we will follow these steps −For all number from 1 to n, if a number is divisible by 3 and 5 both, print “FizzBuzz”otherwise when the number is divisible by 3, print “Fizz”otherwise when ... Read More

First Unique Character in a String in Python

Arnab Chakraborty
Updated on 28-Apr-2020 11:01:32

4K+ Views

Suppose we have a string and we have to find the first unique character in the string. So if the string is like “people”, the first letter whose occurrence is one is ‘o’. So the index will be returned, that is 2 here. If there is no such character, then return -1.To solve this, we will follow these steps −create one frequency mapfor each character c in the string, doif c is not in frequency, then insert it into frequency, and put value 1otherwise, increase the count in frequencyScan the frequency map, if the value of a specific key is ... Read More

Sum of Two Integers in Python

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

676 Views

Suppose we have two integers a and b. Our task is to find the sum of these two integers. One constraint is that, we cannot use any operator like + or -. So if a = 5 and b = 7, the result will be 12.To solve this, we will follow these steps −For solving we will use the bitwise logical operatorsIf b = 0, then return aotherwise, recursively use the sum function by providing an XOR b, and a AND b after left shifting the result one timeExample (Python)Let us see the following implementation to get a better understanding ... Read More

Reverse String in Python

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

1K+ Views

Suppose we have an array of characters. We have to reverse the string without using any additional space. So if the string is like [‘H’, ‘E’, ‘L’, ‘L’, ‘O’], the output will be [‘O’, ‘L’, ‘L’, ‘E’, ‘H’]To solve this, we will follow these steps −Take two pointers to start = 0 and end = length of the string – 1swap first and last charactersincrease start by 1 and decrease-end by 1ExampleLet us see the following implementation to get a better understanding − Live Democlass Solution(object):    def reverseString(self, s):       """       :type s: List[str]   ... Read More

Power of Three in Python

Arnab Chakraborty
Updated on 28-Apr-2020 10:52:30

1K+ Views

Suppose we have a number n. We have to check whether the number is the power of 3 or not. So if the number is like n = 27, that is the power of 3, the result will be true, if n = 15, it will be false.To solve this, we will follow these steps −We will use the Logarithm to solve thisif [log10(n) / log10(3)] mod 1 == 0, then it will be power of three, otherwise notExampleLet us see the following implementation to get a better understanding − Live Democlass Solution(object):    def isPowerOfThree(self, n):       """       :type n: int       :rtype: bool       """       if not n or n

Move Zeroes in Python

Arnab Chakraborty
Updated on 28-Apr-2020 10:46:18

868 Views

Suppose we have an array to hold some numbers. There are non-zero values as well as zero values. So we have to send all zeros to the right without changing the relative order of other numbers. So if the array is like [0, 1, 5, 0, 3, 8, 0, 0, 9], then the final array will be [1, 5, 3, 8, 9, 0, 0, 0, 0]To solve this, we will follow these steps −Suppose index = 0for i = 0 to the length of Aif A[i] != 0, thenA[index] := A[i]index := index + 1for i = index to the ... Read More

Missing Number in Python

Arnab Chakraborty
Updated on 28-Apr-2020 10:43:48

309 Views

Suppose we have a list of numbers from 0 to n. There is one number that is missing. We have to find the missing number in an efficient approach. So if A = [0, 1, 2, 3, 4, 5, 7, 8, 9], missing number is 6.To solve this, we will use the binary search approach.sort the list in ascending orderhigh = length of A, and low = 0while low < high, domid = low + (high – low)/2if A[mid] > midhigh = midotherwiselow = mid + 1return lowExampleLet us see the following implementation to get a better understanding − Live Democlass ... Read More

Advertisements