Last active
September 12, 2022 07:14
-
-
Save boraseoksoon/f08cea48135ee0a7bb140c764780fa35 to your computer and use it in GitHub Desktop.
The Pascal Triangle in Clojure
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
;; The Pascal Triangle: https://www.hackerrank.com/challenges/pascals-triangle/problem?isFullScreen=true | |
;; For a given integer , print the first rows of Pascal's Triangle. | |
;; Print each row with each value separated by a single space. | |
;; The value at the row and column of the triangle is equal to where indexing starts from. | |
;; These values are the binomial coefficients. | |
;; The Pascal Triangle | |
;; 1 | |
;; 1 1 | |
;; 1 2 1 | |
;; 1 3 3 1 | |
;; 1 4 6 4 1 | |
;; .... | |
;; Input Format | |
;; A single line of input, integer `K`. | |
;; Constraints | |
;; 2 <= `K` <= 10 | |
;; Output Format | |
;; Output the first `K` rows of Pascal's triangle. | |
;; Sample Input | |
;; 4 | |
;; Sample Output | |
;; 1 | |
;; 1 1 | |
;; 1 2 1 | |
;; 1 3 3 1 | |
;; Sample Input | |
;; 7 | |
;; Sample Output | |
;; 1 | |
;; 1 1 | |
;; 1 2 1 | |
;; 1 3 3 1 | |
;; 1 4 6 4 1 | |
;; 1 5 10 10 5 1 | |
;; 1 6 15 20 15 6 1 | |
(defn triangle | |
[input & {:keys [row output] | |
:or {row 1 output []}}] | |
(defn append [seq & {:keys [to] :or {to []}}] | |
(conj to seq)) | |
(defn form-seq [seq] | |
(flatten [(first seq) (form-rest seq)])) | |
(defn form-rest | |
[input-seq & {:keys [column output] | |
:or {column 1 output []}}] | |
(let [sum-seq (+ (or (first input-seq) 0) | |
(or (second input-seq) 0)) | |
seq (flatten (list (append sum-seq :to output)))] | |
(if (empty? input-seq) | |
(drop-last seq) | |
(form-rest (nthrest input-seq 1) | |
:column (inc column) | |
:output (vec seq))))) | |
(let [seq (cond (or (<= input 1) (= row 1)) '(1) | |
:else (form-seq (last output)))] | |
(cond | |
(<= input 1) | |
(append seq :to output) | |
(= row 1) | |
(triangle input | |
:row (inc row) | |
:output (append seq :to output)) | |
(< row input) | |
(triangle input | |
:row (inc row) | |
:output (append seq :to output)) | |
:else | |
(append seq :to output)))) | |
(doseq [row (triangle 10)] | |
(apply prn row)) | |
;; 1 | |
;; 1 1 | |
;; 1 2 1 | |
;; 1 3 3 1 | |
;; 1 4 6 4 1 | |
;; 1 5 10 10 5 1 | |
;; 1 6 15 20 15 6 1 | |
;; 1 7 21 35 35 21 7 1 | |
;; 1 8 28 56 70 56 28 8 1 | |
;; 1 9 36 84 126 126 84 36 9 1 | |
;; for hackerrank submission: https://www.hackerrank.com/challenges/pascals-triangle/problem?isFullScreen=true | |
; Enter your code here. Read input from STDIN. Print output to STDOUT | |
; | |
(let [i (read-string (read-line))] | |
(defn append [seq & {:keys [to] :or {to []}}] | |
(conj to seq)) | |
(defn form-rest | |
[input-seq & {:keys [column output] | |
:or {column 1 output []}}] | |
(let [sum-seq (+ (or (first input-seq) 0) | |
(or (second input-seq) 0)) | |
seq (flatten (list (append sum-seq :to output)))] | |
(if (empty? input-seq) | |
(drop-last seq) | |
(form-rest (nthrest input-seq 1) | |
:column (inc column) | |
:output (vec seq))))) | |
(defn form-seq [seq] | |
(flatten [(first seq) (form-rest seq)])) | |
(defn triangle | |
[input & {:keys [row output] | |
:or {row 1 output []}}] | |
(let [seq (cond (or (<= input 1) (= row 1)) '(1) | |
:else (form-seq (last output)))] | |
(cond | |
(<= input 1) | |
(append seq :to output) | |
(= row 1) | |
(triangle input | |
:row (inc row) | |
:output (append seq :to output)) | |
(< row input) | |
(triangle input | |
:row (inc row) | |
:output (append seq :to output)) | |
:else | |
(append seq :to output)))) | |
(doseq [row (triangle i)] | |
(apply prn row))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment