Skip to content

Instantly share code, notes, and snippets.

View jneira's full-sized avatar
:octocat:

Javier Neira jneira

:octocat:
View GitHub Profile
@jneira
jneira / idd.clj
Created October 7, 2011 20:05 — forked from marick/gist:1270755
;; I cant resist paste my version writing bootom-up through interactive driven devlopment ;-)
;; the origin of the puzzle is http://swizec.com/blog/functional-isnt-always-better/swizec/2591
(defn tails [xs]
(take-while seq (iterate rest xs)))
(defn pairs [xs]
(mapcat (fn [[x & y]] (map list (repeat x) y))
(tails xs)))
;; some vesions in comments in the blog post
module Foldtree where
data Tree a = Leaf a | Trees [Tree a] deriving(Show)
cow = Trees [Leaf 1, Trees [ Leaf 2, Leaf 3], Leaf 4]
-- Using concatMap
aplaneitor1 :: Tree a -> [a]
aplaneitor1 (Leaf a) = [a]
aplaneitor1 (Trees trees) = trees >>= aplaneitor1
function flatten (xs) {
return xs.reduce (function (acc,x){
return acc.concat(Array.isArray(x)?flatten(x):x)},[])
}
function phineas (arr,sep) {
return flatten(arr).join(sep)
}
var res=phineas(["hola", ["soy", ["juan", "fernandez"] ], "y", ["no", "tengo", ["dinero"] ] ],"*")
console.log(res)
function aplaneitor(soa,acu) {
if (soa instanceof Array) {
for (var j=0, len = soa.length;j<len;j++) {
aplaneitor(soa[j],acu);
}
}
else {
acu.push(soa);
}
}
;; jneira's solution to Beauty is Symmetry
;; https://4clojure.com/problem/96
(fn s [[_ [x l r]
[y i d]]]
(and (= x y)
(or (= nil l i)
(and
(s [_ l d])
(s [_ r i])))))
;; jneira's solution to Graph Connectivity
;; https://4clojure.com/problem/91
(fn [c]
(let [[f & r] (seq c)
h #(apply concat %)]
(= (set (h c))
(reduce (fn [a x]
(into a (h (filter #(some a %) c))))
(set f) r))))
;; jneira's solution to Read Roman numerals
;; https://4clojure.com/problem/92
(fn rr [[f & [s & r :as nxt]]]
(condp #(or %1 %2) nil
({"IV" 4 "IX" 9 "XL" 40 "XC" 90 "CD" 400 "CM" 900}
(str f s)) :>> #(+ % (rr r))
({"I" 1 "V" 5 "X" 10 "L" 50 "C" 100 "D" 500 "M" 1000}
(str f)) :>> #(+ % (rr nxt))
0))
;; jneira's solution to Partially Flatten a Sequence
;; https://4clojure.com/problem/93
(fn f [c]
(if (coll? c)
(if (some coll? c)
(mapcat f c) [c])
c))
;; jneira's solution to Transitive Closure
;; https://4clojure.com/problem/84
(fn [s]
(let [f #(for [[a b] % [c d] %
:when (= c b)] [a d])]
(->> s (iterate #(let [n (into % (f %))]
(when (not= % n) n)))
(take-while identity)
(last))))
;; jneira's solution to Power Set
;; https://4clojure.com/problem/85
;; translated directly from algorithm described in wikipedia :-P
(fn P [S]
(if (empty? S) #{S}
(let [e (first S)
T (disj S e)
PT (P T)
F (fn [e T] (set (map #(conj % e) T)))]