10000 🟢 Solve problem 412 (Clojure) · AF2B/Leetcode@a527d9c · GitHub
[go: up one dir, main page]

Skip to content

Commit a527d9c

Browse files
committed
🟢 Solve problem 412 (Clojure)
1 parent deda5c5 commit a527d9c

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

‎clojure/412.clj

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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)

0 commit comments

Comments
 (0)
0