aboutsummaryrefslogtreecommitdiff
path: root/challenge-072
diff options
context:
space:
mode:
authorTyler Wardhaugh <tyler.wardhaugh@gmail.com>2020-09-18 11:55:02 -0700
committerTyler Wardhaugh <tyler.wardhaugh@gmail.com>2020-09-18 12:25:22 -0700
commit337a8c3c4beb1997ca189028acf3453d82d73c33 (patch)
tree51cd39cb2af71068dc414024cc5bc757cdaf3004 /challenge-072
parentc01bf46ceba692e26db808cf12a1c6d2d190a835 (diff)
downloadperlweeklychallenge-club-337a8c3c4beb1997ca189028acf3453d82d73c33.tar.gz
perlweeklychallenge-club-337a8c3c4beb1997ca189028acf3453d82d73c33.tar.bz2
perlweeklychallenge-club-337a8c3c4beb1997ca189028acf3453d82d73c33.zip
Task 1
Diffstat (limited to 'challenge-072')
-rw-r--r--challenge-072/tyler-wardhaugh/clojure/src/tw/weekly/c72/t1.clj18
-rw-r--r--challenge-072/tyler-wardhaugh/clojure/test/tw/weekly/c72_test.clj9
2 files changed, 22 insertions, 5 deletions
diff --git a/challenge-072/tyler-wardhaugh/clojure/src/tw/weekly/c72/t1.clj b/challenge-072/tyler-wardhaugh/clojure/src/tw/weekly/c72/t1.clj
index 5aa5ba8d23..daefe5c833 100644
--- a/challenge-072/tyler-wardhaugh/clojure/src/tw/weekly/c72/t1.clj
+++ b/challenge-072/tyler-wardhaugh/clojure/src/tw/weekly/c72/t1.clj
@@ -2,7 +2,21 @@
(:require [clojure.pprint :refer [cl-format]])
(:require [clojure.edn :as edn]))
+(defn fact
+ "Factorial"
+ [n]
+ (->> n inc (range 1) (reduce *)))
+
+(defn trailing-zeros
+ "Count the trailing 0s in a number"
+ [n]
+ (let [[d cnt] (->> n str reverse (partition-by #(= \0 %)) first ((juxt first count)))]
+ (if (= d \0) cnt 0)))
+
(defn -main
- ""
+ "Run Task 1 with a number N, defaulting to 10"
[& args]
- )
+ (let [n (or (some-> args first edn/read-string) 10)
+ n! (fact n)
+ zs (trailing-zeros n!)]
+ (cl-format true "~a! = ~a has ~a trailing zero~:p" n n! zs)))
diff --git a/challenge-072/tyler-wardhaugh/clojure/test/tw/weekly/c72_test.clj b/challenge-072/tyler-wardhaugh/clojure/test/tw/weekly/c72_test.clj
index 0ad8d5b8ee..40f0c39e7a 100644
--- a/challenge-072/tyler-wardhaugh/clojure/test/tw/weekly/c72_test.clj
+++ b/challenge-072/tyler-wardhaugh/clojure/test/tw/weekly/c72_test.clj
@@ -1,11 +1,14 @@
(ns tw.weekly.c72-test
- (:require [clojure.test :refer [deftest is testing]]
- [tw.weekly.c72.t1 :refer []]
+ (:require [clojure.test :refer [are deftest is testing]]
+ [tw.weekly.c72.t1 :refer [fact trailing-zeros]]
[tw.weekly.c72.t2 :refer []]))
(deftest task-1
(testing "Task 1, Trailing Zeroes"
- ))
+ (are [n e] (= (trailing-zeros (fact n)) e)
+ 10 2
+ 7 1
+ 4 0)))
(deftest task-2
(testing "Task 2, Lines Range"