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
@saswata-dutta
saswata-dutta / json_collect_sum.sh
Created June 9, 2021 04:09
jq filter group_by and sum
cat summary.json | \
jq -r '.weeklyTotalCount[] | to_entries | .[] | select(.value > 0)' | \
jq -s 'group_by(.key) | map({week : .[0].key, count : [.[].value] | add })'
@saswata-dutta
saswata-dutta / iter_tree_trav.cpp
Last active June 7, 2021 20:26
Iterative binary tree traversals
vector<int> inorderTraversal(TreeNode* root) {
unordered_map<TreeNode*, int> count;
stack<TreeNode*> stk;
vector<int> traversal;
stk.push(root);
while(!stk.empty()) {
TreeNode* cur = stk.top();
@saswata-dutta
saswata-dutta / thread_safe_queue.cpp
Created May 11, 2021 08:35 — forked from holtgrewe/thread_safe_queue.cpp
A simple thread-safe queue implementation based on std::list<> and C++11 threading primitives.
// A simple thread-safe queue implementation based on std::list<>::splice
// after a tip in a talk by Sean Parent of Adobe.
//
// Uses standard library threading and synchronization primitives together
// with a std::list<> container for implementing a thread-safe queue. The
// only special thing is that the queue uses std::list<>::splice in the
// locked region to minimize locked time.
//
// Also implements a maximal size and can thus be used as a buffer between
// the elements in a pipeline with limited buffer bloat.
@saswata-dutta
saswata-dutta / flatten_array.java
Last active June 9, 2021 08:39
Flatten an array of arbitrarily nested arrays of integers into a flat array of integers. e.g.[[1, [2, [3]]], 4] -> [1,2,3,4].
import java.util.ArrayList;
import java.util.List;
public class FlattenArray {
static List<Integer> flatten(Object[] input) {
if (input == null) return null;
List<Integer> acc = new ArrayList<>();
for (Object el : input) {
@saswata-dutta
saswata-dutta / dictionary_nomad.py
Last active April 25, 2021 07:42
want to reach from start to end by modifying one character at a time and making sure each resulting word is also in the dictionary
from itertools import combinations
from collections import deque
def is_nbr(s1, s2):
if len(s1) != len(s2):
return False
dist = (int(a != b) for a,b in zip(s1, s2))
@saswata-dutta
saswata-dutta / disjoint_set_union_find.py
Last active April 25, 2021 06:03
using disjoint set union find to count connected components in a graph
class DisjointSet:
def __init__(self, n):
self.parent = list(range(n))
self.rank = [0] * n
def union(self, x, y):
x = self.find(x)
y = self.find(y)
@saswata-dutta
saswata-dutta / multi_dimension_avg.scala
Created April 17, 2021 06:36
averages across multiple dimensions
/*
Inner_Collapsingkey - Outer_GroupingKey = Implicit_ObservationKey
collapsingKey == primaryKey(default)
what is the average daily amount of items sold for each employee ?
Inner_Collapsingkey = (day, employee) => what we want to define as an observation for this analysis
Outer_GroupingKey = employee => key defining the required groups to report
import fileinput
import copy
import logging
import time
from queue import Queue
from threading import Thread
format = "%(asctime)s.%(msecs)03d %(threadName)s %(levelname)s: %(message)s"
from math import isqrt
def primes_less_than(n: int) -> list[int]:
if n<= 2:
return []
is_prime = [True] * n
is_prime[0] = False
is_prime[1] = False
for i in range(2, isqrt(n)+1):
@saswata-dutta
saswata-dutta / tree-walk.py
Created March 22, 2021 10:55
json tree walking
def count(node, target):
""" counts occurrences of target in node's hierarchy"""
if node == target:
return 1
if isinstance(node, list):
return sum(count(el, target) for el in node)
if isinstance(node, dict):