diff --git a/.gitignore b/.gitignore index c909fb8..adfae2d 100644 --- a/.gitignore +++ b/.gitignore @@ -15,4 +15,5 @@ pom.xml.asc .hg/ .clj-kondo .calva -.lsp \ No newline at end of file +.lsp +.DS_Store \ No newline at end of file diff --git a/otus-02/src/otus_02/homework/common_child.clj b/otus-02/src/otus_02/homework/common_child.clj index 04c5e8a..4fc4533 100644 --- a/otus-02/src/otus_02/homework/common_child.clj +++ b/otus-02/src/otus_02/homework/common_child.clj @@ -16,4 +16,16 @@ ;; Еще пример HARRY и SALLY. Ответ будет - 2, так как общий элемент у них AY -(defn common-child-length [fist-string second-string]) +(defn common-child-length [first-string second-string] + (cond + (or (empty? first-string) (empty? second-string)) 0 + (= (last first-string) (last second-string)) + ( + 1 (common-child-length + (subs first-string 0 (- (count first-string) 1)) + (subs second-string 0 (- (count second-string) 1)))) + :else (max (common-child-length first-string + (subs second-string 0 (- (count second-string) 1))) + (common-child-length (subs first-string 0 (- (count first-string) 1)) + second-string)) + )) + diff --git a/otus-02/src/otus_02/homework/palindrome.clj b/otus-02/src/otus_02/homework/palindrome.clj index dd3d8a4..a674b25 100644 --- a/otus-02/src/otus_02/homework/palindrome.clj +++ b/otus-02/src/otus_02/homework/palindrome.clj @@ -1,4 +1,14 @@ -(ns otus-02.homework.palindrome) +(ns otus-02.homework.palindrome + (:require [clojure.string :as s])) +(def alphabet (set "abcdefghijklmnopqrstuvwxyz")) + +(defn normalize-str [s-not-normal] + (filter some? + (map alphabet + (s/lower-case s-not-normal)))) + +(defn is-palindrome [test-string] + (= (normalize-str test-string) + (normalize-str (s/reverse test-string)))) -(defn is-palindrome [test-string]) diff --git a/otus-02/src/otus_02/homework/pangram.clj b/otus-02/src/otus_02/homework/pangram.clj index cc35c99..d60b5f5 100644 --- a/otus-02/src/otus_02/homework/pangram.clj +++ b/otus-02/src/otus_02/homework/pangram.clj @@ -1,4 +1,9 @@ -(ns otus-02.homework.pangram) +(ns otus-02.homework.pangram + (:require [otus-02.homework.palindrome :refer [normalize-str, alphabet]])) -(defn is-pangram [test-string]) + +(defn is-pangram [test-string] + (= alphabet + (set (normalize-str test-string)))) + diff --git a/otus-02/src/otus_02/homework/square_code.clj b/otus-02/src/otus_02/homework/square_code.clj index 823a185..cd0933f 100644 --- a/otus-02/src/otus_02/homework/square_code.clj +++ b/otus-02/src/otus_02/homework/square_code.clj @@ -1,4 +1,6 @@ -(ns otus-02.homework.square-code) +(ns otus-02.homework.square-code + (:require [otus-02.homework.palindrome :refer [normalize-str]] + [clojure.string :as s])) ;; Реализовать классический метод составления секретных сообщений, называемый `square code`. ;; Выведите закодированную версию полученного текста. @@ -48,9 +50,38 @@ "aohghn " "sseoau " +(defn size-field [len-str] + (let [x (int (Math/sqrt len-str))] + (cond + (= len-str (* x x)) [x x] + (<= len-str (* x (inc x))) [(inc x) x] + :else [(inc x) (inc x)]))) + -(defn encode-string [input]) +(defn mix [v r] + (for [i (range r)] + (map #(nth % i "") v))) +(defn l->str [l] + (map #(apply str %) l) + ) -(defn decode-string [input]) +(defn encode-string [input] + (let [n-input (normalize-str input) + [r _] (size-field (count n-input)) + v-input (partition r r (cycle " ") n-input) + l-mix (mix v-input r) + s-mix (l->str l-mix)] + (s/join " " s-mix))) + +(encode-string "If man was meant to stay on the ground, god would have given us roots.") + +(defn decode-string [input] + (let [l-input (s/split input #" ") + r (count (get l-input 0)) + v-input (mix l-input r) + s-input (l->str v-input)] + (apply str (normalize-str (s/join s-input))))) + +(decode-string "imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghn sseoau ") \ No newline at end of file