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 merge-sort | |
"Produce a sorted sequence from an input sequence. Sort into ascending order by default, or use optional comparison function." | |
([x] (merge-sort < x)) | |
([swo x] | |
(letfn [(merge [left right] | |
(loop [l left r right result []] | |
(let [l-first (first l) r-first (first r)] | |
(cond (nil? l-first) (concat result r) | |
(nil? r-first) (concat result l) | |
:default (if (swo l-first r-first) |
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
(use '[clojure.string :only [trim replace]] | |
'[clojure.pprint :only [cl-format] :rename {cl-format format}]) | |
(defn- unspace [s] | |
(trim (replace s " " ""))) | |
(defn decode [bitstring] | |
"\"01100110 01101111 01101111\" -> \"foo\" | |
Whitespace in the input string is ignored." |
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 gen-primes [] | |
(letfn [(reinsert [table x prime] | |
(update-in table [(+ prime x)] conj prime)) | |
(step [table d] | |
(if-let [factors (table d)] | |
(recur (reduce #(reinsert %1 d %2) (dissoc table d) factors) | |
(inc d)) | |
(lazy-seq (cons d | |
(step (assoc table (* d d) (list d)) (inc d))))))] | |
(step {} 2))) |
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
(let [alpha (into #{} (concat (map char (range (int \a) (inc (int \z)))) | |
(map char (range (int \A) (inc (int \Z)))))) | |
rot13-map (zipmap alpha (take 52 (drop 26 (cycle alpha))))] | |
(defn rot13 | |
"Given an input string, produce the rot 13 version of the string. \"hello\" -> \"uryyb\"" | |
[s] (apply str (map #(get rot13-map % %) s)))) |
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
(map #(let [s (str %2 %3)] (if (seq s) s %)) (range 1 101) (cycle ["" "" "Fizz"]) (cycle ["" "" "" "" "Buzz"])) |
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 first-unique [s] | |
(let [f (frequencies s)] | |
(first (filter #(= (f %) 1) s)))) |
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
#include <iostream> | |
#include <vector> | |
#include <algorithm> | |
#include <functional> | |
using namespace std; | |
struct O | |
{ | |
void m(int n) const |
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
;; less code golf, more readability | |
;; also complies with specs of the problem by sending results to stdout (rather than just generating a sequence) | |
(doseq [n (map (some-fn #(and (= (mod % 3) 0) (= (mod % 5) 0) "FizzBuzz") | |
#(and (= (mod % 3) 0) "Fizz") | |
#(and (= (mod % 5) 0) "Buzz") | |
identity) (range 1 101))] | |
(println n)) |
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
(fn e [exp] | |
(fn [env] | |
(apply ({'+ + '- - '* * '/ /} (first exp)) | |
(map #(if (seq? %) ((e %) env) (env % %)) | |
(rest exp))))) |
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
(fn red | |
([f [v & vs]] (red f v vs)) | |
([f v [u & us]] | |
(cons v (when u (lazy-seq (red f (f v u) us)))))) |
OlderNewer