Skip to content

Instantly share code, notes, and snippets.

@pathikrit
pathikrit / NQueen.scala
Last active January 19, 2023 21:30
O(n!) solution to the n-Queen puzzle (https://en.wikipedia.org/wiki/Eight_queens_puzzle)
/**
* Solves the n-Queen puzzle in O(n!)
* Let p[r] be the column of the queen on the rth row (must be exactly 1 queen per row)
* There also must be exactly 1 queen per column and hence p must be a permuation of (0 until n)
* There must be n distinct (col + diag) and n distinct (col - diag) for each queen (else bishop attacks)
* @return returns a Iterator of solutions
* Each solution is an array p of length n such that p[i] is the column of the queen on the ith row
*/
def nQueens(n: Int): Iterator[Seq[Int]] =
(0 until n)
@pathikrit
pathikrit / ValueSortedMap.scala
Last active June 3, 2019 14:36
A sorted map that sorts keys by value
import scala.collection.mutable
type PQ[K, V] = mutable.SortedMap[K, V]
object PQ {
def apply[K, V: Ordering](elems: Seq[(K, V)]): PQ[K, V] =
elems.foldLeft(PQ.empty[K, V])(_ += _)
/**
* A SortedMap which sorts keys by the value
@pathikrit
pathikrit / bulk_edit_commit_msgs.sh
Created July 5, 2016 13:24
Script to replace a substring in all new commit messages in your feature branch
#!/usr/bin/env bash
substring=$1
replace=$2
current_branch=$(git name-rev --name-only HEAD)
echo "Replacing ${substring} with ${replace} in all new commits in ${current_branch}"
cd $(git root)
git filter-branch --msg-filter "'sed ""s/${substring}/${replace}/g""'" master..${current_branch}
git push -f
@pathikrit
pathikrit / MortgageMath.md
Created August 16, 2016 19:35
Mortgage Math

Input

p := Original Principal amount
apr := Annual Percentage Rate
t := Number of years of loan

Calcuation:

r := apr/100/12                    # monthly interest rate
@pathikrit
pathikrit / README.md
Last active April 20, 2026 05:58
My highly opinionated list of things needed to build an app in Scala
@pathikrit
pathikrit / DbBatchedJob.scala
Created September 14, 2016 17:57
Batched prepared statements
package com.coatue.datascience.util
import java.sql.{Connection, PreparedStatement}
import com.typesafe.scalalogging.Logger
import org.slf4j.LoggerFactory
import slick.driver.PostgresDriver.api._
class DbBatchedJob(db: Database, sql: String, batchSize: Int) extends AutoCloseable {
@pathikrit
pathikrit / GroupNamedRegex.scala
Last active September 21, 2017 01:37
Group named Regex
import java.util.regex.{MatchResult, Pattern}
import scala.collection.mutable
/**
* Supports named group finding
*
* @see http://stackoverflow.com/questions/39754604/
*/
class GroupNamedRegex(pattern: Pattern, namedGroups: Set[String]) {
def this(regex: String) = this(Pattern.compile(regex), GroupNamedRegex.namePattern.findAllMatchIn(regex).map(_.group(1)).toSet)
@pathikrit
pathikrit / ProducerConsumer.scala
Last active August 19, 2025 23:29
Single synchronous Producer/Consumer in Scala
import java.util.concurrent.ArrayBlockingQueue
import scala.concurrent.{ExecutionContext, Future}
/**
* Rick's implementation of ghetto back-pressure algo
* Implement this trait and pass it off to ProducerConsumer.Runner to run it
*
* @tparam R Type of result to be crunched
* @tparam S State to iterate on
@pathikrit
pathikrit / deremote.sh
Created January 4, 2017 16:44
Delete all local branches whose remote is gone
git fetch -p && for branch in `git branch -vv | grep ': gone]' | awk '{print $1}'`; do git branch -D $branch; done
@pathikrit
pathikrit / CircularBuffer.java
Last active August 19, 2025 23:29
A circular (or ring) buffer: O(1) random-indexing (get/set), append, prepend, dropFirst, dropLast, clear
class CircularBuffer<T> {
private T[] array = (T[]) new Object[1<<4];
private int start = 0, end = 0;
public T get(int i) {
assert(0 <= i && i < size());
return array[mod(start + i)];
}
public void set(int i, T elem) {