Skip to content

Instantly share code, notes, and snippets.

View abhin4v's full-sized avatar

Abhinav Sarkar abhin4v

View GitHub Profile
@abhin4v
abhin4v / dfs.hs
Last active December 22, 2020 12:16
Implementation of depth first search in Haskell
module DepthFirstSearch where
import Data.Foldable (asum)
import Data.List ((\\))
dfs :: (Eq a) => (a -> [a]) -> a -> a -> Maybe [a]
dfs next start goal = dfs' [] start
where dfs' path current
| current == goal = Just . reverse $ goal : path
| null nexts = Nothing
@abhin4v
abhin4v / upsert.clj
Last active July 4, 2022 20:42
Postgres Upsert in Clojure using clojure.java.jdbc and honeysql.
(ns postgres.upsert
(:require [honeysql.core :as sql]
[clojure.java.jdbc :as jdbc]
[clojure.string :as str]
[clojure.set :as set]))
(defn- keyword->colname [kwd]
(-> kwd
name
(str/replace #"-" "_")))
@abhin4v
abhin4v / ilog2.sql
Last active December 29, 2015 10:59
Integer log base 2 function for Postgres PL/PGSQL. Useful for finding Most Significant set Bit of 32-bit integers.
CREATE OR REPLACE FUNCTION ilog2(v integer)
RETURNS integer AS
$$
DECLARE
r integer;
shift integer;
BEGIN
IF v > x'FFFF'::integer THEN r := 1 << 4; ELSE r := 0 << 4; END IF;
v := v >> r;
@abhin4v
abhin4v / SnakesAndLadders.hs
Created September 12, 2015 11:37
Simple Snake and Ladders gameplay implementation in Haskell
{-# LANGUAGE RecordWildCards #-}
module Main where
import qualified Data.Map as Map
import Control.Monad.State
import System.Random
import Data.Maybe
import Debug.Trace
main :: IO ()
@abhin4v
abhin4v / Calc.hs
Last active November 29, 2022 08:02
Simple Applicative Parser and Expression Calculator in Haskell
module Calc
( Expr(..)
, parse
, calculate
) where
import Control.Applicative
import Parser
data Expr = Add Expr Expr
@abhin4v
abhin4v / Treap.hs
Last active August 29, 2015 14:07
A Treap implementation in Haskell with QuickCheck tests
module Treap
( Treap (..)
, empty
, null
, insert
, delete
, member
, size
, rInsert
, rFromList
@abhin4v
abhin4v / maximal_cliques.py
Last active October 7, 2021 16:46
Finds all maximal cliques in a graph using the Bron-Kerbosch algorithm
# Finds all maximal cliques in a graph using the Bron-Kerbosch algorithm. The input graph here is
# in the adjacency list format, a dict with vertexes as keys and lists of their neighbors as values.
# https://en.wikipedia.org/wiki/Bron-Kerbosch_algorithm
from collections import defaultdict
def find_cliques(graph):
p = set(graph.keys())
r = set()
x = set()
@abhin4v
abhin4v / astar.hs
Last active November 24, 2022 06:36
A* (A star) search in haskell
import qualified Data.PQueue.Prio.Min as PQ
import qualified Data.HashSet as Set
import qualified Data.HashMap.Strict as Map
import Data.Hashable (Hashable)
import Data.List (foldl')
import Data.Maybe (fromJust)
astarSearch :: (Eq a, Hashable a) => a -> (a -> Bool) -> (a -> [(a, Int)]) -> (a -> Int) -> Maybe (Int, [a])
astarSearch startNode isGoalNode nextNodeFn heuristic =
astar (PQ.singleton (heuristic startNode) (startNode, 0))
@abhin4v
abhin4v / onlinedrummer_downloader.py
Last active January 10, 2023 11:39
Python script to download all song drum sheets from onlinedrummer.com and save them in an organized structure in the current directory. Requires python-mechanize lib to be installed.
import sys
import os
import itertools
import re
import traceback
import time
import mechanize
from functools import wraps
from urllib import urlretrieve
@abhin4v
abhin4v / dictlookup.sh
Last active December 21, 2015 12:29
Shell one liner to lookup the currently selected word's definition. Requires xclip, dict and python module 'inflection' to be installed and dict configured to use a dictionary. Opens the definition in less in a new terminal window.
gnome-terminal --geometry=80x100+450 -x sh -c \
"xclip -o | tr -cd '[:alpha:]' | python -c 'import sys, inflection; print inflection.singularize(sys.stdin.readline())' | xargs dict | less"