From c49d7af2f5dd2d754c0f0a5683c73fdfdaaf5769 Mon Sep 17 00:00:00 2001 From: Vadych Date: Sat, 29 Apr 2023 22:26:42 +0300 Subject: [PATCH 1/6] Change gitignore for Calva --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 98b40fa..0574b15 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,6 @@ pom.xml.asc *.iml .hgignore .hg/ +.clj-kondo +.calva +.lsp From 0cb5b6e5bd330e07f18d39263208212e6086d703 Mon Sep 17 00:00:00 2001 From: Vadych Date: Tue, 2 May 2023 17:47:18 +0300 Subject: [PATCH 2/6] gitignore --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index ff28705..e461387 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,6 @@ pom.xml.asc *.iml .hgignore .hg/ -clj-kondo +.clj-kondo .calva .lsp \ No newline at end of file From 5db689dae5c701bcce35cc0bb4e7f63b6bbcefaf Mon Sep 17 00:00:00 2001 From: Vadych Date: Sat, 29 Apr 2023 22:26:42 +0300 Subject: [PATCH 3/6] Change gitignore for Calva --- .gitignore | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index ff28705..0574b15 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,6 @@ pom.xml.asc *.iml .hgignore .hg/ -clj-kondo +.clj-kondo .calva -.lsp \ No newline at end of file +.lsp From 9fbbfdd91fc3fa90df1ab8068c0c2f08e41b41b5 Mon Sep 17 00:00:00 2001 From: Vadych Date: Fri, 5 May 2023 18:35:47 +0300 Subject: [PATCH 4/6] add .DS_Store to gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 1e50fc6..adfae2d 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ pom.xml.asc .clj-kondo .calva .lsp +.DS_Store \ No newline at end of file From 76af3b7fc6a897f02b99da2ac7a3e551e2cbb590 Mon Sep 17 00:00:00 2001 From: Vadych Date: Wed, 10 May 2023 09:57:50 +0300 Subject: [PATCH 5/6] add .DS_Store to .gitignore --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 From 7043672f216d2dd9f721cdb8c24fe57380df5126 Mon Sep 17 00:00:00 2001 From: Vadych Date: Wed, 10 May 2023 14:14:27 +0300 Subject: [PATCH 6/6] dz2 is done --- otus-02/src/otus_02/homework/common_child.clj | 14 ++++++- otus-02/src/otus_02/homework/palindrome.clj | 14 ++++++- otus-02/src/otus_02/homework/pangram.clj | 9 ++++- otus-02/src/otus_02/homework/square_code.clj | 37 +++++++++++++++++-- 4 files changed, 66 insertions(+), 8 deletions(-) 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