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() | |
} |
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 max_gap(nums: &[i32]) -> i32 { | |
if nums.len() < 2 { | |
return 0; | |
} | |
let mut nums = nums.to_vec(); | |
nums.sort(); | |
nums.windows(2).map(|w| w[1] - w[0]).max().unwrap_or(0) | |
} |
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 remove_digit(n: i32, digit: i32) -> i32 { | |
let digit_char = char::from_digit(digit as u32, 10).unwrap(); | |
let n_str = n.to_string(); | |
let mut max_num = 0; | |
for (i, c) in n_str.char_indices() { | |
if c == digit_char { | |
let new_num_str = [&n_str[..i], &n_str[i + 1..]].concat(); | |
let new_num = new_num_str.parse::<i32>().unwrap(); |
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 enum Direction { | |
Horizontal, | |
Vertical, | |
} | |
pub fn flip<T>(arr: Vec<Vec<T>>, direction: Direction) -> Vec<Vec<T>> { | |
match direction { | |
Direction::Horizontal => arr | |
.into_iter() | |
.map(|row| row.into_iter().rev().collect()) |
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 between-nums | |
(:require [clojure.test :refer [deftest is]])) | |
(defn between-nums [a b op] | |
(let [[a b] (sort [a b]) | |
nums (range (inc a) b) | |
prime? (fn [n] (when (> n 1) | |
(empty? | |
(filter #(= 0 (mod n %)) (range 2 n)))))] | |
(case op |
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 do-tasks | |
(:require [clojure.test :refer [deftest is]])) | |
(defn do-tasks [tasks time-to-work] | |
(let [time-remaining (atom time-to-work) | |
sorted-tasks (sort-by :duration tasks) | |
completed-tasks (set | |
(reduce (fn [completed-tasks next-task] | |
(if (>= @time-remaining (:duration next-task)) | |
(do |
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 score-word-game | |
(:require [clojure.test :refer [deftest is]])) | |
(defn score-word-game [word-list letter-scores] | |
(let [word-score (fn [word] | |
(let [letter-score (reduce + (map letter-scores word))] | |
(* (count word) letter-score)))] | |
(apply max-key word-score word-list))) | |
(deftest score-word-game-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 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)])) | |
[[] []] |
NewerOlder