Last active
December 28, 2015 22:59
-
-
Save moserrya/7575154 to your computer and use it in GitHub Desktop.
4Clojure #97: Pascal's triangle
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 [string] | |
(let [br (re-seq #"[()\[\]{}]" string)] | |
(loop [brackets br acc []] | |
(if (empty? brackets) | |
true | |
(if-let [lb (#{"(" "[" "{"} (first brackets))] | |
(recur (rest brackets) (conj acc lb)) | |
(if (= (peek acc) ({")" "(" "}" "{" "[" "]"} (first brackets))) | |
(recur (rest brackets) (pop acc)) | |
false)))))) | |
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 fact [n] | |
(loop [n n acc 1] | |
(if (zero? n) | |
acc | |
(recur (dec n) (* n acc))))) | |
(defn n-choose-k [n k] | |
(/ (fact n) (* (fact k) (fact (- n k))))) | |
(defn pascal-row [n] | |
(for [el (range (inc n)) | |
:let [i (n-choose-k n el)]] | |
i)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment