Skip to content

Instantly share code, notes, and snippets.

View anish000kumar's full-sized avatar
🎯
Focusing

Anish Kumar anish000kumar

🎯
Focusing
  • Gurgaon, India
View GitHub Profile
@anish000kumar
anish000kumar / multiply.py
Last active November 22, 2020 10:05
Multiply two positive numbers
class Solution:
def getSum(self, nums_list, limit):
# accepts nums_list, array of numbers in reversed order eg, [ [1, 3], [3, 2] ] == 31+23
res = []
iters = [ iter(nums) for nums in nums_list];
carry = 0
for i in range(limit):
val = carry
for _iter in iters: val += next(_iter, 0)
def getClosest(nums, k):
if k < nums[0]: return nums[0]
if k > nums[len(nums)-1]: return nums[len(nums)-1]
left, right = 0 , len(nums)-1
while left <= right:
mid = left + (right-left)//2
if nums[mid] > k:
@anish000kumar
anish000kumar / power.py
Created August 29, 2020 23:35
Pow(x, n)
def myPow(self, x: float, n: int) -> float:
ans, power = 1, abs(n)
while power:
if power % 2 == 0:
x *= x
power //= 2
else:
ans *= x
power -= 1
@anish000kumar
anish000kumar / Rejection Sampling.py
Last active August 24, 2020 09:38
Using rejection sampling to generate random number
# The rand7() API is already defined for you.
# def rand7():
# @return a random integer in the range 1 to 7
class Solution:
def generate(self):
row, col = rand7(), rand7()
return ((row-1) * 7 ) + col
def rand10(self):
@anish000kumar
anish000kumar / binarySumMultiply.js
Last active August 14, 2021 15:22
Binary Sum & multiply
function sum(x, y){
if(y === 0) return x
return sum( x^y, (x&y)<<1 )
}
function multiply(x, y){
if(x ===0 || y ===0) return 0
@anish000kumar
anish000kumar / dutchFlag.py
Last active February 9, 2021 16:17
dutch national flag problem solution
class Solution:
def sortColors(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
start, end = 0, len(nums)-1
i = start
@anish000kumar
anish000kumar / Trie.py
Last active July 19, 2020 04:23
Trie implementation in python
class Node:
def __init__(self, val=None):
self.val = val
self.children = {}
self.count = 0. # number of times words with this prefix
self.end = 0 # number of times this word has been inserted
class Trie:
@anish000kumar
anish000kumar / UnionFind.py
Last active July 18, 2020 22:09
Quick Disjoint Set in python (Union Find with path compression)
class Node:
def __init__(self, value):
self.value = value
self.rank = 1
self.parent = self
class DisjointSet:
def __init__(self):
self.mapping = {}
@anish000kumar
anish000kumar / listMidNode.js
Created November 9, 2018 17:47
Get middle node of linked list
function getListMiddle(head){
let slow = head;
let fast = head;
let moveBoth = false;
// to get second element, in case of two mid-nodes, use 'fast' instead of 'fast.next'
while(fast.next){
if(moveBoth){
slow = slow.next;
fast = fast.next;
@anish000kumar
anish000kumar / reverseLinkedList.js
Last active November 9, 2018 17:44
reverse a linked list
function reverseLinkedList(head){
let prev = null;
let next = null;
while(head){
next = head.next;
head.next = prev;
prev = head;