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 / Stack.js
Last active July 27, 2018 15:21
Stack implementation in javascript (es6)
let Stack = (()=>{
let map = new WeakMap();
let _items = [];
class Stack {
constructor(...items){
// let's store our items array inside the weakmap
map.set(this, []);
// let's get the items array from map, and store it in _items for easy access elsewhere
var Queue = (()=>{
const map = new WeakMap();
let _items = []
class Queue{
constructor(...items){
//initialize the items in queue
map.set(this, []);
_items = map.get(this);
// enqueuing the items passed to the constructor
this.enqueue(...items)
@anish000kumar
anish000kumar / hackerearth_js_boilerplate.js
Last active October 23, 2018 20:04
hackerearch js boilerplate
process.stdin.resume();
process.stdin.setEncoding("utf-8");
var stdin_input = "";
process.stdin.on("data", function (input) {
stdin_input += input; // Reading input from STDIN
});
process.stdin.on("end", function () {
@anish000kumar
anish000kumar / numsort.js
Last active November 8, 2018 13:52
Sort array of numbers
numArray.sort((a, d) => a - d); // For ascending sort
numArray.sort((a, d) => d - a); // For descending sort
@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;
@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 / 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 / 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 / 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 / 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