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-call | |
(:require [clojure.test :refer [deftest is]])) | |
(defn roll-call [names] | |
(->> names | |
(map reverse) | |
(map (partial apply str)) | |
sort)) | |
(deftest roll-call-test |
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 max-the-stock | |
(:require [clojure.test :refer [deftest is]])) | |
(defn max-the-stock [prices] | |
(->> (rest prices) | |
(reduce (fn [[min-price max-profit] price] | |
[(min min-price price) | |
(max max-profit (- price min-price))]) | |
[(first prices) 0]) | |
second)) |
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 group-anagrams | |
(:require [clojure.test :refer [deftest is]])) | |
(defn group-anagrams [words] | |
(vals (group-by set words))) | |
(deftest group-anagrams-test | |
(is (= (group-anagrams ["eat" "tea" "tan" "ate" "nat" "bat"]) | |
[["eat" "tea" "ate"] ["tan" "nat"] ["bat"]])) | |
(is (= (group-anagrams ["vote" "please"]) |
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 split | |
"Prompt: Implement your own String split() function | |
in your preferred programming language. | |
My solution is only for single character separators | |
so is not nearly as useful as Clojure's version | |
allowing for regexes." | |
(:require [clojure.test :refer [deftest is]])) | |
(defn split [s sep] |
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 execution-times | |
"assumes each task/function is only run once | |
and logs are clean so that the start and end | |
times of each task show up consecutively in | |
their respective order." | |
(:require [clojure.test :refer [deftest is]])) | |
(defn calculate-execution-times [logs] | |
(reduce (fn [acc [task1 task2]] | |
(let [task (:name task1) |
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 squares | |
(:require [clojure.test :refer [deftest is]] | |
[criterium.core :as cc])) | |
(defn squares [n] | |
(let [nums (range 1 (inc n))] | |
(->> nums | |
(map #(* % %)) | |
(reduce +)))) |
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 fruit-stand | |
"Example stand -> {\"apple\" {:name \"apple\" :quantity 10 :price 0.5} | |
\"banana\" {:name \"banana\" :quantity 5 :price 0.2}}\n | |
Input for `add-fruit` and `update-quantity` would be validated (parsed) | |
at the call source, hence no error handling in the functions themselves.") | |
(defonce stand (atom {})) | |
(defn add-fruit [{:keys [name] :as fruit}] | |
(swap! stand assoc name fruit)) |
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 fix-inverted-punc | |
(:require [clojure.test :refer [deftest is])) | |
(defn fix-inverted-punc [s] | |
(let [sentences (->> (re-seq #".+?[.!?]" s) | |
(map str/trim))] | |
(->> sentences | |
(map (fn [sentence] | |
(let [first-char (first sentence) | |
last-char (last sentence)] |
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 max-product | |
(:require [clojure.test :refer [deftest is]])) | |
(defn max-product [nums] | |
(->> (sort nums) | |
(take-last 3) | |
(reduce * 1))) | |
(deftest max-product-test | |
(is (= 72 (max-product [2 4 1 3 -5 6])))) |
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 unique_sum(nums: &[i32]) -> i32 { | |
nums.iter() | |
.filter(|&&n| { | |
let mut chars_set = std::collections::HashSet::new(); | |
// `insert(t)` on a hashset returns false if `t` already exists. | |
n.to_string().chars().all(|c| chars_set.insert(c)) | |
}) | |
.sum() | |
} |
NewerOlder