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
;; 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) |
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
(ns buttondemo.core | |
(:gen-class) | |
(:import [javax.swing AbstractButton JButton JPanel JFrame ImageIcon] | |
[java.awt.event ActionEvent KeyEvent ActionListener]) | |
(:use [clojure.contrib.swing-utils])) | |
(defn create-image-icon [path] | |
; (prn (. System getProperty "user.dir")) | |
; (prn (. System getProperty "java.class.path")) | |
(if-let [img-url (clojure.java.io/resource path)] |
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
;; 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)))))) |
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
;; 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 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 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 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 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 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 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] |
OlderNewer