Skip to content

Instantly share code, notes, and snippets.

View ndonolli's full-sized avatar

Nathan Donolli ndonolli

View GitHub Profile
(defn binary-search [target coll]
(loop [tree coll]
(let [idx (int (/ (count tree) 2))
node (nth tree idx)]
(cond
(= target node) true
(= 1 (count tree)) false
(< target node) (recur (subvec tree 0 idx))
(> target node) (recur (subvec tree idx (count tree)))))))
@ndonolli
ndonolli / factors-string.clj
Created August 11, 2020 17:55
factors->string
(defn factors->string [factors]
(->> (frequencies factors)
(map (fn [[n exp]]
(if (= 1 exp) (str n) (str n "^" exp))))
(interpose " x ")
(cons (str (apply * factors) " = "))
(apply str)))
(defn uniques [coll]
(let [freqs (frequencies coll)]
(filter #(= 1 (get freqs %)) coll)))
(defn contraindications [meds pairs]
(let [meds-set (set (map :rxNorm meds))
contains-pair? (partial every? meds-set)]
(->> pairs (filter contains-pair?) not-empty)))
@ndonolli
ndonolli / sudoku-validator.clj
Last active May 14, 2020 21:51
sudoku-validator
(defn valid-set? [s]
(= (sort s) (range 1 10)))
(defn rotate [matrix]
(apply map list matrix))
(defn rows [matrix] matrix)
(defn cols [matrix] (rotate matrix))
(defn check-path-win
"Checks a path for a win"
[s]
(if (apply = s)
(first s)
nil))
(defn check-win-direction
"Checks a sequence of paths in a particular direction for a win"
[dir-seq]
; Write a function to determine if two words are anagrams.
(defn is-anagram [target src]
(if (string? target)
(if (= target src)
false
(is-anagram (frequencies target) src))
(= target (frequencies src))))
; Given a dictionary of words (such as this one), find all of the anagrams for a target word.
; Assumption: dictionary of words is a collection
@ndonolli
ndonolli / remove-at.clj
Created April 3, 2019 16:24
Remove nth element from a list or vector
(defn remove-at [n xs]
(let [removed (flatten (conj (drop (+ n 1) xs)(take n xs)))]
(if (vector? xs)
(vec removed)
removed)))
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">