Created
January 28, 2026 15:42
-
-
Save vvgomes/2232f024134815919345f08ba758a5e4 to your computer and use it in GitHub Desktop.
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 goal of this program is to draw a pyramid in the terminal with a given height. | |
| ;; | |
| ;; Examples: | |
| ;; | |
| ;; ↓ height = 1 | |
| ;; | |
| ;; ∆ | |
| ;; | |
| ;; ↓ height = 2 | |
| ;; | |
| ;; ∆ | |
| ;; ∆∆∆ | |
| ;; | |
| ;; ↓ height = 3 | |
| ;; | |
| ;; ..∆.. | |
| ;; .∆∆∆. | |
| ;; ∆∆∆∆∆ | |
| ;; | |
| ;; ↓ height = 6 | |
| ;; | |
| ;; ∆ | |
| ;; ∆∆∆ | |
| ;; ∆∆∆∆∆ | |
| ;; ∆∆∆∆∆∆∆ | |
| ;; ∆∆∆∆∆∆∆∆∆ | |
| ;; ∆∆∆∆∆∆∆∆∆∆∆ | |
| ;; | |
| (ns pyramid | |
| (:require [clojure.string :refer [join]] | |
| [clojure.test :refer [deftest is]])) | |
| (defn- layer [height story] | |
| (let [width (- (* 2 height) 1) | |
| bricks (join (repeat width "∆")) | |
| padding (join (repeat story "_"))] | |
| (join [padding bricks padding]))) | |
| (defn draw | |
| "Draws pyramid bottom up" | |
| ([height] (draw height 0)) | |
| ([height story] | |
| (case height | |
| 0 "" | |
| 1 (layer height story) ;; reached the top of the pyramid (last iteration) | |
| (join "\n" [(draw (dec height) (inc story)) | |
| (layer height story)])))) | |
| (println (draw 0)) | |
| (println (draw 1)) | |
| (println (draw 2)) | |
| (println (draw 3)) | |
| (println (draw 6)) | |
| (println (draw 10)) | |
| (deftest test-layer | |
| (is (= (layer 1 2) "__∆__")) | |
| (is (= (layer 2 1) "_∆∆∆_")) | |
| (is (= (layer 3 0) "∆∆∆∆∆"))) | |
| (deftest test-draw | |
| (is (= (draw 3) "__∆__\n_∆∆∆_\n∆∆∆∆∆"))) | |
| ;; --- | |
| (defn ward | |
| "Draws pyramid top down" | |
| ([height] (ward height (dec height))) | |
| ([height story] | |
| (case story | |
| -1 "" | |
| 0 (layer height story) ;; reached the bottom of the pyramid (last iteration) | |
| (join "\n" [(layer (- height story) story) | |
| (ward height (dec story))])))) | |
| (println (ward 0)) | |
| (println (ward 1)) | |
| (println (ward 2)) | |
| (println (ward 3)) | |
| (println (ward 6)) | |
| (println (ward 10)) | |
| (deftest test-ward | |
| (is (= (ward 3) "__∆__\n_∆∆∆_\n∆∆∆∆∆"))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment