File tree Expand file tree Collapse file tree 2 files changed +37
-0
lines changed
Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change 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" ))))
Original file line number Diff line number Diff line change 1+ PAYPALISHIRING
2+ PINALSIGYAHRPI
3+ A
You can’t perform that action at this time.
0 commit comments