Last active
May 17, 2023 00:44
-
-
Save chase-lambert/c5347e3ad35509182977dc86af45c416 to your computer and use it in GitHub Desktop.
rendezvous with cassidoo challenge: 22.07.31
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 num-of-ones | |
(:require [clojure.test :refer [deftest is]])) | |
(defn n->digits | |
"ex. (n->digits 14) => [1 4]" | |
[n] | |
(if (< n 10) | |
[n] | |
(conj (n->digits (quot n 10)) (rem n 10)))) | |
(defn number-of-ones [n] | |
(->> (range (inc n)) | |
(mapcat n->digits) | |
(filter #(= 1 %)) | |
count)) | |
(deftest number-of-ones-test | |
(is (= 7 (number-of-ones 14)))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Various transducer versions that run almost twice as fast for a large input number: