From dc6a609b51cfb1de1d1aea2e63953c1673a7c4b6 Mon Sep 17 00:00:00 2001 From: Vadzimz <56486120+Vadzimz@users.noreply.github.com> Date: Thu, 9 Nov 2023 19:11:19 +0300 Subject: [PATCH 01/25] Update homework.clj --- otus-01/src/otus/homework.clj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/otus-01/src/otus/homework.clj b/otus-01/src/otus/homework.clj index 82c8e9d..b81430e 100644 --- a/otus-01/src/otus/homework.clj +++ b/otus-01/src/otus/homework.clj @@ -1,5 +1,5 @@ (ns otus.homework) -(defn solution "Add your solution as fuction body here" []) +(defn solution "Add your solution as fuction body here" [] (/ (= 5 4 (- 2 (- 3 (+ 6 (/ 4 5))))) (* 3 (- 6 2) (- 2 7)))) From e225a894a897b765156c86e9ac1572aa06874c76 Mon Sep 17 00:00:00 2001 From: Vadzimz <56486120+Vadzimz@users.noreply.github.com> Date: Sun, 12 Nov 2023 20:29:08 +0300 Subject: [PATCH 02/25] Update homework.clj --- otus-01/src/otus/homework.clj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/otus-01/src/otus/homework.clj b/otus-01/src/otus/homework.clj index b81430e..f507fc4 100644 --- a/otus-01/src/otus/homework.clj +++ b/otus-01/src/otus/homework.clj @@ -1,5 +1,5 @@ (ns otus.homework) -(defn solution "Add your solution as fuction body here" [] (/ (= 5 4 (- 2 (- 3 (+ 6 (/ 4 5))))) (* 3 (- 6 2) (- 2 7)))) +(defn solution [] (double (/ (+ 5 4 (- 2 (- 3 (+ 6 (/ 4 5))))) (* 3 (- 6 2) (- 2 7))))) From b0f435b9399588abb3ac2b81318e67f6d711d8f1 Mon Sep 17 00:00:00 2001 From: Vadzimz <56486120+Vadzimz@users.noreply.github.com> Date: Sun, 12 Nov 2023 20:36:57 +0300 Subject: [PATCH 03/25] Update palindrome.clj --- otus-02/src/otus_02/homework/palindrome.clj | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/otus-02/src/otus_02/homework/palindrome.clj b/otus-02/src/otus_02/homework/palindrome.clj index 591f98e..dd89398 100644 --- a/otus-02/src/otus_02/homework/palindrome.clj +++ b/otus-02/src/otus_02/homework/palindrome.clj @@ -1,6 +1,5 @@ (ns otus-02.homework.palindrome (:require [clojure.string :as string])) - -(defn is-palindrome [test-string]) +(defn is-palindrome [test-string] (->> test-string string/lower-case (filter #(Character/isLetter %)) (#(= % (reverse %))))) From c4f2530526574eee6ab37a194cbffdce7190f6f0 Mon Sep 17 00:00:00 2001 From: Vadzimz <56486120+Vadzimz@users.noreply.github.com> Date: Sun, 12 Nov 2023 20:40:57 +0300 Subject: [PATCH 04/25] Update pangram.clj --- otus-02/src/otus_02/homework/pangram.clj | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/otus-02/src/otus_02/homework/pangram.clj b/otus-02/src/otus_02/homework/pangram.clj index dc5d34c..b8d09f1 100644 --- a/otus-02/src/otus_02/homework/pangram.clj +++ b/otus-02/src/otus_02/homework/pangram.clj @@ -1,6 +1,4 @@ (ns otus-02.homework.pangram (:require [clojure.string :as string])) - -(defn is-pangram [test-string]) - +(defn is-pangram [test-string] (->> test-string string/lower-case (filter #(Character/isLetter %)) (map int) (into #{}) count (= 26))) From 3a0cb24f4908e86cae7f56442f3631b9bb178ca9 Mon Sep 17 00:00:00 2001 From: Vadzimz <56486120+Vadzimz@users.noreply.github.com> Date: Fri, 17 Nov 2023 16:56:36 +0300 Subject: [PATCH 05/25] Update square_code.clj --- otus-02/src/otus_02/homework/square_code.clj | 29 ++++++++++++++++---- 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/otus-02/src/otus_02/homework/square_code.clj b/otus-02/src/otus_02/homework/square_code.clj index 823a185..7704d23 100644 --- a/otus-02/src/otus_02/homework/square_code.clj +++ b/otus-02/src/otus_02/homework/square_code.clj @@ -1,4 +1,5 @@ -(ns otus-02.homework.square-code) +(ns otus-02.otus-02.homework.square-code +(:require [clojure.math :as math] [clojure.string :as string])) ;; Реализовать классический метод составления секретных сообщений, называемый `square code`. ;; Выведите закодированную версию полученного текста. @@ -48,9 +49,25 @@ "aohghn " "sseoau " +(defn encode-string [input] +(let [x (->> input string/lower-case (filter #(Character/isLetter %)))] +(->> x count math/sqrt +(#(list (math/floor %) (math/round %) (math/ceil %))) +pop (map int) +(#(list (inc (first %)) (second %))) +(#(repeat (first %) (range (second %)))) +(apply concat) +(map vector (concat x (repeat \space))) +(sort-by second) (map first) +drop-last (apply str)) +)) - -(defn encode-string [input]) - - -(defn decode-string [input]) +(defn decode-string [input] +(->> (range) (map #(vector % %)) +(apply concat) (#(map vector % (drop 1 %))) +(drop-while #(< (reduce * %) (count input))) +first (#(repeat (first %) (range (second %)))) +(apply concat)(map vector (concat input (repeat \space))) +(sort-by second ) (map first) +(apply str) (string/trim) +)) From 370d245d74f582a016a36d9ea43fbce3006da9ae Mon Sep 17 00:00:00 2001 From: Vadzimz <56486120+Vadzimz@users.noreply.github.com> Date: Tue, 21 Nov 2023 16:59:45 +0300 Subject: [PATCH 06/25] Update magic_square.clj --- otus-04/src/otus_04/homework/magic_square.clj | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/otus-04/src/otus_04/homework/magic_square.clj b/otus-04/src/otus_04/homework/magic_square.clj index c90f3bf..5501c34 100644 --- a/otus-04/src/otus_04/homework/magic_square.clj +++ b/otus-04/src/otus_04/homework/magic_square.clj @@ -6,12 +6,24 @@ ;; Подсказка: используйте "Siamese method" ;; https://en.wikipedia.org/wiki/Siamese_method -(defn magic-square + "Функция возвращает вектор векторов целых чисел, описывающий магический квадрат размера n*n, где n - нечётное натуральное число. Магический квадрат должен быть заполнен так, что суммы всех вертикалей, горизонталей и диагоналей длиной в n должны быть одинаковы." - [n] - [[0]]) + +;; r = 1..n MagSq = n * mod(r' + (r - div(n + 3, 2)), n) + mod(r' + r * 2 - 2, n) + 1 + +(defn magic-square [n] +{:pre [(odd? n)]} +(let [rng (->> n range (map inc)) + repeat-rng (fn [x] (flatten (repeat n x))) + sum-row-col (fn [x y] (map + (sort (repeat-rng x)) (repeat-rng y))) + f-main (fn [x] (map #(mod % n) (sum-row-col rng x))) + b1 (map (partial + (- (quot (+ n 3), 2))) rng) + b2 (map (partial #(- (* 2 %) 2)) rng)] +(->> (map + (map (partial * n) (f-main b1)) (f-main b2)) + (map inc) (partition n) (map vec) vec) +)) From 7db6bbb9eeee8111349dcf522f48e4652e24b8f4 Mon Sep 17 00:00:00 2001 From: Vadzimz <56486120+Vadzimz@users.noreply.github.com> Date: Tue, 21 Nov 2023 17:02:38 +0300 Subject: [PATCH 07/25] Update scramblies.clj --- otus-04/src/otus_04/homework/scramblies.clj | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/otus-04/src/otus_04/homework/scramblies.clj b/otus-04/src/otus_04/homework/scramblies.clj index f76d335..7aaad1f 100644 --- a/otus-04/src/otus_04/homework/scramblies.clj +++ b/otus-04/src/otus_04/homework/scramblies.clj @@ -7,4 +7,6 @@ "Функция возвращает true, если из букв в строке letters можно составить слово word." [letters word] - nil) + (let [fun (fn [x] (->> (set word) (map #(get (frequencies x) % 0)))) + [x y] (->> [letters word] (map fun))] + (->> (map >= x y) (reduce #(and %1 %2) true)))) From 87105d362d62f207e22ee8b1f022b255903005c2 Mon Sep 17 00:00:00 2001 From: Vadzimz <56486120+Vadzimz@users.noreply.github.com> Date: Wed, 22 Nov 2023 12:49:56 +0300 Subject: [PATCH 08/25] Update square_code.clj --- otus-02/src/otus_02/homework/square_code.clj | 47 ++++++++++++-------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/otus-02/src/otus_02/homework/square_code.clj b/otus-02/src/otus_02/homework/square_code.clj index 7704d23..3ebdd0e 100644 --- a/otus-02/src/otus_02/homework/square_code.clj +++ b/otus-02/src/otus_02/homework/square_code.clj @@ -50,24 +50,33 @@ "sseoau " (defn encode-string [input] -(let [x (->> input string/lower-case (filter #(Character/isLetter %)))] -(->> x count math/sqrt -(#(list (math/floor %) (math/round %) (math/ceil %))) -pop (map int) -(#(list (inc (first %)) (second %))) -(#(repeat (first %) (range (second %)))) -(apply concat) -(map vector (concat x (repeat \space))) -(sort-by second) (map first) -drop-last (apply str)) -)) + (let [x (->> input string/lower-case + (filter #(Character/isLetter %)))] + (->> x + count + math/sqrt + (#(list (math/floor %) (math/round %) (math/ceil %))) + pop + (#(list (inc (first %)) (second %))) + (#(repeat (first %) (range (second %)))) + flatten + (map vector (concat x (repeat \space))) + (sort-by second) + (map first) + drop-last + (apply str)))) (defn decode-string [input] -(->> (range) (map #(vector % %)) -(apply concat) (#(map vector % (drop 1 %))) -(drop-while #(< (reduce * %) (count input))) -first (#(repeat (first %) (range (second %)))) -(apply concat)(map vector (concat input (repeat \space))) -(sort-by second ) (map first) -(apply str) (string/trim) -)) + (->> (range) + (map #(vector % %)) + (apply concat) + (#(map vector % (drop 1 %))) + (drop-while #(< (reduce * %) (count input))) + first + (#(repeat (first %) (range (second %)))) + flatten + (map vector (concat input (repeat \space))) + (sort-by second ) + (map first) + (apply str) + (string/trim))) From 20df1325ad60d225ba16f393da2cfe5fe315d14c Mon Sep 17 00:00:00 2001 From: Vadzimz <56486120+Vadzimz@users.noreply.github.com> Date: Wed, 22 Nov 2023 12:52:00 +0300 Subject: [PATCH 09/25] Update magic_square.clj --- otus-04/src/otus_04/homework/magic_square.clj | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/otus-04/src/otus_04/homework/magic_square.clj b/otus-04/src/otus_04/homework/magic_square.clj index 5501c34..071f426 100644 --- a/otus-04/src/otus_04/homework/magic_square.clj +++ b/otus-04/src/otus_04/homework/magic_square.clj @@ -17,13 +17,17 @@ ;; r = 1..n MagSq = n * mod(r' + (r - div(n + 3, 2)), n) + mod(r' + r * 2 - 2, n) + 1 (defn magic-square [n] -{:pre [(odd? n)]} -(let [rng (->> n range (map inc)) - repeat-rng (fn [x] (flatten (repeat n x))) - sum-row-col (fn [x y] (map + (sort (repeat-rng x)) (repeat-rng y))) - f-main (fn [x] (map #(mod % n) (sum-row-col rng x))) - b1 (map (partial + (- (quot (+ n 3), 2))) rng) - b2 (map (partial #(- (* 2 %) 2)) rng)] -(->> (map + (map (partial * n) (f-main b1)) (f-main b2)) - (map inc) (partition n) (map vec) vec) -)) + {:pre [(odd? n)]} + (let [rng (->> n + range + (map inc)) + repeat-rng (fn [x] (flatten (repeat n x))) + sum-row-col (fn [x y] (map + (sort (repeat-rng x)) (repeat-rng y))) + f-main (fn [x] (map #(mod % n) (sum-row-col rng x))) + b1 (map (partial + (- (quot (+ n 3), 2))) rng) + b2 (map (partial #(- (* 2 %) 2)) rng)] + (->> (map + (map (partial * n) (f-main b1)) (f-main b2)) + (map inc) + (partition n) + (map vec) + vec))) From 51eb7a5a7b69441881ce4bb3e7d4cc92cd1da6ca Mon Sep 17 00:00:00 2001 From: Vadzimz <56486120+Vadzimz@users.noreply.github.com> Date: Wed, 22 Nov 2023 12:58:07 +0300 Subject: [PATCH 10/25] Update scramblies.clj --- otus-04/src/otus_04/homework/scramblies.clj | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/otus-04/src/otus_04/homework/scramblies.clj b/otus-04/src/otus_04/homework/scramblies.clj index 7aaad1f..682534a 100644 --- a/otus-04/src/otus_04/homework/scramblies.clj +++ b/otus-04/src/otus_04/homework/scramblies.clj @@ -7,6 +7,9 @@ "Функция возвращает true, если из букв в строке letters можно составить слово word." [letters word] - (let [fun (fn [x] (->> (set word) (map #(get (frequencies x) % 0)))) - [x y] (->> [letters word] (map fun))] - (->> (map >= x y) (reduce #(and %1 %2) true)))) + (let [f-freq (fn [x] (->> (set word) + (map #(get (frequencies x) % 0)))) + [x y] (->> [letters word] + (map f-freq))] + (->> (map >= x y) + (reduce #(and %1 %2) true)))) From ecad7f63470bf42dbaf305c61c62957d5830a314 Mon Sep 17 00:00:00 2001 From: Vadzimz <56486120+Vadzimz@users.noreply.github.com> Date: Wed, 22 Nov 2023 13:01:45 +0300 Subject: [PATCH 11/25] Update magic_square.clj --- otus-04/src/otus_04/homework/magic_square.clj | 8 -------- 1 file changed, 8 deletions(-) diff --git a/otus-04/src/otus_04/homework/magic_square.clj b/otus-04/src/otus_04/homework/magic_square.clj index 071f426..2c25fff 100644 --- a/otus-04/src/otus_04/homework/magic_square.clj +++ b/otus-04/src/otus_04/homework/magic_square.clj @@ -7,15 +7,7 @@ ;; https://en.wikipedia.org/wiki/Siamese_method - "Функция возвращает вектор векторов целых чисел, - описывающий магический квадрат размера n*n, - где n - нечётное натуральное число. - - Магический квадрат должен быть заполнен так, что суммы всех вертикалей, - горизонталей и диагоналей длиной в n должны быть одинаковы." - ;; r = 1..n MagSq = n * mod(r' + (r - div(n + 3, 2)), n) + mod(r' + r * 2 - 2, n) + 1 - (defn magic-square [n] {:pre [(odd? n)]} (let [rng (->> n From aa186a871e8b7e908aa71769d3972eee4cbb0e54 Mon Sep 17 00:00:00 2001 From: Vadzimz <56486120+Vadzimz@users.noreply.github.com> Date: Wed, 22 Nov 2023 15:39:56 +0300 Subject: [PATCH 12/25] Update palindrome.clj --- otus-02/src/otus_02/homework/palindrome.clj | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/otus-02/src/otus_02/homework/palindrome.clj b/otus-02/src/otus_02/homework/palindrome.clj index dd89398..08e0a8f 100644 --- a/otus-02/src/otus_02/homework/palindrome.clj +++ b/otus-02/src/otus_02/homework/palindrome.clj @@ -1,5 +1,8 @@ (ns otus-02.homework.palindrome (:require [clojure.string :as string])) -(defn is-palindrome [test-string] (->> test-string string/lower-case (filter #(Character/isLetter %)) (#(= % (reverse %))))) +(defn is-palindrome [test-string] + (->> test-string string/lower-case + (filter #(Character/isLetter %)) + (#(= % (reverse %))))) From cb495949e60b9465483cc2805e945b3650fd1f15 Mon Sep 17 00:00:00 2001 From: Vadzimz <56486120+Vadzimz@users.noreply.github.com> Date: Wed, 22 Nov 2023 15:41:05 +0300 Subject: [PATCH 13/25] Update pangram.clj --- otus-02/src/otus_02/homework/pangram.clj | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/otus-02/src/otus_02/homework/pangram.clj b/otus-02/src/otus_02/homework/pangram.clj index b8d09f1..e599697 100644 --- a/otus-02/src/otus_02/homework/pangram.clj +++ b/otus-02/src/otus_02/homework/pangram.clj @@ -1,4 +1,10 @@ (ns otus-02.homework.pangram (:require [clojure.string :as string])) -(defn is-pangram [test-string] (->> test-string string/lower-case (filter #(Character/isLetter %)) (map int) (into #{}) count (= 26))) +(defn is-pangram [test-string] + (->> test-string string/lower-case + (filter #(Character/isLetter %)) + (map int) + (into #{}) + count + (= 26))) From e99a55b12b0d904e37e7fdeca11b827ca96b6b50 Mon Sep 17 00:00:00 2001 From: Vadzimz <56486120+Vadzimz@users.noreply.github.com> Date: Wed, 22 Nov 2023 15:56:13 +0300 Subject: [PATCH 14/25] Update scramblies.clj --- otus-04/src/otus_04/homework/scramblies.clj | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/otus-04/src/otus_04/homework/scramblies.clj b/otus-04/src/otus_04/homework/scramblies.clj index 682534a..6f637c1 100644 --- a/otus-04/src/otus_04/homework/scramblies.clj +++ b/otus-04/src/otus_04/homework/scramblies.clj @@ -7,9 +7,6 @@ "Функция возвращает true, если из букв в строке letters можно составить слово word." [letters word] - (let [f-freq (fn [x] (->> (set word) - (map #(get (frequencies x) % 0)))) - [x y] (->> [letters word] - (map f-freq))] - (->> (map >= x y) - (reduce #(and %1 %2) true)))) + (let [f-freq (fn [x] (map #(get (frequencies x) % 0) (set word))) + [x y] (map f-freq [letters word])] + (reduce #(and %1 %2) true (map >= x y)))) From 48cbfdbd10019f66bf93547a0cb74f42e2eb4655 Mon Sep 17 00:00:00 2001 From: Vadzimz <56486120+Vadzimz@users.noreply.github.com> Date: Wed, 22 Nov 2023 16:40:30 +0300 Subject: [PATCH 15/25] Update magic_square.clj --- otus-04/src/otus_04/homework/magic_square.clj | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/otus-04/src/otus_04/homework/magic_square.clj b/otus-04/src/otus_04/homework/magic_square.clj index 2c25fff..7b4ec13 100644 --- a/otus-04/src/otus_04/homework/magic_square.clj +++ b/otus-04/src/otus_04/homework/magic_square.clj @@ -10,15 +10,13 @@ ;; r = 1..n MagSq = n * mod(r' + (r - div(n + 3, 2)), n) + mod(r' + r * 2 - 2, n) + 1 (defn magic-square [n] {:pre [(odd? n)]} - (let [rng (->> n - range - (map inc)) + (let [rng (map inc (range n)) repeat-rng (fn [x] (flatten (repeat n x))) sum-row-col (fn [x y] (map + (sort (repeat-rng x)) (repeat-rng y))) f-main (fn [x] (map #(mod % n) (sum-row-col rng x))) - b1 (map (partial + (- (quot (+ n 3), 2))) rng) - b2 (map (partial #(- (* 2 %) 2)) rng)] - (->> (map + (map (partial * n) (f-main b1)) (f-main b2)) + b1 (map #(- % (quot (+ n 3), 2)) rng) + b2 (map #(- (* % 2) 2) rng)] + (->> (map + (map #(* % n) (f-main b1)) (f-main b2)) (map inc) (partition n) (map vec) From 6b44096c72dedc68117289464343f1c1afb281ad Mon Sep 17 00:00:00 2001 From: Vadzimz <56486120+Vadzimz@users.noreply.github.com> Date: Wed, 22 Nov 2023 18:35:38 +0300 Subject: [PATCH 16/25] Update square_code.clj --- otus-02/src/otus_02/homework/square_code.clj | 21 +++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/otus-02/src/otus_02/homework/square_code.clj b/otus-02/src/otus_02/homework/square_code.clj index 3ebdd0e..4a2372d 100644 --- a/otus-02/src/otus_02/homework/square_code.clj +++ b/otus-02/src/otus_02/homework/square_code.clj @@ -50,17 +50,14 @@ "sseoau " (defn encode-string [input] - (let [x (->> input string/lower-case - (filter #(Character/isLetter %)))] - (->> x - count - math/sqrt - (#(list (math/floor %) (math/round %) (math/ceil %))) - pop - (#(list (inc (first %)) (second %))) - (#(repeat (first %) (range (second %)))) + (let [st (->> input string/lower-case + (filter #(Character/isLetter %))) + sqr (math/sqrt (count st)) + rnd (math/round sqr) + [x y] (if (= (int (math/ceil sqr)) rnd) [(inc rnd) rnd] [rnd rnd])] + (->> (repeat (inc x) (range (inc y))) flatten - (map vector (concat x (repeat \space))) + (map vector (concat st (repeat \space))) (sort-by second) (map first) drop-last @@ -69,12 +66,12 @@ (defn decode-string [input] (->> (range) (map #(vector % %)) - (apply concat) + flatten (#(map vector % (drop 1 %))) (drop-while #(< (reduce * %) (count input))) first (#(repeat (first %) (range (second %)))) - flatten + flatten (map vector (concat input (repeat \space))) (sort-by second ) (map first) From cafbdbcdd2cd5b9beaa0af4249162319b8aa9bce Mon Sep 17 00:00:00 2001 From: Vadzimz <56486120+Vadzimz@users.noreply.github.com> Date: Wed, 22 Nov 2023 18:40:01 +0300 Subject: [PATCH 17/25] Update scramblies.clj --- otus-04/src/otus_04/homework/scramblies.clj | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/otus-04/src/otus_04/homework/scramblies.clj b/otus-04/src/otus_04/homework/scramblies.clj index 6f637c1..3ac94fa 100644 --- a/otus-04/src/otus_04/homework/scramblies.clj +++ b/otus-04/src/otus_04/homework/scramblies.clj @@ -7,6 +7,6 @@ "Функция возвращает true, если из букв в строке letters можно составить слово word." [letters word] - (let [f-freq (fn [x] (map #(get (frequencies x) % 0) (set word))) - [x y] (map f-freq [letters word])] - (reduce #(and %1 %2) true (map >= x y)))) + (let [f-freq (fn [x] (map #(get (frequencies x) % 0) (set word))) + [x y] (map f-freq [letters word])] + (reduce #(and %1 %2) true (map >= x y)))) From a0fc4417b0adf04dcd0df68ac764986ddc6f6067 Mon Sep 17 00:00:00 2001 From: Vadzimz <56486120+Vadzimz@users.noreply.github.com> Date: Wed, 22 Nov 2023 19:23:18 +0300 Subject: [PATCH 18/25] Update square_code.clj --- otus-02/src/otus_02/homework/square_code.clj | 26 ++++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/otus-02/src/otus_02/homework/square_code.clj b/otus-02/src/otus_02/homework/square_code.clj index 4a2372d..22f7555 100644 --- a/otus-02/src/otus_02/homework/square_code.clj +++ b/otus-02/src/otus_02/homework/square_code.clj @@ -64,16 +64,16 @@ (apply str)))) (defn decode-string [input] - (->> (range) - (map #(vector % %)) - flatten - (#(map vector % (drop 1 %))) - (drop-while #(< (reduce * %) (count input))) - first - (#(repeat (first %) (range (second %)))) - flatten - (map vector (concat input (repeat \space))) - (sort-by second ) - (map first) - (apply str) - (string/trim))) + (let [[x y] (->> (range) + (map #(vector % %)) + flatten + (#(map vector % (drop 1 %))) + (drop-while #(< (reduce * %) (count input))) + first)] + (->> (repeat x (range y)) + flatten + (map vector (concat input (repeat \space))) + (sort-by second) + (map first) + (apply str) + (string/trim)))) From c896d8e761a761bbcac15075a5db8c9b17529333 Mon Sep 17 00:00:00 2001 From: Vadzimz <56486120+Vadzimz@users.noreply.github.com> Date: Wed, 22 Nov 2023 20:58:21 +0300 Subject: [PATCH 19/25] Update scramblies.clj --- otus-04/src/otus_04/homework/scramblies.clj | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/otus-04/src/otus_04/homework/scramblies.clj b/otus-04/src/otus_04/homework/scramblies.clj index 3ac94fa..6481c8b 100644 --- a/otus-04/src/otus_04/homework/scramblies.clj +++ b/otus-04/src/otus_04/homework/scramblies.clj @@ -3,10 +3,9 @@ ;; Оригинальная задача: ;; https://www.codewars.com/kata/55c04b4cc56a697bb0000048 -(defn scramble? +(defn scramble? [letters word] "Функция возвращает true, если из букв в строке letters можно составить слово word." - [letters word] - (let [f-freq (fn [x] (map #(get (frequencies x) % 0) (set word))) - [x y] (map f-freq [letters word])] - (reduce #(and %1 %2) true (map >= x y)))) + (let [f-freq (fn [x] (map #(get (frequencies x) % 0) (set word))) + [x y] (map f-freq [letters word])] + (every? true? (map >= x y)))) From dba3c8a5431512028ff4e9df2bf2c4de72dc6193 Mon Sep 17 00:00:00 2001 From: Vadzimz <56486120+Vadzimz@users.noreply.github.com> Date: Fri, 29 Dec 2023 19:48:06 +0300 Subject: [PATCH 20/25] Vadzimz-homework-6 --- otus-06/src/otus_06/homework.clj | 126 ++++++++++++++++++++++++++++++- 1 file changed, 125 insertions(+), 1 deletion(-) diff --git a/otus-06/src/otus_06/homework.clj b/otus-06/src/otus_06/homework.clj index d5d5228..fe6745c 100644 --- a/otus-06/src/otus_06/homework.clj +++ b/otus-06/src/otus_06/homework.clj @@ -1,4 +1,7 @@ -(ns otus-06.homework) +(ns otus-06.homework + (:require [clojure.string :as string]) + (:require [clojure.java.io :as io]) + (:require [clojure.set :as set])) ;; Загрузить данные из трех файлов на диске. ;; Эти данные сформируют вашу базу данных о продажах. @@ -94,3 +97,124 @@ ;; Файлы находятся в папке otus-06/resources/homework + +;; VARS +;;************************************************** + +(def files ["cust.txt" "prod.txt" "sales.txt"]) + +;; запросы +(def menu "\n*** Sales Menu *** +------------------ +1. Display Customer Table +2. Display Product Table +3. Display Sales Table +4. Total Sales for Customer +5. Total Count for Product +6. Exit + +Enter an option\n") + +(def cust-req "Enter a customer name\n") +(def item-req "Enter an item\n") + +;; ключи для чтения и отображения +(def cust-keys [:custID :name :address :phoneNumber]) +(def prod-keys [:prodID :itemDescription :unitCost]) +(def sales-keys [:salesID :custID :prodID :itemCount]) +(def sales-keys-to-show [:salesID :name :itemDescription :itemCount]) + +(def key-names [cust-keys prod-keys sales-keys]) + +;; ключи для конвертации и расчета total-sum +(def keys-to-int [:itemCount :prodID :custID :salesID]) +(def keys-to-double [:unitCost]) +(def keys-to-count-total-sum [:unitCost :itemCount]) + +;; ключи для агрегации +(def keys-to-cust-aggr [:name :totalCost]) +(def keys-to-item-aggr [:itemDescription :itemCount]) + +;; FUNCTIONS +;;********************************************************************** + +(defn read-to-maps + "читаем файл с соответствующими keys в список мап" + [f k] + (let [r (io/reader (io/resource f))] + (->> (line-seq r) + (map #(string/split % #"\|")) + (map #(zipmap k %))))) + +(defn update-mult-vals + "преобразуем несколько столбов (keys), например, конвертируем" + [map vals fun] + (reduce #(update-in % [%2] fun) map vals)) + +(defn add-new-key + "добавляем новый столбец (key), рассчитанный из нескольких существующих" + [map keys name fun] + (assoc map name (reduce fun (vals (select-keys map keys))))) + +(defn select-data + "выделяем несколько столбцов и преобразуем каждую строку к виду мапа (ID - данные)" + [keys-to-select key-to-first coll] + (->> coll + (map #(select-keys % keys-to-select)) + (map #((fn [x key] (merge {(key x) (dissoc x key)})) % key-to-first)))) + +(defn aggregate + "агрегируем данные aggr-key, группируя по name-key" + [[name-key aggr-key] coll] + (->> coll + (map #(select-keys % [name-key aggr-key])) + (group-by name-key) + (map (fn [[name vals]] + {name-key name + aggr-key (reduce + (map aggr-key vals))})) + (map #((fn [x key] {(key x) (dissoc x key)}) % name-key)) + (reduce merge))) + +(defn aggregate-and-filter + "агрегируем и фильтруем результат по запрошенному значению name-key" + [[name-key aggr-key] message coll] + (println message) + (flush) + (let [filter-input (read-line)] + (->> coll + (aggregate [name-key aggr-key]) + (#(find % filter-input))))) + +(defn print-result + "печатаем либо результат, либо уточняющий запрос" + [x] + (if-not (nil? x) + (run! println x) + (println "Precise your input\n"))) + + +;; собираем все данные в одну таблицу (список мап) +(def full-data (->> (map #(read-to-maps %1 %2) files key-names) + (reduce set/join) + (map (fn [x] (update-mult-vals x keys-to-int #(Integer/parseInt %)))) + (map (fn [x] (update-mult-vals x keys-to-double parse-double))) + (map (fn [x] (add-new-key x keys-to-count-total-sum :totalCost *))))) + +(defn main + "результирующая функция" + [coll] + (println menu) + (flush) + (let [x (read-line)] + (println (str x "\n")) + (->> (cond + (= x "1") (select-data cust-keys :custID coll) + (= x "2") (select-data prod-keys :prodID coll) + (= x "3") (select-data sales-keys-to-show :salesID coll) + (= x "4") (aggregate-and-filter keys-to-cust-aggr cust-req coll) + (= x "5") (aggregate-and-filter keys-to-item-aggr item-req coll) + (= x "6") ["Good Bye\n"]) + print-result) + (if (not= x "6") (main coll) nil))) + +(main full-data) From d260dbedd0273dc98495e9558e70e4114089affc Mon Sep 17 00:00:00 2001 From: Vadzimz <56486120+Vadzimz@users.noreply.github.com> Date: Sun, 31 Dec 2023 16:36:47 +0300 Subject: [PATCH 21/25] vadzimz homework 6.clj --- otus-06/src/otus_06/homework.clj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/otus-06/src/otus_06/homework.clj b/otus-06/src/otus_06/homework.clj index fe6745c..a98992b 100644 --- a/otus-06/src/otus_06/homework.clj +++ b/otus-06/src/otus_06/homework.clj @@ -101,7 +101,7 @@ ;; VARS ;;************************************************** -(def files ["cust.txt" "prod.txt" "sales.txt"]) +(def files (map #(str "homework/" %) ["cust.txt" "prod.txt" "sales.txt"])) ;; запросы (def menu "\n*** Sales Menu *** From 61c1fce7f8e6125e04339f9eb82ddd9517073d42 Mon Sep 17 00:00:00 2001 From: Vadzimz <56486120+Vadzimz@users.noreply.github.com> Date: Tue, 16 Jan 2024 09:27:14 +0300 Subject: [PATCH 22/25] Vadzinz homework 10.clj --- otus-10/src/otus_10/homework.clj | 58 +++++++++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 4 deletions(-) diff --git a/otus-10/src/otus_10/homework.clj b/otus-10/src/otus_10/homework.clj index 3d3169d..4c7a258 100644 --- a/otus-10/src/otus_10/homework.clj +++ b/otus-10/src/otus_10/homework.clj @@ -1,6 +1,56 @@ -(ns otus-10.homework) +(ns otus-10-homework.core +(:require [clojure.string :as string]) + (:require [clojure.java.io :as io])) +(def n1 "file-12926-ed090b.mp3") -(defn -main - "I don't do a whole lot ... yet." - [& args]) +(defn check-flag + "Проверяем n-ый бит байта в виде integer" + [num-int ind] + (->> num-int + Integer/toBinaryString + reverse + (#(nth % ind \0)) + (= \1))) + +(defn get-size + "Получаем размер (header, subheader or frame)" + [arr] + (->> (range (count arr)) + (map #(Math/pow 128 %)) + reverse + (map * arr) + (apply +))) + +(defmulti parse-frame first) +(defmethod parse-frame 0 + [coll] (String. (byte-array (remove zero? (next coll))) "Windows-1252")) +(defmethod parse-frame 1 + [coll] (String. (byte-array (remove zero? (next coll))) "UTF-16")) +(defmethod parse-frame 2 + [coll] (String. (byte-array (remove zero? (next coll))) "UTF-16BE")) +(defmethod parse-frame 3 + [coll] (String. (byte-array (remove zero? (next coll))) "UTF-8")) + + +(defn get-frame + "парсим один фрейм" + [stream] + (let [[nm1 nm2 nm3 nm4 sz1 sz2 sz3 sz4 & rest] stream + name (apply str (map char [nm1 nm2 nm3 nm4])) + size (get-size [sz1 sz2 sz3 sz4]) + text (parse-frame (take (+ size 2) rest))] + {:name name :size size :text text})) + +(defn parse-mp3-tags [file] +(with-open [in (io/input-stream (io/file (io/resource file)))] + (let [[_ _ _ _ _ flag-byte sz1 sz2 sz3 sz4 & rest] (.readAllBytes in) + tags-size (get-size [sz1 sz2 sz3 sz4]) + [x1 x2 x3 x4] rest + sub-header-size (if (check-flag flag-byte 6) (get-size [x1 x2 x3 x4]) 0) + frames (drop sub-header-size (take tags-size rest))] + (loop [rem-frames frames + acc []] + (if (or (empty? rem-frames) (zero? (first rem-frames))) acc + (recur (drop (+ 10 (:size (get-frame rem-frames))) rem-frames) + (conj acc (get-frame rem-frames)))))))) From b50d4a1d257760092ef0b8066b8d4b3389ba353c Mon Sep 17 00:00:00 2001 From: Vadzimz <56486120+Vadzimz@users.noreply.github.com> Date: Sun, 21 Jan 2024 21:36:49 +0300 Subject: [PATCH 23/25] Update homework.clj --- otus-10/src/otus_10/homework.clj | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/otus-10/src/otus_10/homework.clj b/otus-10/src/otus_10/homework.clj index 4c7a258..7643efc 100644 --- a/otus-10/src/otus_10/homework.clj +++ b/otus-10/src/otus_10/homework.clj @@ -32,16 +32,22 @@ (defmethod parse-frame 3 [coll] (String. (byte-array (remove zero? (next coll))) "UTF-8")) +;; вариант функции выбора кодировки без case +(defn parse-frame-v2 [coll] + (let [[encoding-byte & rest] coll + encoding-types ["Windows-1252" "UTF-16" "UTF16BE" "UTF-8"]] + (String. (byte-array rest) (nth encoding-types encoding-byte)))) (defn get-frame "парсим один фрейм" [stream] - (let [[nm1 nm2 nm3 nm4 sz1 sz2 sz3 sz4 & rest] stream + (let [[nm1 nm2 nm3 nm4 sz1 sz2 sz3 sz4 _ _ & rest] stream name (apply str (map char [nm1 nm2 nm3 nm4])) size (get-size [sz1 sz2 sz3 sz4]) - text (parse-frame (take (+ size 2) rest))] + text (parse-frame (take size rest))] {:name name :size size :text text})) + (defn parse-mp3-tags [file] (with-open [in (io/input-stream (io/file (io/resource file)))] (let [[_ _ _ _ _ flag-byte sz1 sz2 sz3 sz4 & rest] (.readAllBytes in) From f0ea685c8ee75b0f4b6fdde0b9e617e0b381e645 Mon Sep 17 00:00:00 2001 From: Vadzimz <56486120+Vadzimz@users.noreply.github.com> Date: Mon, 29 Jan 2024 18:06:03 +0300 Subject: [PATCH 24/25] Create homework_test.clj --- otus-10/test/otus_10/homework_test.clj | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 otus-10/test/otus_10/homework_test.clj diff --git a/otus-10/test/otus_10/homework_test.clj b/otus-10/test/otus_10/homework_test.clj new file mode 100644 index 0000000..d634e3c --- /dev/null +++ b/otus-10/test/otus_10/homework_test.clj @@ -0,0 +1,11 @@ +(ns otus-10-homework-test + (:require [clojure.test :refer :all] + [otus-10-homework :as sut])) + +(deftest test-parse-mp3-tags + (testing "проверка бита в байте, представленным числом" + (is (false? (sut/check-flag 1 5))) + (testing "проверка размера фрейма" + (is (= 129.0 (sut/get-size [0 0 1 1])))) + (testing "проверка парсинга массива байтов с выбранной кодировкой" + (is (= "Clojure" (sut/parse-frame-text [0 67 108 111 106 117 114 101])))))) From 8cc5f7b42d5ac112d4eecfbed77755c56e36e89d Mon Sep 17 00:00:00 2001 From: Vadzimz <56486120+Vadzimz@users.noreply.github.com> Date: Mon, 29 Jan 2024 18:10:41 +0300 Subject: [PATCH 25/25] Update homework 10.clj --- otus-10/src/otus_10/homework.clj | 63 +++++++++++++++----------------- 1 file changed, 30 insertions(+), 33 deletions(-) diff --git a/otus-10/src/otus_10/homework.clj b/otus-10/src/otus_10/homework.clj index 7643efc..c6f5450 100644 --- a/otus-10/src/otus_10/homework.clj +++ b/otus-10/src/otus_10/homework.clj @@ -1,10 +1,10 @@ -(ns otus-10-homework.core -(:require [clojure.string :as string]) - (:require [clojure.java.io :as io])) +(ns otus-10-homework + (:require [clojure.string :as string]) + (:require [clojure.java.io :as io])) (def n1 "file-12926-ed090b.mp3") -(defn check-flag +(defn check-flag "Проверяем n-ый бит байта в виде integer" [num-int ind] (->> num-int @@ -13,7 +13,7 @@ (#(nth % ind \0)) (= \1))) -(defn get-size +(defn get-size "Получаем размер (header, subheader or frame)" [arr] (->> (range (count arr)) @@ -22,41 +22,38 @@ (map * arr) (apply +))) -(defmulti parse-frame first) -(defmethod parse-frame 0 - [coll] (String. (byte-array (remove zero? (next coll))) "Windows-1252")) -(defmethod parse-frame 1 - [coll] (String. (byte-array (remove zero? (next coll))) "UTF-16")) -(defmethod parse-frame 2 - [coll] (String. (byte-array (remove zero? (next coll))) "UTF-16BE")) -(defmethod parse-frame 3 - [coll] (String. (byte-array (remove zero? (next coll))) "UTF-8")) - -;; вариант функции выбора кодировки без case -(defn parse-frame-v2 [coll] + +(defn parse-frame-text [coll] (let [[encoding-byte & rest] coll encoding-types ["Windows-1252" "UTF-16" "UTF16BE" "UTF-8"]] (String. (byte-array rest) (nth encoding-types encoding-byte)))) -(defn get-frame +(defn get-frame-inside "парсим один фрейм" [stream] - (let [[nm1 nm2 nm3 nm4 sz1 sz2 sz3 sz4 _ _ & rest] stream - name (apply str (map char [nm1 nm2 nm3 nm4])) + (let [[_ _ _ _ sz1 sz2 sz3 sz4 _ _ & rest] stream size (get-size [sz1 sz2 sz3 sz4]) - text (parse-frame (take size rest))] - {:name name :size size :text text})) + text (parse-frame-text (take size rest))] + {:size size :text text})) + +(defmulti get-frame (fn [coll] (apply str (map char (take 4 coll))))) +(defmethod get-frame "TALB" [coll] (assoc (get-frame-inside coll) :name "TALB" :description "Album/Movie/Show title")) +(defmethod get-frame "TYER" [coll] (assoc (get-frame-inside coll) :name "TYER" :description "Year")) +(defmethod get-frame "TCON" [coll] (assoc (get-frame-inside coll) :name "TCON" :description "Content type")) +(defmethod get-frame "TDRC" [coll] (assoc (get-frame-inside coll) :name "TDRC" :description "Recording time")) +(defmethod get-frame "TIT2" [coll] (assoc (get-frame-inside coll) :name "TIT2" :description "Title/songname/content description")) +(defmethod get-frame "TPE1" [coll] (assoc (get-frame-inside coll) :name "TPE1" :description "Lead performer(s)/Soloist(s)")) (defn parse-mp3-tags [file] -(with-open [in (io/input-stream (io/file (io/resource file)))] - (let [[_ _ _ _ _ flag-byte sz1 sz2 sz3 sz4 & rest] (.readAllBytes in) - tags-size (get-size [sz1 sz2 sz3 sz4]) - [x1 x2 x3 x4] rest - sub-header-size (if (check-flag flag-byte 6) (get-size [x1 x2 x3 x4]) 0) - frames (drop sub-header-size (take tags-size rest))] - (loop [rem-frames frames - acc []] - (if (or (empty? rem-frames) (zero? (first rem-frames))) acc - (recur (drop (+ 10 (:size (get-frame rem-frames))) rem-frames) - (conj acc (get-frame rem-frames)))))))) + (with-open [in (io/input-stream (io/file (io/resource file)))] + (let [[_ _ _ _ _ flag-byte sz1 sz2 sz3 sz4 & rest] (.readAllBytes in) + tags-size (get-size [sz1 sz2 sz3 sz4]) + [x1 x2 x3 x4] rest + sub-header-size (if (check-flag flag-byte 6) (get-size [x1 x2 x3 x4]) 0) + frames (drop sub-header-size (take tags-size rest))] + (loop [rem-frames frames + acc []] + (if (or (empty? rem-frames) (zero? (first rem-frames))) acc + (recur (drop (+ 10 (:size (get-frame rem-frames))) rem-frames) + (conj acc (get-frame rem-frames))))))))