Skip to content

Instantly share code, notes, and snippets.

Pathikrit Bhowmick pathikrit

View GitHub Profile
@pathikrit
pathikrit / script.scala
Last active Dec 12, 2018
Move duplicate files to a directory
View script.scala
import better.files._
def moveDupes(
dir: File,
logFile: File = (File.home / "dupes.txt"),
dupeFolder: File = (File.home / 'dupes).createDirectory()
) = {
for {
log <- logFile.printWriter()
(hash, toKeep :: toMove) <- dir.listRecursively.toSeq.groupBy(_.md5).mapValues(_.toList)
@pathikrit
pathikrit / GitPunchCard.scala
Last active Apr 15, 2018
Scala Script to print Git PunchCard
View GitPunchCard.scala
/**
* Quick and dirty Scala app to print git commit punch-card e.g.
*
* ┃08┃09┃10┃11┃12┃13┃14┃15┃16┃17┃18┃19┃20┃21┃22┃23┃00┃01┃02┃03┃04┃05┃06┃07┃
* Sun┃▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
* Mon┃▁▁▁▄▄▄▅▅▅▅▅▅▄▄▄▆▆▆▇▇▇▇▇▇███▆▆▆▅▅▅▄▄▄▃▃▃▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
* Tue┃▁▁▁▃▃▃▆▆▆▅▅▅▅▅▅▅▅▅▆▆▆▇▇▇▆▆▆▆▆▆▅▅▅▅▅▅▄▄▄▃▃▃▂▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
* Wed┃▁▁▁▄▄▄▅▅▅▇▇▇▅▅▅▅▅▅███▇▇▇▅▅▅▆▆▆▇▇▇▃▃▃▃▃▃▂▂▂▂▂▂▂▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
* Thu┃▁▁▁▂▂▂▄▄▄▆▆▆▅▅▅▆▆▆▇▇▇▇▇▇▆▆▆▇▇▇▅▅▅▄▄▄▃▃▃▂▂▂▂▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
* Fri┃▁▁▁▂▂▂▄▄▄▅▅▅▅▅▅▄▄▄▄▄▄▅▅▅▅▅▅▃▃▃▃▃▃▂▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
View local.sbt
triggeredMessage := Watched.clearWhenTriggered
libraryDependencies += "com.lihaoyi" % "ammonite" % "latest.release" % "test" cross CrossVersion.full
initialCommands in (Test, console) := """ammonite.Main().run()"""
watchSources ++= (
(baseDirectory.value * "*.sbt").get
++ (baseDirectory.value / "project" * "*.scala").get
++ (baseDirectory.value / "project" * "*.sbt").get
@pathikrit
pathikrit / MajorityElement.scala
Last active Jun 12, 2017
Boyer–Moore majority vote algorithm
View MajorityElement.scala
import scala.collection.generic.Growable
/**
* Boyer–Moore majority vote algorithm (https://en.wikipedia.org/wiki/Boyer–Moore_majority_vote_algorithm)
* A Data structure that supports O(1) tracking of the majority element in streaming data
* (i.e. something that occurs strictly > 50% of the time)
*/
class MajorityElement[A] extends Growable[A] {
private[this] var majorityElement = Option.empty[A]
private[this] var count = 0
@pathikrit
pathikrit / README.md
Last active Jan 12, 2019
Contravariance vs. Covariance
View README.md
  • Let C<A> be a higher-kinded type e.g. in List<Animal>, List is C and Animal is A.
  • Let S be a subtype of T e.g. in class Cat extends Animal, Cat is S and Animal is T
  • If C<S> is a subtype of C<T>, then C is covaraint on T e.g. List<Cat> is a subtype of List<Animal>
  • If C<T> is a subtype of C<S>, then C is contravariant on T e.g. Predicate<Animal> is a subtype of Predicate<Cat>
  • If neither C<T> and nor C<S> are subtypes of the other, thenC is invariant on T
  • If both C<T> and C<S> are subtypes of each other, then C is phantom variant on T. This is possible in languages which support phantom types like Haskell

In Scala:

@pathikrit
pathikrit / PalindromicSubstrings.scala
Last active Apr 15, 2018
Count number of palindromic substrings (non-empty) of a string
View PalindromicSubstrings.scala
/**
* 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 Jun 26, 2018
Better Config
View Config.scala
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 Mar 2, 2017
Thread-safe k-shingling in Scala
View KShingling.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 Oct 28, 2018
A circular (or ring) buffer: O(1) random-indexing (get/set), append, prepend, dropFirst, dropLast, clear
View CircularBuffer.java
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 Jan 4, 2017
Delete all local branches whose remote is gone
View deremote.sh
git fetch -p && for branch in `git branch -vv | grep ': gone]' | awk '{print $1}'`; do git branch -D $branch; done
You can’t perform that action at this time.