Skip to content

Instantly share code, notes, and snippets.

@pathikrit
pathikrit / PalindromicSubstrings.scala
Last active April 15, 2018 11:18
Count number of palindromic substrings (non-empty) of a string
/**
* Dumb O(n^3) version: Check if all n^2 substrings are palindromes
*/
def countPalindromes3(s: String): Int =
(1 to s.length).flatMap(s.sliding).count(t => t.reverse == t)
/***********************************************************************/
/**
* O(n^2) solution - count how many (odd and even) palindromes are centered at index i by expanding left and right
*/
@pathikrit
pathikrit / Config.scala
Last active March 5, 2020 14:22
Better Config
import scala.util.control.NonFatal
import better.files.Scanner.Read
/**
* Extend this trait to create your application config
*
* Pros of this approach:
* 1) Library free approach - only 15 lines of dependency free "library" (four one-line defs for you to override)
* 2) Failures happen when the Config object is loaded instead of when a config value is accessed
* 3) Strongly typed
@pathikrit
pathikrit / KShingling.scala
Created March 2, 2017 15:46
Thread-safe k-shingling in Scala
import scala.collection.mutable
/**
* A thread-safe Scala port of the KShingling implementation of https://github.com/tdebatty/java-string-similarity
*/
class KShingling(k: Int = 3) {self =>
private[this] val shingles = mutable.Map.empty[String, Int]
def profile(s: String) = {
@pathikrit
pathikrit / CircularBuffer.java
Last active October 28, 2018 05:02
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) {
@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 / ProducerConsumer.scala
Last active October 28, 2018 05:06
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 / 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 / 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 / README.md
Last active April 24, 2021 17:36
My highly opinionated list of things needed to build an app in Scala
@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