File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change
1
+ (ns clojure.412
2
+ " Leetcode: 412
3
+ Name: Fizz Buzz"
4
+ (:require [clojure.test :refer [deftest is run-tests]]))
5
+
6
+ (defn fizz-buzz [n]
7
+ {:pre [(and (integer? n) (<= 1 n (Math/pow 10 4 )))]}
8
+ (loop [vector-n (range 1 (inc n))
9
+ result []]
10
+ (if (empty? vector-n)
11
+ result
12
+ (let [current-num (first vector-n)
13
+ next-vector (rest vector-n)
14
+ fizz-buzz-result (cond
15
+ (and (zero? (mod current-num 3 ))
16
+ (zero? (mod current-num 5 ))) " FizzBuzz"
17
+ (zero? (mod current-num 3 )) " Fizz"
18
+ (zero? (mod current-num 5 )) " Buzz"
19
+ :else (str current-num))]
20
+ (recur next-vector (conj result fizz-buzz-result))))))
21
+
22
+ (deftest test-fizz-buzz
23
+ (is (= (fizz-buzz 1 ) [" 1" ]))
24
+ (is (= (fizz-buzz 3 ) [" 1" " 2" " Fizz" ]))
25
+ (is (= (fizz-buzz 5 ) [" 1" " 2" " Fizz" " 4" " Buzz" ]))
26
+ (is (= (fizz-buzz 15 ) [" 1" " 2" " Fizz" " 4" " Buzz" " Fizz" " 7" " 8" " Fizz" " Buzz" " 11" " Fizz" " 13" " 14" " FizzBuzz" ]))
27
+ (is (thrown? AssertionError (fizz-buzz 10001 )))
28
+ (is (thrown? AssertionError (fizz-buzz 0 )))
29
+ (is (thrown? AssertionError (fizz-buzz -1 ))))
30
+
31
+ (run-tests )
You can’t perform that action at this time.
0 commit comments