This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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 | |
/** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; Suppose you have a CSV file with the first row being the column names | |
; and the rest being data. Using the following snippet you can process | |
; the contents passing each row's contents as keyword arguments to | |
; another function. | |
; | |
; Example CSV structure: | |
; +----------------------------------------------------------+ | |
; | customer-code | name | e-mail | address | phone | | |
; +----------------------------------------------------------+ | |
; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn getfn [fname] | |
"Gets a function by its fully qualified name." | |
(let [fsym (symbol fname) | |
nsym (symbol (namespace fsym))] | |
(require nsym) | |
(resolve fsym))) | |
(defn interpolate [s vals] | |
"Returns the interpolation of string 's' with a map of key/values." | |
;Keys in 's' are marked with #{key-name} syntax. |
NewerOlder