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 / parentPreOrder.js
Created August 28, 2021 04:33
PreOrder traversal using parent pointer
function Node(value, parent = null){
this.value = value
this.left = null
this.right = null
this.parent = parent
}
// create tree
@anish000kumar
anish000kumar / parentPostOrder.js
Last active August 28, 2021 05:29
PostOrder traversal using parent pointer
function Node(value, parent = null){
this.value = value
this.left = null
this.right = null
this.parent = parent
}
// create tree
@anish000kumar
anish000kumar / parentInOrder.js
Created August 28, 2021 04:32
inOrder traversal with parent pointer
function Node(value, parent = null){
this.value = value
this.left = null
this.right = null
this.parent = parent
}
// create tree
@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 / FenwickTree.js
Created June 4, 2021 05:39
FenwickTree Javascript implementation
class FenwickTree{
constructor(array){
this.source = [...array]
this.length = this.source.length;
this.fenwick = new Array(array.length+1).fill(0)
this.build();
}
build(){
for(let i = 0; i < this.length; i++)
@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)
@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 / 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 / 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 / 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