Skip to content

Instantly share code, notes, and snippets.

View saswata-dutta's full-sized avatar
💭
I may be slow to respond.

Saswata Dutta saswata-dutta

💭
I may be slow to respond.
View GitHub Profile
public class Solution {
private HashMap<Integer, UndirectedGraphNode> map = new HashMap<>();
public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
return clone(node);
}
private UndirectedGraphNode clone(UndirectedGraphNode node) {
if (node == null) return null;
if (map.containsKey(node.label)) {
@saswata-dutta
saswata-dutta / zig-zag-tree.java
Created July 6, 2021 05:07
traverse binary tree in zig zag
void zigzag(TreeNode curr, ArrayList<LinkedList<Integer>> sol, int level) {
if(curr == null) return;
if(sol.size() <= level) {
sol.add(new LinkedList<>());
}
List<Integer> collection = sol.get(level);
if(level % 2 == 0) collection.add(curr.val);
else collection.add(0, curr.val);
def tryWithResource[R <: Closeable, T](createResource: => R)(f: R => T): T = {
val resource = createResource
try f.apply(resource) finally resource.close()
}
# Credit for this: Nicholas Swift
# as found at https://medium.com/@nicholas.w.swift/easy-a-star-pathfinding-7e6689c7f7b2
from warnings import warn
import heapq
class Node:
"""
A node class for A* Pathfinding
"""
@saswata-dutta
saswata-dutta / jvm_stuck_debug.sh
Created June 23, 2021 06:36
tools to look into stuck threads in jvm
jps -lm
jstack -l <pid>
jcmd <pid> Thread.print
@saswata-dutta
saswata-dutta / html_td_to_csv.js
Last active July 24, 2022 15:05
extract csv from webpage table
rows = document.querySelectorAll('tr.???');
cellText = cell => cell.innerText;
rowText = r => Array.from(r.getElementsByTagName("td"), c => cellText(c)).join('|');
csv = Array.from(rows, row => rowText(row)).join("\n");
@saswata-dutta
saswata-dutta / pipenv_cheat_sheet.md
Last active June 22, 2021 03:36 — forked from bradtraversy/pipenv_cheat_sheet.md
Pipenv cheat sheet for common commands

Pipenv Cheat Sheet

Install pipenv

pip3 install pipenv

Activate

pipenv shell
@saswata-dutta
saswata-dutta / kv_zip.py
Created June 17, 2021 08:25
operate on two list at once
def process(kstr, vstr, n):
k = filter(lambda x: x != "id", kstr.split())
v = vstr.split()
kv = list(zip(k, v))
exe = filter(lambda x: x[0] == "execution", kv)
tot = filter(lambda x: x[0] == "total", kv)
times = ((float(t[1]), float(e[1])) for e,t in zip(exe, tot))
@saswata-dutta
saswata-dutta / len_lis.cpp
Created June 15, 2021 08:58
find len of LIS using patience sort like piles
int lengthOfLIS(vector<int>& nums) {
vector<int> piles;
for(auto i: nums) {
auto it = std::lower_bound(piles.begin(), piles.end(), i);
if(it==piles.end()) piles.push_back(i);
else *it = i;
}
return piles.size();
}
@saswata-dutta
saswata-dutta / TaskScheduler.java
Created June 9, 2021 07:30
Emulating delayed scheduled thread executor
import java.util.Comparator;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.PriorityBlockingQueue;
class PqItem {
public final long ts;
public final Runnable action;