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 fib-iter [a b n] | |
(if (zero? n) | |
b | |
(recur (+ a b) a (dec n)))) | |
(defn fib [n] | |
(fib-iter 1 0 n)) | |
(fib 40) ;102334155 |
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 gdc [x y] | |
(if (= x y) | |
x | |
(if (> x y) | |
(recur (- x y) y) | |
(recur (- y x) x)))) | |
(gdc 10, 100) ; 10 |
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 qpart [data p] | |
(reduce (fn [acc item] | |
(cond | |
(< item p) (assoc acc :left (conj (:left acc) item)) | |
(> item p) (assoc acc :right (conj (:right acc) item)) | |
:else (assoc acc :ps (conj (:ps acc) item)))) | |
{:left [] :right [] :ps []} data)) | |
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 finditemwithoutpair [data] | |
(if (= 1 (count data)) | |
(first data) | |
(if (= (first data) (second data)) | |
(finditemwithoutpair (drop 2 data)) | |
(first data)))) |
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 mergestep [l r op] | |
(cond (empty? l) r | |
(empty? r) l | |
:else | |
(let [fl (first l) | |
fr (first r)] | |
(if (op fl fr) | |
(cons fl (mergestep (rest l) r op)) | |
(cons fr (mergestep l (rest r) op)))))) | |
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
func insertionSort(data []int) []int { | |
for n := 1; n < len(data); n++ { | |
v := data[n] | |
for k:=n - 1; k >= 0; k-- { | |
if v < data[k] { | |
// swap | |
data[k+1] = data[k] | |
data[k] = v | |
} | |
} |
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
var sumBTree = function(tree){ | |
if(!tree || tree.length == 0) return 0 | |
return tree[0] + sumBtree(tree[1]) + sumBtree(tree[2]) | |
} | |
var tree = [1, [2, [1], [6]], [3, [1]]] | |
sumBTree(tree) // 14 |
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 bubble-step [coll was-changed less?] | |
(if (second coll) | |
(let [[x1 x2 & xs] coll | |
is-less-than (less? x1 x2) | |
smaller (if is-less-than x1 x2) | |
larger (if is-less-than x2 x1) | |
is-changed (or was-changed (not is-less-than)) | |
[sorted is-sorted] (bubble-step (cons larger xs) is-changed less?)] | |
[(cons smaller sorted) is-sorted]) | |
[coll (not was-changed)])) |
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
(:require [clj-time.format :as clj-time-format])) | |
(defn get-day [date-str] | |
(let [date (clj-time-format/parse (clj-time-format/formatter "YYYY-MM-dd") date-str) | |
day (clj-time-format/unparse (clj-time-format/formatter "EEEE") date)] | |
day)) | |
(get-day "2013-06-09") | |
"Monday" |