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
(ns separate-and-sort | |
(:require [clojure.test :refer [deftest is]])) | |
(defn separate-and-sort [nums] | |
(let [nums (sort (remove zero? nums))] | |
(reduce (fn [[evens odds] n] | |
(if (even? n) | |
[(conj evens n) odds] | |
[evens (conj odds n)])) | |
[[] []] |
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
(ns min-subs | |
(:require [clojure.test :refer [deftest is]])) | |
(defn min-subs [nums k] | |
(let [subarrays (partition k 1 nums)] | |
(reduce (fn [min-array next-array] | |
(let [[sum-a sum-b] | |
(map (partial apply +) [min-array next-array])] | |
(if (< sum-a sum-b) | |
min-array |
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
(ns faulty-keeb | |
(:require [clojure.test :refer [deftest is]])) | |
(defn faulty-keeb [s] | |
(let [vowels #{\a \e \i \o \u \A \E \I \O \U} | |
vowel? (fn [c] (vowels c))] | |
(reduce (fn [acc c] | |
(if (vowel? c) | |
(apply str (reverse acc)) | |
(str acc c))) |
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
(ns maximum-profit | |
(:require [clojure.test :refer [deftest is])) | |
(defn maximum-profit [prices] | |
(loop [n (first prices) | |
xs (rest prices) | |
acc []] | |
(if (seq xs) | |
(let [max-profit (apply max | |
(map #(- % n) xs))] |
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
(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?) |
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
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 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 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 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 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)) |