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 / 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 / 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
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 / 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)
@anish000kumar
anish000kumar / Sliding window maximum.py
Created December 3, 2020 15:24
Sliding window maximum
from collections import deque
def maxSlidingWindow(nums, window_size):
start, ans = 0, []
q = deque();
for end in range(len(nums)):
while q and nums[end] > nums[q[-1]]: q.pop()
@anish000kumar
anish000kumar / Minimum swaps to sort an array.py
Created December 7, 2020 05:50
Minimum swaps to sort an array
def minSwaps(arr, N):
newArr = [ (arr[i], i) for i in range(len(arr)) ]
newArr.sort()
ans = 0
for i in range(len(newArr)):
j = newArr[i][1]
while newArr[i][1] != newArr[j][1]:
@anish000kumar
anish000kumar / MergeSort linkedList.py
Last active December 7, 2020 10:11
MergeSort linkedList
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def findMiddle(self, head):
slow, fast = head, head
while fast and fast.next and fast.next.next:
@anish000kumar
anish000kumar / coordinate compression.py
Last active December 7, 2020 16:34
coordinate compression
def compress(arr):
arr.sort();
compress_map = {}
val = 1
for el in arr:
if el not in compress_map:
compress_map[el] = val
val += 1
@anish000kumar
anish000kumar / calendar.js
Created June 3, 2021 07:08
Calendar component in pure JS
class Calendar {
constructor(selector){
this.MONTHS= ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'July', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
this.root = document.querySelector(selector);
this.activeDateTime = new Date();
this.render();
this.attachEvents();
}
@anish000kumar
anish000kumar / animationLoop.js
Last active June 3, 2021 11:51
Animation loop
function animate({ timing, duration, draw }){
return new Promise((resolve, reject) => {
const start = performance.now();
function animation(time){
const progress = Math.min((time-start)/duration, 1);
const visualProgress = timing ? timing(progress): progress;
draw(visualProgress);
if(progress < 1) requestAnimationFrame(animation)