diff --git a/otus-01/src/otus/homework.clj b/otus-01/src/otus/homework.clj index 82c8e9d..6a75ff5 100644 --- a/otus-01/src/otus/homework.clj +++ b/otus-01/src/otus/homework.clj @@ -1,5 +1,6 @@ (ns otus.homework) -(defn solution "Add your solution as fuction body here" []) +(defn solution "Add your solution as fuction body here" [] + (/ (+ 5.0 4.0 (- 2.0 (- 3.0 (+ 6.0 (/ 4.0 5.0))))) (* 3.0 (- 6.0 2.0) (- 2.0 7.0)))) diff --git a/otus-01/test/otus/homework_test.clj b/otus-01/test/otus/homework_test.clj index 9cc096f..50bb99d 100644 --- a/otus-01/test/otus/homework_test.clj +++ b/otus-01/test/otus/homework_test.clj @@ -4,5 +4,5 @@ (deftest s-expression-solution-test - (is (= (sut/solution) -0.2466666666666667) + (is (= (sut/solution) -0.24666666666666667) "solution function returns a correct answer")) diff --git a/otus-02/src/otus_02/homework/palindrome.clj b/otus-02/src/otus_02/homework/palindrome.clj index 591f98e..a0bef49 100644 --- a/otus-02/src/otus_02/homework/palindrome.clj +++ b/otus-02/src/otus_02/homework/palindrome.clj @@ -1,6 +1,11 @@ (ns otus-02.homework.palindrome (:require [clojure.string :as string])) - -(defn is-palindrome [test-string]) - +(defn is-palindrome + "Func for check strings on palyndrome" + [s] + {:pre [(not= s nil)]} + (let [test-string (as-> s $ + (string/lower-case $) + (string/replace $ #"[^a-z]" ""))] + (= test-string (str/reverse test-string)))) diff --git a/otus-02/src/otus_02/homework/pangram.clj b/otus-02/src/otus_02/homework/pangram.clj index dc5d34c..5f989fc 100644 --- a/otus-02/src/otus_02/homework/pangram.clj +++ b/otus-02/src/otus_02/homework/pangram.clj @@ -1,6 +1,11 @@ (ns otus-02.homework.pangram (:require [clojure.string :as string])) - -(defn is-pangram [test-string]) - +(defn is-pangram [test-string] + "Func for check strings on pangram" + {:pre [(not= test-string nil)]} + (let [set-chars (-> test-string + string/lower-case + (string/replace #"[^a-z]" "") + set)] + (= (count set-chars) 26))) diff --git a/otus-02/src/otus_02/homework/square_code.clj b/otus-02/src/otus_02/homework/square_code.clj index 823a185..d4b5c41 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 [clojure.string :as string]) + (:require [clojure.math :as math])) ;; Реализовать классический метод составления секретных сообщений, называемый `square code`. ;; Выведите закодированную версию полученного текста. @@ -48,9 +50,31 @@ "aohghn " "sseoau " +(defn add-spaces [amount line] + (apply str (concat line (repeat amount " ")))) +(defn encode-string [input] + ;; убираем знаки препинания, пробелы и переводим в нижний регистр + ;; а после переводим строку в вектор (line) + (let [inner (-> input + (string/lower-case) + (string/replace #"[^a-z]" "")) + size (count inner) + rows (int (math/sqrt (count inner))) + columns (inc rows) + line (->> inner + ;; добавляем пробелы к концу строки + (add-spaces (- (* columns rows) size)) + (vec))] -(defn encode-string [input]) - - -(defn decode-string [input]) + (string/join " " (map #(apply str %) + (partition rows + (replace line + (vec (loop [start 0 + rows 0 + l () + max (count line) + step columns] + (if (> rows (dec step)) + l + (recur (inc start) (inc rows) (concat l (range start max step)) max step))))))))))