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
| pub fn generate_arrays(n: i32) -> Vec<Vec<i32>> { | |
| (1..=n).map(|x| (1..=x).collect()).collect() | |
| } | |
| #[test] | |
| fn generate_arrays_test() { | |
| assert_eq!( | |
| generate_arrays(4), | |
| vec![vec![1], vec![1, 2], vec![1, 2, 3], vec![1, 2, 3, 4]] | |
| ); |
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
| (ns generate-arrays | |
| (:require [clojure.test :refer [deftest is])) | |
| (defn generate-arrays [n] | |
| (mapv #(vec (range 1 (inc %))) (range 1 (inc n)))) | |
| (deftest generate-arrays-test | |
| (is (= (generate-arrays 4) [[1] [1 2] [1 2 3] [1 2 3 4]])) | |
| (is (= (generate-arrays 1) [[1]]))) |
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
| (ns num-balanced | |
| (:require [clojure.test :refer [deftest is])) | |
| (defn paren-as-num [c] | |
| (case c | |
| \( 1 | |
| \) -1 | |
| 0)) | |
| (defn num-balanced [s-of-parens] |
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
| (ns repeated-groups | |
| (:require [clojure.test :refer [deftest is])) | |
| (defn repeated-groups [nums] | |
| (->> nums | |
| (partition-by identity) | |
| (filter #(> (count %) 1)) | |
| (into []))) | |
| ;; Using transducers instead which gives |
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
| (ns area | |
| (:require | |
| [clojure.math :as math] | |
| [criterium.core :as crit])) | |
| (defmulti area :shape) | |
| (defmethod area :square [{:keys [side]}] | |
| (* side side)) |
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
| #!/usr/bin/env bb | |
| ;; You need to have an OpenAI API key and set it like: | |
| ;; export OPENAI_API_KEY=<your key> in your .bashrc | |
| ;; | |
| ;; Make sure you have babashka installed | |
| ;; https://github.com/babashka/babashka#installation | |
| ;; | |
| ;; One way to run this is to install bbin | |
| ;; https://github.com/babashka/bbin |
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
| (ns scramble | |
| (:require [clojure.string :as string])) | |
| (defn scramble-word [w] | |
| (let [middle-letters (subs w 1 (dec (count w))) | |
| mixed-middle (shuffle (seq middle-letters))] | |
| (str (first w) (apply str mixed-middle) (last w)))) | |
| (defn scramble [s] | |
| (string/join " " |
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
| (ns roll-dice | |
| (:require | |
| [clojure.string :as string] | |
| [clojure.test :refer [deftest is])) | |
| (defn roll-dice [dice-str] | |
| (let [roll-die (fn [die-size] | |
| (inc (rand-int die-size)))] | |
| (->> (string/split dice-str #"\+") ;; ["1d8" "2d10"] | |
| (map #(string/split % #"d")) ;; (["1" "8"] ["2" "10"]) |
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
| pub fn binary_pal(n: i32) -> bool { | |
| let s = format!("{n:b}"); | |
| s == s.chars().rev().collect::<String>() | |
| } | |
| #[test] | |
| fn binary_pal_test() { | |
| assert_eq!(binary_pal(5), true); | |
| assert_eq!(binary_pal(10), false); |
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
| (ns explode-string | |
| (:require | |
| [clojure.string :as string] | |
| [clojure.test :refer [deftest is])) | |
| (defn explode-string [s] | |
| (->> (group-by identity s) | |
| vals | |
| (map (partial apply str)) | |
| (remove string/blank?) |