Skip to content

Instantly share code, notes, and snippets.

View flengyel's full-sized avatar
🎯
Focusing

F Lengyel flengyel

🎯
Focusing
  • The City University of New York
  • New York
  • 17:21 (UTC -04:00)
View GitHub Profile
;; flengyel's solution to Graph Tour
;; https://4clojure.com/problem/89
;; The edge vector is a vector of edges, each a vector with two vertices [e1 e2]
(fn [edgevec]
(letfn [(neighbors [edgevec] ;; map each vertex to its set of neighbors
(reduce
(fn [m e]
(letfn [(addmap [m a b] (assoc m a (if-let [nbdset (get m a)]
(conj nbdset b)
;; 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))))))
;; 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))
@flengyel
flengyel / reverseproxied.py
Created December 7, 2011 22:16 — forked from joshourisman/gist:970746
Basic Flask REST server
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):
;; 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
;; 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))))))
@flengyel
flengyel / flengyel-4clojure-solution65.clj
Created December 8, 2011 06:34 — forked from anonymous/flengyel-4clojure-solution65.clj
Solution to Black Box Testing (4clojure problem 65)
;; 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
;; 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)]
;; 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]
@flengyel
flengyel / flengyel-4clojure-solution92.clj
Created December 8, 2011 06:46 — forked from anonymous/flengyel-4clojure-solution92.clj
Read Roman Numerals (4clojure problem 92)
;; 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)]