From 10d99fd0b579115219ac9c946deb06cb7ecc4824 Mon Sep 17 00:00:00 2001 From: Andrey Boriskin Date: Sun, 5 Nov 2023 00:17:40 +0300 Subject: [PATCH 1/2] First houmwork --- otus-01/src/otus/homework.clj | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/otus-01/src/otus/homework.clj b/otus-01/src/otus/homework.clj index 82c8e9d..516d6e7 100644 --- a/otus-01/src/otus/homework.clj +++ b/otus-01/src/otus/homework.clj @@ -1,5 +1,11 @@ (ns otus.homework) -(defn solution "Add your solution as fuction body here" []) +(defn solution + "Add your solution as function body here" + [] + (double + (/ + (+ 5 4 (- 2 (- 3 (+ 6 (/ 4 5))))) + (* 3 (- 6 2) (- 2 7))))) From 22603a016ca0ea5b462f2aeed47926fd73000ad8 Mon Sep 17 00:00:00 2001 From: Andrey Boriskin Date: Sun, 10 Dec 2023 16:17:25 +0300 Subject: [PATCH 2/2] HW2 Boriskin Andrey done --- otus-02/src/otus_02/homework/common_child.clj | 43 ++++++++++++++++--- otus-02/src/otus_02/homework/palindrome.clj | 9 +++- otus-02/src/otus_02/homework/pangram.clj | 10 ++++- 3 files changed, 52 insertions(+), 10 deletions(-) diff --git a/otus-02/src/otus_02/homework/common_child.clj b/otus-02/src/otus_02/homework/common_child.clj index f1474f6..da2917e 100644 --- a/otus-02/src/otus_02/homework/common_child.clj +++ b/otus-02/src/otus_02/homework/common_child.clj @@ -1,4 +1,6 @@ -(ns otus-02.homework.common-child) +(ns otus-02.homework.common-child + (:require [clojure.string :as s] + [clojure.set :refer [intersection]])) ;; Строка называется потомком другой строки, @@ -15,8 +17,37 @@ ;; Еще пример HARRY и SALLY. Ответ будет - 2, так как общий элемент у них AY - -(defn common-child-length [first-string second-string]) - - - +(defn combinations + "Ищем все возможные комбинации потомков в сторке с учетом порядка следования букв" + [letters] + (loop [result nil + rest-result letters] + (if (empty? rest-result) + #{result} + (let [current (first rest-result) + next-result (conj result current) + combinations-without (combinations (rest rest-result)) + combinations-with (mapv #(str current %) combinations-without)] + (set (concat combinations-with (combinations (rest rest-result)))))))) + +(defn delete-not-repeat-letter + "Удаляем не повторяющиеся буквы в двух строках" + [first-string second-string] + (filterv identity + (map (fn [x] + (if (s/includes? second-string x) + x)) first-string))) + +(defn common-child-length + "Вычисляем максимального потомка" + [first-string second-string] + (let [s1 (re-seq #"[\w+]" first-string) + s2 (re-seq #"[\w+]" second-string) + s1-not-repeat (delete-not-repeat-letter s1 s2) + s2-not-repeat (delete-not-repeat-letter s2 s1)] + (reduce max + (map count + (some-> (intersection (combinations s1-not-repeat) (combinations s2-not-repeat))))))) + +;;(time (common-child-length "HARRY" "SALLY")) +;;=> "Elapsed time: 0.132041 msecs" diff --git a/otus-02/src/otus_02/homework/palindrome.clj b/otus-02/src/otus_02/homework/palindrome.clj index 591f98e..7e4ae9b 100644 --- a/otus-02/src/otus_02/homework/palindrome.clj +++ b/otus-02/src/otus_02/homework/palindrome.clj @@ -2,5 +2,10 @@ (:require [clojure.string :as string])) -(defn is-palindrome [test-string]) - +(defn is-palindrome + [test-string] + (as-> test-string p + (re-seq #"[\w+]" p) + (string/join "" p) + (string/lower-case p) + (= (string/reverse p) p))) diff --git a/otus-02/src/otus_02/homework/pangram.clj b/otus-02/src/otus_02/homework/pangram.clj index dc5d34c..d677c7f 100644 --- a/otus-02/src/otus_02/homework/pangram.clj +++ b/otus-02/src/otus_02/homework/pangram.clj @@ -2,5 +2,11 @@ (:require [clojure.string :as string])) -(defn is-pangram [test-string]) - +(defn is-pangram + [test-string] + (as-> test-string p + (string/lower-case p) + (re-seq #"[\w+]" p) + (set p) + (string/join "" p) + (= (count p) 26)))