Skip to content

Instantly share code, notes, and snippets.

View bahmanm's full-sized avatar

Bahman Movaqar bahmanm

View GitHub Profile
@bahmanm
bahmanm / test-runner.el
Created December 10, 2014 23:53
Simple Maven + JUnit test runner for Emacs.
;;; A quick way to run JUnit tests:
;;; `C-x t u` runs all the test cases in a unit.
;;; `C-x t c` runs a single test case.
;;;
;;; Simply add the snippet at the end of `init.el` or `.emacs`.
;;; NOTE: It assumes you are using projectile to manage the project.
;;;
;;; By Bahman Movaqar
;; runs all test cases in the current class
@bahmanm
bahmanm / simple-constraint-problem.groovy
Last active August 29, 2015 14:10
Solve the constraint equation: A + B = CD - E = FG
/*
* Solve the equation A + B = CD - E = FG
* WHERE A..G in [0,1,2,3,7,8,9]
* AND CD = 10*C + D
* AND FG = 10*F + G
* AND C not 0 and F not 0
* AND different letters are different digits
*/
[0,1,2,3,7,8,9].permutations().findAll {
@bahmanm
bahmanm / ListCombinations.groovy
Last active August 29, 2015 14:10
Iterator (lazy) style class to compute all the combinations of any number of given lists.
/*
* Author: Bahman Movaqar <Bahman AT BahmanM.com>
* Copyright (c) Bahman Movaqar
* This file is released under public domain license.
*/
///// Tests below the main class
import groovy.transform.PackageScope
/**
@bahmanm
bahmanm / kcombinations.groovy
Last active August 29, 2015 14:10
Function to compute k-Combinations of a list
List kCombinations(List l, Integer k) {
assert k >= 0, 'Invalid "k"'
if (l.empty || !k) []
else if (k == 1) l.collate(1)
else
(0..l.size()-1).inject([] as Set) { acc, i ->
def c = kCombinations(l.drop(i+1), k-1)
acc + [l[i], c].combinations { cn ->
def cnf = cn.flatten()
cnf.size() == k ? cnf : [l[i],c].flatten()