This file contains hidden or 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
| ;; flengyel's solution to Longest Increasing Sub-Seq | |
| ;; https://4clojure.com/problem/53 | |
| (fn [v] | |
| (let [[maybe n cur _] | |
| (let [v1 (first v)] | |
| (reduce | |
| (fn | |
| [[longest maxlen current prv] elt] | |
| (if (>= prv elt) |
This file contains hidden or 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
| ;; flengyel's solution to Read Roman numerals | |
| ;; https://4clojure.com/problem/92 | |
| ;; 4clojure rates this as "hard" but I found it easy. | |
| ;; Uses a reduce with destructuring to keep track of the "accumulated state". | |
| (fn [numeral] | |
| (-> (let [roman {\I 1, \V 5, \X 10, \L 50, \C 100, \D 500, \M 1000} | |
| digit (roman (first (vec numeral)))] | |
| (reduce | |
| (fn [[acc prv] cur] | |
| (let [nacc (+ acc cur)] |
This file contains hidden or 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
| ;; flengyel's solution to Write Roman Numerals | |
| ;; https://4clojure.com/problem/104 | |
| (fn [n] | |
| (let [rmap {1000 "M",900 "CM",500 "D",400 "CD", 100 "C", 90 "XC", 50 "L", 40 "XL", 10 "X", 9 "IX", 5 "V", 4 "IV", 1 "I"}] | |
| (first (reduce | |
| (fn [[s n] e] | |
| (loop [s s n n] | |
| (if (< n e) | |
| [s n] |
This file contains hidden or 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
| ;; flengyel's solution to Word Chains | |
| ;; https://4clojure.com/problem/82 | |
| (fn [wordset] (letfn [(patronize [s] (re-pattern (apply str (interpose ".{0,1}" (map str (vec s)))))) | |
| (onediff? [s t n] (let [v (vec s) | |
| w (vec t)] | |
| (= 1 (reduce + (for [i (range n)] (if (= (v i) (w i)) 0 1)))))) | |
| (linked? [s t] | |
| (let [a (count s) | |
| b (count t)] |
This file contains hidden or 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
| ;; flengyel's solution to Black Box Testing | |
| ;; https://4clojure.com/problem/65 | |
| (fn blackbox [s] | |
| (cond | |
| (= (conj s {}) s) :map | |
| (empty? s) (cond | |
| (= (clojure.set/union s #{}) #{}) :set | |
| (= (conj (conj s 0) 1) [0 1]) :vector | |
| :else :list) | |
| (= (clojure.set/union s s) s) :set |
This file contains hidden or 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
| ;; flengyel's solution to Function Composition | |
| ;; https://4clojure.com/problem/58 | |
| ;; comp is disallowed. The composition has function type | |
| (fn comb [f & fs] | |
| (fn [x & xs] (let [arglist (conj xs x)] | |
| (if (nil? fs) | |
| (apply f arglist) | |
| (f (apply (apply comb fs) arglist)))))) |
This file contains hidden or 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
| ;; flengyel's solution to Flatten a Sequence | |
| ;; https://4clojure.com/problem/28 | |
| (fn flat [L] (reduce #(if (coll? %2) ;; if item is collection | |
| (vec (concat %1 (flat %2))) ;; concat current with flat %2 | |
| (conj %1 %2)) ;; otherwise conj at top level | |
| [] L)) ;; empty flattened collection, L |
This file contains hidden or 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
| class ReverseProxied(object): | |
| def __init__(self, app): | |
| self.app = app | |
| def __call__(self, environ, start_response): | |
| script_name = environ.get('HTTP_X_SCRIPT_NAME', None) | |
| if script_name is not None: | |
| environ['SCRIPT_NAME'] = script_name | |
| path_info = environ['PATH_INFO'] | |
| if path_info.startswith(script_name): |
This file contains hidden or 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
| ;; flengyel's solution to Transitive Closure | |
| ;; https://4clojure.com/problem/84 | |
| (fn [rel] | |
| (reduce clojure.set/union | |
| (take (count rel) | |
| (iterate | |
| (partial | |
| (fn [R S] | |
| (set (let [left (lazy-seq (map #(% 0) R)) |
This file contains hidden or 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
| ;; flengyel's solution to Infix Calculator | |
| ;; https://4clojure.com/problem/135 | |
| (fn [a & b] | |
| (loop [acc a l b] | |
| (if (empty? l) | |
| acc | |
| (recur ((first l) acc (first (rest l))) | |
| (rest (rest l)))))) |