Skip to content

Instantly share code, notes, and snippets.

View mourjo's full-sized avatar
👋

Mourjo Sen mourjo

👋
View GitHub Profile
@mourjo
mourjo / TravellingSalesman.java
Created July 18, 2016 18:59
Travelling Salesman (Simple Brute Force Algorithm)
import java.awt.Point;
import java.awt.geom.Point2D;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class TravellingSalesman {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
@mourjo
mourjo / hash.py
Created August 23, 2016 02:56
Custom Hashmap
class bst:
def __init__(self, k, v):
self.left = None
self.right = None
self.v = v
self.k = k
self.size = 1
def __str__(self):
# for debugging only
@mourjo
mourjo / Queue.java
Last active July 10, 2017 06:36
Array backed queue
package arraybackedqueue;
import java.util.LinkedList;
import java.util.Random;
public class Queue {
private int head;
private int tail;
private int data[];
@mourjo
mourjo / klotski.ml
Created December 11, 2018 16:34
Solving the klotski puzzle in OCaml
(* For the original problem statement, check http://mourjo.me/klotski_problem.pdf *)
exception NotFound
type 'e rel = 'e -> 'e list
type 'e prop = 'e -> bool
type ('a, 'set) set_operations = {
empty : 'set; (* The empty set. *)
mem : 'a -> 'set -> bool; (* [mem x s = true] iff [x] is in [s]. *)
@mourjo
mourjo / with-local-redefs.clj
Last active July 27, 2024 12:07 — forked from gfredericks/with-local-redefs.clj
thread-local version of with-redefs
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Commentary ;;
;; ;;
;; The goal for writing this started with the idea to have tests run in ;;
;; parallel using the leiningen plugin eftest ;;
;; https://github.com/weavejester/eftest. ;;
;; ;;
;; With tests using with-redefs, it was not possible to run them in ;;
;; parallel if they were changing the root binding of the same ;;
;; vars. Here, we are binding the root of the var to one function that ;;
@mourjo
mourjo / plank.clj
Last active September 1, 2020 12:15
An inverted index like Elasticsearch for term filter lookups only
(ns plank.core
(:import (java.util.regex Pattern)))
; Problem Statement: Write code to solve this problem
; Given a set of news articles find articles that match the search query from a user.
; Assume that the search query is a single word.
;
; Each article has this structure: Id,Headline,Content
; Articles cannot be updated
@mourjo
mourjo / docker-compose.yml
Last active August 14, 2024 07:06
Docker compose for a minimal Kibana + Elasticsearch locally
version: '2.2'
services:
es01:
image: docker.elastic.co/elasticsearch/elasticsearch:7.15.1
container_name: es01
environment:
- node.name=es01
- cluster.name=es-docker-cluster
- discovery.type=single-node
- bootstrap.memory_lock=true
@mourjo
mourjo / apple_health_stats.js
Created January 1, 2022 05:59
Poor person's Apple Health stats for distance run in a year
import fs, { readFileSync } from 'fs';
import readline from 'readline';
const YEAR = '2021';
function infer(row) {
const ts = row.match(/startDate=".*?"/g)[0].match(/.?.?.?.?-.?.?-.?.? .?.?:.?.?:.?.? .?.?.?.?.?/)[0]
const dt = ts.substring(0, 10)
if (dt.startsWith(YEAR)) {
const unit = row.match(/unit=".*?"/)[0].match(/".?.?"/)[0].substring(1, 3)
@mourjo
mourjo / primes.clj
Created January 15, 2022 12:21
Clojure - Lazy vs non lazy sequence of primes
(defn prime?
[x]
(not-any? (fn [factor] (zero? (mod x factor)))
(range 2 (dec x))))
(defn eager-primes
([n] (eager-primes n 2 []))
([n curr xs]
(if (= n (count xs))
@mourjo
mourjo / workerdemo.js
Last active February 9, 2022 11:20
Concurrent Node.js workers
import * as nodeWorker from 'worker_threads';
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
async function doSomething(name) {
console.log(`${Date.now()}: Worker ${name} working ...`);
await sleep(Math.floor(10000));