-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsquare_code.clj
48 lines (41 loc) · 1.27 KB
/
square_code.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
(ns otus-02.homework.square-code
(:require [clojure.math :as math]
[clojure.string :as str]))
(defn- pad-right [len val col]
(take len (concat col (repeat val))))
(defn- get-col-width [input-string]
(-> input-string
(count)
(math/sqrt)
(math/ceil)
(int)))
(defn- prepare-str [input]
(-> input
(str/replace #"[\W]{1,}" "")
(str/lower-case)))
(defn- str->matrix [input]
(let [len (get-col-width input)]
(->> input
(partition-all len)
(map (fn [lst] (pad-right len " " lst))))))
(defn encode-string [input]
(->> input
(prepare-str)
(str->matrix)
(apply mapv str)
(str/join " ")))
(defn decode-string [ciphertext]
(let [normalized (str/replace ciphertext #"([\w]{1,})([\W]{1,1})" "$1")
length (count normalized)
side (Math/sqrt length)
rows (Math/floor side)
cols (Math/ceil side)]
(str/join
(apply concat (for [i (range rows)]
(for [j (range cols)]
(let [value
(get normalized
(int (+ (* j rows) i))
"")]
(when-not (= value \space)
value))))))))