Skip to content

Commit 1d0135f

Browse files
committed
🟡 Solve problem 6 Clojure
1 parent 14f6188 commit 1d0135f

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

‎clojure/6.clj

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
(ns 6
2+
(:require [clojure.test :refer [deftest testing is run-tests]]))
3+
4+
(defn read-input
5+
[]
6+
(slurp "./clojure/input.txt"))
7+
8+
(defn convert
9+
[num-rows]
10+
(let [input (read-input)
11+
n (count input)
12+
result (vec (repeat num-rows ""))]
13+
(loop [i 0
14+
row 0
15+
direction 1
16+
result result]
17+
(if (= i n)
18+
(apply str result)
19+
(let [updated-result (update result row str (nth input i))
20+
new-row (if (or (= row 0) (= row (dec num-rows)))
21+
(+ row direction)
22+
(+ row direction))]
23+
(recur (inc i)
24+
new-row
25+
(if (or (= new-row 0) (= new-row (dec num-rows)))
26+
(- direction)
27+
direction)
28+
updated-result))))))
29+
30+
(deftest zigzag-problem
31+
(testing "should return the zigzag string"
32+
(is (= (convert 3) "PAYPALISHIRING"))
33+
(is (= (convert 4) "PINALSIGYAHRPI"))
34+
(is (= (convert 1) "A"))))

‎clojure/input.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
PAYPALISHIRING
2+
PINALSIGYAHRPI
3+
A

0 commit comments

Comments
 (0)