From 96a7df8afff137135e1165e694a13b42fb36469a Mon Sep 17 00:00:00 2001 From: Gregory Tereshko Date: Sat, 16 Nov 2024 19:30:12 +0300 Subject: [PATCH 1/8] fix tests namespace --- otus-04/test/{ => otus_04}/homework/crossword_puzzle_test.clj | 0 otus-04/test/{ => otus_04}/homework/magic_square_test.clj | 0 otus-04/test/{ => otus_04}/homework/scramblies_test.clj | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename otus-04/test/{ => otus_04}/homework/crossword_puzzle_test.clj (100%) rename otus-04/test/{ => otus_04}/homework/magic_square_test.clj (100%) rename otus-04/test/{ => otus_04}/homework/scramblies_test.clj (100%) diff --git a/otus-04/test/homework/crossword_puzzle_test.clj b/otus-04/test/otus_04/homework/crossword_puzzle_test.clj similarity index 100% rename from otus-04/test/homework/crossword_puzzle_test.clj rename to otus-04/test/otus_04/homework/crossword_puzzle_test.clj diff --git a/otus-04/test/homework/magic_square_test.clj b/otus-04/test/otus_04/homework/magic_square_test.clj similarity index 100% rename from otus-04/test/homework/magic_square_test.clj rename to otus-04/test/otus_04/homework/magic_square_test.clj diff --git a/otus-04/test/homework/scramblies_test.clj b/otus-04/test/otus_04/homework/scramblies_test.clj similarity index 100% rename from otus-04/test/homework/scramblies_test.clj rename to otus-04/test/otus_04/homework/scramblies_test.clj From 200109a2002988da647bcc7e8622779911330d8e Mon Sep 17 00:00:00 2001 From: Gregory Tereshko Date: Sun, 17 Nov 2024 19:49:38 +0300 Subject: [PATCH 2/8] complete otus-04/scramblies task --- otus-04/src/otus_04/homework/scramblies.clj | 40 ++++++++++++++++++--- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/otus-04/src/otus_04/homework/scramblies.clj b/otus-04/src/otus_04/homework/scramblies.clj index f76d335..a7a91d3 100644 --- a/otus-04/src/otus_04/homework/scramblies.clj +++ b/otus-04/src/otus_04/homework/scramblies.clj @@ -1,10 +1,40 @@ (ns otus-04.homework.scramblies) -;; Оригинальная задача: -;; https://www.codewars.com/kata/55c04b4cc56a697bb0000048 +;; 1-st attempt +;; первая попытка не учитывала что в letters может быть не достаточно букв для +;; построения word +(defn scramble? + [letters word] + (let [letters-set (set letters)] + (every? true? + (map (comp not nil? letters-set) word)))) + +;; 2-nd attempt +;; вторая попытка учитывает кол-во букв, но результат получаем двумя проходами по +;; последовательности ключей, решил заменить на один проход при помощи reduce +(defn reducer [acc val] + (if (acc val) + (update acc val inc) + (assoc acc val 1))) + +(defn word->map + [word] + (reduce reducer {} word)) (defn scramble? - "Функция возвращает true, если из букв в строке letters - можно составить слово word." [letters word] - nil) + (let [letters-map (word->map letters) + word-map (word->map word) + word-map-keys (keys word-map)] + + (every? false? + (map #(< (letters-map %1 0) (word-map %1 0)) word-map-keys)))) + +;; 3-rd attempt +(defn scramble? + [letters word] + (let [letters-map (word->map letters) + word-map (word->map word) + word-map-keys (keys word-map)] + + (reduce #(if (< (letters-map %2 0) (word-map %2 0)) (not %1) %1) true word-map-keys))) From dd8709b4c58d7a9bd26a53f3c936931f3d1c2e14 Mon Sep 17 00:00:00 2001 From: Gregory Tereshko Date: Mon, 18 Nov 2024 01:56:07 +0300 Subject: [PATCH 3/8] otus-04 magic-square wip --- otus-04/src/otus_04/homework/magic_square.clj | 41 +++++++++++++------ 1 file changed, 29 insertions(+), 12 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..846e4f2 100644 --- a/otus-04/src/otus_04/homework/magic_square.clj +++ b/otus-04/src/otus_04/homework/magic_square.clj @@ -1,17 +1,34 @@ (ns otus-04.homework.magic-square) -;; Оригинальная задача: -;; https://www.codewars.com/kata/570b69d96731d4cf9c001597 -;; -;; Подсказка: используйте "Siamese method" -;; https://en.wikipedia.org/wiki/Siamese_method +(defn index-overflow-creator [n] (partial < (dec n))) +(defn next-coord [sq-vec curr-coord] [sq-vec curr-coord]) -(defn magic-square - "Функция возвращает вектор векторов целых чисел, - описывающий магический квадрат размера n*n, - где n - нечётное натуральное число. +(defn magic-square-iterate + [n m sq-vec coord] + (let [square-size n] + + [n m sq-vec coord square-size next-coord])) - Магический квадрат должен быть заполнен так, что суммы всех вертикалей, - горизонталей и диагоналей длиной в n должны быть одинаковы." +(defn magic-square [n] - [[0]]) + {:pre [(odd? n)]} + + (let [magic-vector (vec (repeat n (vector))) + initial-index (quot n 2) + coordinates {:x 0 :y initial-index}] + + [coordinates magic-vector])) + +(comment + (magic-square 9) + (get [1 2 3 4 5 6 7 8 9] 4) + (def n 3) + (def index-overflow? (index-overflow-creator n)) + (index-overflow? 3) + (defn iterate-fn [n m] + (if (= m 0) + n + (iterate-fn (* n 2) (dec m)))) + + (= (iterate-fn 1 10) 1024) +) From 47315169ecb89712c29a481e76d3f1a600c25baa Mon Sep 17 00:00:00 2001 From: Gregory Tereshko Date: Tue, 19 Nov 2024 07:02:39 +0300 Subject: [PATCH 4/8] otus-04 magic-square wip --- otus-04/src/otus_04/homework/magic_square.clj | 143 ++++++++++++++++-- 1 file changed, 132 insertions(+), 11 deletions(-) diff --git a/otus-04/src/otus_04/homework/magic_square.clj b/otus-04/src/otus_04/homework/magic_square.clj index 846e4f2..ace4fdc 100644 --- a/otus-04/src/otus_04/homework/magic_square.clj +++ b/otus-04/src/otus_04/homework/magic_square.clj @@ -1,26 +1,54 @@ (ns otus-04.homework.magic-square) -(defn index-overflow-creator [n] (partial < (dec n))) -(defn next-coord [sq-vec curr-coord] [sq-vec curr-coord]) +(defn get-item + [square-vec {:keys [x y]}] + (let [row (nth square-vec y)] -(defn magic-square-iterate - [n m sq-vec coord] - (let [square-size n] + (nth row x))) - [n m sq-vec coord square-size next-coord])) +(defn set-item + [square-vec item {:keys [x y]}] + (let [row (nth square-vec y) + new-row (assoc row x item)] + + (assoc square-vec y new-row))) + +(defn repeat-n-vec + [item n] + (->> item + (repeat n) + (vec))) + +(defn empty-square-creator + [n] + (repeat-n-vec (repeat-n-vec nil n) n)) + +(defn index-overflow-creator + [n] + (partial < (dec n))) + +(defn inc-coords + [{:keys [x y]} index-overflow?] + + {:x (if (index-overflow? (inc x)) 0 (inc x)) + :y (if (index-overflow? (inc y)) 0 (inc y))}) + +;; ============================================================ + +(defn magic-square-iterate []) (defn magic-square [n] {:pre [(odd? n)]} - (let [magic-vector (vec (repeat n (vector))) + (let [initial-vector (empty-square-creator 3) ;;(vec (repeat n (vector))) initial-index (quot n 2) - coordinates {:x 0 :y initial-index}] + initial-coords {:y 0 :x initial-index}] - [coordinates magic-vector])) + (magic-square-iterate))) (comment - (magic-square 9) + (magic-square 3) (get [1 2 3 4 5 6 7 8 9] 4) (def n 3) (def index-overflow? (index-overflow-creator n)) @@ -31,4 +59,97 @@ (iterate-fn (* n 2) (dec m)))) (= (iterate-fn 1 10) 1024) -) + + (vec (repeat n nil)) + + ;; params destructuring + + (defn test [a b {:keys [x y]}] + [x y]) + + (test 1 2 {:x 2 :y 4})) + +(comment + (def test-vec-3 [[] [8 7 6] []]) + + (def x 2) + (def y 0) + (def item 777) + + (assoc test-vec-3 x + (assoc (get test-vec-3 y) y item)) + + (def n 5) + + (def empty-square-vec + (repeat-n-vec + (repeat-n-vec nil n) n)) + empty-square-vec) + +;; x +;; [ | +;; [1 22 15] ;; <- y +;; [6 88 42] +;; [0 54 65] +;; ] + +(def n 3) + +(def empty-square-vec + (repeat-n-vec + (repeat-n-vec nil n) n)) + +empty-square-vec + +(def sample-vec + (set-item + (set-item + empty-square-vec + 77 {:x 1 :y 0}) + 42 {:x 2 :y 0})) + +sample-vec +;; [[nil 77 42] [nil nil nil] [nil nil nil]] + +(def index-overflow? (index-overflow-creator n)) + +(get-item sample-vec {:x 2 :y 0}) ;; 42 + +;; данная функция не подходит для итеративного процесса напрямую +;; вместо неё создаем в let диагональные-координаты и шаг-вниз-координаты +;; проверяем ячейку квадрата на nil и делаем выбор следующих координат + +(defn next-coords + [square-vec coords index-overflow?] + ;; (let [x (coords :x) + ;; y (coords :y)] + + (let [new-diag-coords {} + new-down-coords {}] + + (if (nil? (get-item square-vec new-diag-coords)) + (new-diag-coords) (new-down-coords)))) + +;; перенеси в let в функции next-coords +(def diag-coords {:x :y}) +(def step-down-coords {:x :y}) + +(next-coords sample-vec {:x 1 :y 0} index-overflow?) + +(index-overflow? 0) ;; false +(index-overflow? 2) ;; false +(index-overflow? 3) ;; true + +(def test-a {:x 1 :y 0}) + +(inc-coords test-a index-overflow?) + +;; (update test-a :a (if (index-overflow? (test-a :a)) * inc)) + +;; Создать hash-map с ключачи парами ячеек квадрата, а далее преобразовать в вектор векторов +(def test-x + {{:x 0 :y 1} 1 + {:x 1 :y 2} 22}) + +(get test-x {:x 0 :y 1}) ; 1 +(get test-x {:x 1 :y 4}) ; nil From eb8eea9c4e0e5f6ffcade5e879a022329f6f343d Mon Sep 17 00:00:00 2001 From: Gregory Tereshko Date: Thu, 21 Nov 2024 03:28:03 +0300 Subject: [PATCH 5/8] complete otus-04/magic-square task --- otus-04/src/otus_04/homework/magic_square.clj | 187 +++++------------ .../otus_04/homework/magic_square.clj.patch | 189 ++++++++++++++++++ 2 files changed, 239 insertions(+), 137 deletions(-) create mode 100644 otus-04/src/otus_04/homework/magic_square.clj.patch diff --git a/otus-04/src/otus_04/homework/magic_square.clj b/otus-04/src/otus_04/homework/magic_square.clj index ace4fdc..e4f6bd4 100644 --- a/otus-04/src/otus_04/homework/magic_square.clj +++ b/otus-04/src/otus_04/homework/magic_square.clj @@ -1,17 +1,40 @@ (ns otus-04.homework.magic-square) -(defn get-item - [square-vec {:keys [x y]}] - (let [row (nth square-vec y)] - - (nth row x))) +(defn next-index-row-factory + [n] + (fn [index] (if (= index 0) (dec n) (dec index)))) -(defn set-item - [square-vec item {:keys [x y]}] - (let [row (nth square-vec y) - new-row (assoc row x item)] +(defn next-index-col-factory + [n] + (fn [index] (if (= index (dec n)) 0 (inc index)))) - (assoc square-vec y new-row))) +(defn next-coords + [next-index-col next-index-row {:keys [col row]}] + {:row (next-index-row row) + :col (next-index-col col)}) + +(defn build-square-map + [square-map coords next-coords-fn value square-size] + (let [new-value (inc value) + next-map (assoc square-map coords new-value) + next-coords (next-coords-fn coords) + cell-empty? (nil? (get next-map next-coords)) + next-coords (if cell-empty? next-coords {:row (inc (:row coords)) :col (:col coords)})] + + (if (zero? square-size) + next-map (build-square-map next-map next-coords next-coords-fn new-value (dec square-size))))) + +(defn build-square-map + [square-map coords next-coords-fn value square-size] + (if (zero? square-size) + square-map + (let [new-value (inc value) + next-map (assoc square-map coords new-value) + next-coords (next-coords-fn coords) + cell-empty? (nil? (get next-map next-coords)) + next-coords (if cell-empty? next-coords {:row (inc (:row coords)) :col (:col coords)})] + + (build-square-map next-map next-coords next-coords-fn new-value (dec square-size))))) (defn repeat-n-vec [item n] @@ -19,137 +42,27 @@ (repeat n) (vec))) -(defn empty-square-creator +(defn empty-square-vec [n] (repeat-n-vec (repeat-n-vec nil n) n)) -(defn index-overflow-creator - [n] - (partial < (dec n))) - -(defn inc-coords - [{:keys [x y]} index-overflow?] - - {:x (if (index-overflow? (inc x)) 0 (inc x)) - :y (if (index-overflow? (inc y)) 0 (inc y))}) - -;; ============================================================ - -(defn magic-square-iterate []) - (defn magic-square [n] {:pre [(odd? n)]} - (let [initial-vector (empty-square-creator 3) ;;(vec (repeat n (vector))) - initial-index (quot n 2) - initial-coords {:y 0 :x initial-index}] - - (magic-square-iterate))) - -(comment - (magic-square 3) - (get [1 2 3 4 5 6 7 8 9] 4) - (def n 3) - (def index-overflow? (index-overflow-creator n)) - (index-overflow? 3) - (defn iterate-fn [n m] - (if (= m 0) - n - (iterate-fn (* n 2) (dec m)))) - - (= (iterate-fn 1 10) 1024) - - (vec (repeat n nil)) - - ;; params destructuring - - (defn test [a b {:keys [x y]}] - [x y]) - - (test 1 2 {:x 2 :y 4})) - -(comment - (def test-vec-3 [[] [8 7 6] []]) - - (def x 2) - (def y 0) - (def item 777) - - (assoc test-vec-3 x - (assoc (get test-vec-3 y) y item)) - - (def n 5) - - (def empty-square-vec - (repeat-n-vec - (repeat-n-vec nil n) n)) - empty-square-vec) - -;; x -;; [ | -;; [1 22 15] ;; <- y -;; [6 88 42] -;; [0 54 65] -;; ] - -(def n 3) - -(def empty-square-vec - (repeat-n-vec - (repeat-n-vec nil n) n)) - -empty-square-vec - -(def sample-vec - (set-item - (set-item - empty-square-vec - 77 {:x 1 :y 0}) - 42 {:x 2 :y 0})) - -sample-vec -;; [[nil 77 42] [nil nil nil] [nil nil nil]] - -(def index-overflow? (index-overflow-creator n)) - -(get-item sample-vec {:x 2 :y 0}) ;; 42 - -;; данная функция не подходит для итеративного процесса напрямую -;; вместо неё создаем в let диагональные-координаты и шаг-вниз-координаты -;; проверяем ячейку квадрата на nil и делаем выбор следующих координат - -(defn next-coords - [square-vec coords index-overflow?] - ;; (let [x (coords :x) - ;; y (coords :y)] - - (let [new-diag-coords {} - new-down-coords {}] - - (if (nil? (get-item square-vec new-diag-coords)) - (new-diag-coords) (new-down-coords)))) - -;; перенеси в let в функции next-coords -(def diag-coords {:x :y}) -(def step-down-coords {:x :y}) - -(next-coords sample-vec {:x 1 :y 0} index-overflow?) - -(index-overflow? 0) ;; false -(index-overflow? 2) ;; false -(index-overflow? 3) ;; true - -(def test-a {:x 1 :y 0}) - -(inc-coords test-a index-overflow?) - -;; (update test-a :a (if (index-overflow? (test-a :a)) * inc)) - -;; Создать hash-map с ключачи парами ячеек квадрата, а далее преобразовать в вектор векторов -(def test-x - {{:x 0 :y 1} 1 - {:x 1 :y 2} 22}) - -(get test-x {:x 0 :y 1}) ; 1 -(get test-x {:x 1 :y 4}) ; nil + (let [initial-index (quot n 2) + initial-coords {:row 0 :col initial-index} + initial-value 0 + next-index-col (next-index-col-factory n) + next-index-row (next-index-row-factory n) + next-coords-fn (partial next-coords next-index-col next-index-row) + empty-vec (empty-square-vec n) + square-map (build-square-map {} initial-coords next-coords-fn initial-value (* n n))] + + (reduce (fn [acc {:keys [row col] :as val}] + (let [square-val (get square-map val) + square-row (nth acc row) + new-row (assoc square-row col square-val)] + + (assoc acc row new-row))) + empty-vec (keys square-map)))) diff --git a/otus-04/src/otus_04/homework/magic_square.clj.patch b/otus-04/src/otus_04/homework/magic_square.clj.patch new file mode 100644 index 0000000..a13d394 --- /dev/null +++ b/otus-04/src/otus_04/homework/magic_square.clj.patch @@ -0,0 +1,189 @@ +diff --git a/otus-04/src/otus_04/homework/magic_square.clj b/otus-04/src/otus_04/homework/magic_square.clj +index ace4fdc..1dde276 100644 +--- a/otus-04/src/otus_04/homework/magic_square.clj ++++ b/otus-04/src/otus_04/homework/magic_square.clj +@@ -1,155 +1,54 @@ + (ns otus-04.homework.magic-square) + +-(defn get-item +- [square-vec {:keys [x y]}] +- (let [row (nth square-vec y)] +- +- (nth row x))) +- +-(defn set-item +- [square-vec item {:keys [x y]}] +- (let [row (nth square-vec y) +- new-row (assoc row x item)] +- +- (assoc square-vec y new-row))) +- +-(defn repeat-n-vec +- [item n] +- (->> item +- (repeat n) +- (vec))) +- +-(defn empty-square-creator ++(defn next-index-row-factory + [n] +- (repeat-n-vec (repeat-n-vec nil n) n)) +- +-(defn index-overflow-creator +- [n] +- (partial < (dec n))) +- +-(defn inc-coords +- [{:keys [x y]} index-overflow?] +- +- {:x (if (index-overflow? (inc x)) 0 (inc x)) +- :y (if (index-overflow? (inc y)) 0 (inc y))}) +- +-;; ============================================================ ++ (fn [index] (if (= index 0) (dec n) (dec index)))) + +-(defn magic-square-iterate []) +- +-(defn magic-square ++(defn next-index-col-factory + [n] +- {:pre [(odd? n)]} +- +- (let [initial-vector (empty-square-creator 3) ;;(vec (repeat n (vector))) +- initial-index (quot n 2) +- initial-coords {:y 0 :x initial-index}] +- +- (magic-square-iterate))) +- +-(comment +- (magic-square 3) +- (get [1 2 3 4 5 6 7 8 9] 4) +- (def n 3) +- (def index-overflow? (index-overflow-creator n)) +- (index-overflow? 3) +- (defn iterate-fn [n m] +- (if (= m 0) +- n +- (iterate-fn (* n 2) (dec m)))) +- +- (= (iterate-fn 1 10) 1024) +- +- (vec (repeat n nil)) +- +- ;; params destructuring +- +- (defn test [a b {:keys [x y]}] +- [x y]) +- +- (test 1 2 {:x 2 :y 4})) ++ (fn [index] (if (= index (dec n)) 0 (inc index)))) + + (comment +- (def test-vec-3 [[] [8 7 6] []]) +- +- (def x 2) +- (def y 0) +- (def item 777) +- +- (assoc test-vec-3 x +- (assoc (get test-vec-3 y) y item)) +- + (def n 5) ++ (def next-index-col ++ (next-index-col-factory n)) + +- (def empty-square-vec +- (repeat-n-vec +- (repeat-n-vec nil n) n)) +- empty-square-vec) +- +-;; x +-;; [ | +-;; [1 22 15] ;; <- y +-;; [6 88 42] +-;; [0 54 65] +-;; ] +- +-(def n 3) +- +-(def empty-square-vec +- (repeat-n-vec +- (repeat-n-vec nil n) n)) +- +-empty-square-vec +- +-(def sample-vec +- (set-item +- (set-item +- empty-square-vec +- 77 {:x 1 :y 0}) +- 42 {:x 2 :y 0})) ++ (def next-index-row ++ (next-index-row-factory n)) + +-sample-vec +-;; [[nil 77 42] [nil nil nil] [nil nil nil]] ++ (next-index-row 0) ; => 2 ++ (next-index-col 2) ; => 0 ++ ) + +-(def index-overflow? (index-overflow-creator n)) ++(defn build-square-map ++ [square-map coords value square-size] ++ (let [next-map (assoc square-map coords value) ++ next-coords ()]) + +-(get-item sample-vec {:x 2 :y 0}) ;; 42 ++ ) + +-;; данная функция не подходит для итеративного процесса напрямую +-;; вместо неё создаем в let диагональные-координаты и шаг-вниз-координаты +-;; проверяем ячейку квадрата на nil и делаем выбор следующих координат +- +-(defn next-coords +- [square-vec coords index-overflow?] +- ;; (let [x (coords :x) +- ;; y (coords :y)] +- +- (let [new-diag-coords {} +- new-down-coords {}] +- +- (if (nil? (get-item square-vec new-diag-coords)) +- (new-diag-coords) (new-down-coords)))) +- +-;; перенеси в let в функции next-coords +-(def diag-coords {:x :y}) +-(def step-down-coords {:x :y}) ++(defn magic-square ++ [n] ++ {:pre [(odd? n)]} + +-(next-coords sample-vec {:x 1 :y 0} index-overflow?) ++ (let [initial-index (quot n 2) ++ initial-coords {:row 0 :col initial-index} ++ initial-value 1] + +-(index-overflow? 0) ;; false +-(index-overflow? 2) ;; false +-(index-overflow? 3) ;; true ++ )) + +-(def test-a {:x 1 :y 0}) ++(magic-square 11) + +-(inc-coords test-a index-overflow?) ++;; Создать hash-map с ключачи парами ячеек квадрата, а далее преобразовать в вектор векторов + +-;; (update test-a :a (if (index-overflow? (test-a :a)) * inc)) ++(def test-key {:x 1 :y 3}) ++(def test-val 46) + +-;; Создать hash-map с ключачи парами ячеек квадрата, а далее преобразовать в вектор векторов + (def test-x + {{:x 0 :y 1} 1 + {:x 1 :y 2} 22}) + + (get test-x {:x 0 :y 1}) ; 1 + (get test-x {:x 1 :y 4}) ; nil ++ ++(assoc test-x test-key test-val) From 08e06e8f8c7d3a2e6911097a0edd272aea037252 Mon Sep 17 00:00:00 2001 From: Gregory Tereshko Date: Thu, 21 Nov 2024 03:36:28 +0300 Subject: [PATCH 6/8] delete patch file --- .../otus_04/homework/magic_square.clj.patch | 189 ------------------ 1 file changed, 189 deletions(-) delete mode 100644 otus-04/src/otus_04/homework/magic_square.clj.patch diff --git a/otus-04/src/otus_04/homework/magic_square.clj.patch b/otus-04/src/otus_04/homework/magic_square.clj.patch deleted file mode 100644 index a13d394..0000000 --- a/otus-04/src/otus_04/homework/magic_square.clj.patch +++ /dev/null @@ -1,189 +0,0 @@ -diff --git a/otus-04/src/otus_04/homework/magic_square.clj b/otus-04/src/otus_04/homework/magic_square.clj -index ace4fdc..1dde276 100644 ---- a/otus-04/src/otus_04/homework/magic_square.clj -+++ b/otus-04/src/otus_04/homework/magic_square.clj -@@ -1,155 +1,54 @@ - (ns otus-04.homework.magic-square) - --(defn get-item -- [square-vec {:keys [x y]}] -- (let [row (nth square-vec y)] -- -- (nth row x))) -- --(defn set-item -- [square-vec item {:keys [x y]}] -- (let [row (nth square-vec y) -- new-row (assoc row x item)] -- -- (assoc square-vec y new-row))) -- --(defn repeat-n-vec -- [item n] -- (->> item -- (repeat n) -- (vec))) -- --(defn empty-square-creator -+(defn next-index-row-factory - [n] -- (repeat-n-vec (repeat-n-vec nil n) n)) -- --(defn index-overflow-creator -- [n] -- (partial < (dec n))) -- --(defn inc-coords -- [{:keys [x y]} index-overflow?] -- -- {:x (if (index-overflow? (inc x)) 0 (inc x)) -- :y (if (index-overflow? (inc y)) 0 (inc y))}) -- --;; ============================================================ -+ (fn [index] (if (= index 0) (dec n) (dec index)))) - --(defn magic-square-iterate []) -- --(defn magic-square -+(defn next-index-col-factory - [n] -- {:pre [(odd? n)]} -- -- (let [initial-vector (empty-square-creator 3) ;;(vec (repeat n (vector))) -- initial-index (quot n 2) -- initial-coords {:y 0 :x initial-index}] -- -- (magic-square-iterate))) -- --(comment -- (magic-square 3) -- (get [1 2 3 4 5 6 7 8 9] 4) -- (def n 3) -- (def index-overflow? (index-overflow-creator n)) -- (index-overflow? 3) -- (defn iterate-fn [n m] -- (if (= m 0) -- n -- (iterate-fn (* n 2) (dec m)))) -- -- (= (iterate-fn 1 10) 1024) -- -- (vec (repeat n nil)) -- -- ;; params destructuring -- -- (defn test [a b {:keys [x y]}] -- [x y]) -- -- (test 1 2 {:x 2 :y 4})) -+ (fn [index] (if (= index (dec n)) 0 (inc index)))) - - (comment -- (def test-vec-3 [[] [8 7 6] []]) -- -- (def x 2) -- (def y 0) -- (def item 777) -- -- (assoc test-vec-3 x -- (assoc (get test-vec-3 y) y item)) -- - (def n 5) -+ (def next-index-col -+ (next-index-col-factory n)) - -- (def empty-square-vec -- (repeat-n-vec -- (repeat-n-vec nil n) n)) -- empty-square-vec) -- --;; x --;; [ | --;; [1 22 15] ;; <- y --;; [6 88 42] --;; [0 54 65] --;; ] -- --(def n 3) -- --(def empty-square-vec -- (repeat-n-vec -- (repeat-n-vec nil n) n)) -- --empty-square-vec -- --(def sample-vec -- (set-item -- (set-item -- empty-square-vec -- 77 {:x 1 :y 0}) -- 42 {:x 2 :y 0})) -+ (def next-index-row -+ (next-index-row-factory n)) - --sample-vec --;; [[nil 77 42] [nil nil nil] [nil nil nil]] -+ (next-index-row 0) ; => 2 -+ (next-index-col 2) ; => 0 -+ ) - --(def index-overflow? (index-overflow-creator n)) -+(defn build-square-map -+ [square-map coords value square-size] -+ (let [next-map (assoc square-map coords value) -+ next-coords ()]) - --(get-item sample-vec {:x 2 :y 0}) ;; 42 -+ ) - --;; данная функция не подходит для итеративного процесса напрямую --;; вместо неё создаем в let диагональные-координаты и шаг-вниз-координаты --;; проверяем ячейку квадрата на nil и делаем выбор следующих координат -- --(defn next-coords -- [square-vec coords index-overflow?] -- ;; (let [x (coords :x) -- ;; y (coords :y)] -- -- (let [new-diag-coords {} -- new-down-coords {}] -- -- (if (nil? (get-item square-vec new-diag-coords)) -- (new-diag-coords) (new-down-coords)))) -- --;; перенеси в let в функции next-coords --(def diag-coords {:x :y}) --(def step-down-coords {:x :y}) -+(defn magic-square -+ [n] -+ {:pre [(odd? n)]} - --(next-coords sample-vec {:x 1 :y 0} index-overflow?) -+ (let [initial-index (quot n 2) -+ initial-coords {:row 0 :col initial-index} -+ initial-value 1] - --(index-overflow? 0) ;; false --(index-overflow? 2) ;; false --(index-overflow? 3) ;; true -+ )) - --(def test-a {:x 1 :y 0}) -+(magic-square 11) - --(inc-coords test-a index-overflow?) -+;; Создать hash-map с ключачи парами ячеек квадрата, а далее преобразовать в вектор векторов - --;; (update test-a :a (if (index-overflow? (test-a :a)) * inc)) -+(def test-key {:x 1 :y 3}) -+(def test-val 46) - --;; Создать hash-map с ключачи парами ячеек квадрата, а далее преобразовать в вектор векторов - (def test-x - {{:x 0 :y 1} 1 - {:x 1 :y 2} 22}) - - (get test-x {:x 0 :y 1}) ; 1 - (get test-x {:x 1 :y 4}) ; nil -+ -+(assoc test-x test-key test-val) From 429b3f9b90e28e1a89746e0ec8e9a623a7a27906 Mon Sep 17 00:00:00 2001 From: Aleksandr Zhuravlev Date: Sat, 16 Nov 2024 22:50:23 +0300 Subject: [PATCH 7/8] Add url-shortener core --- url-shortener/01-core/README.md | 1 + url-shortener/01-core/project.clj | 7 +++ .../01-core/src/url_shortener/core.clj | 44 +++++++++++++++++++ .../01-core/test/url_shortener/core_test.clj | 7 +++ 4 files changed, 59 insertions(+) create mode 100644 url-shortener/01-core/README.md create mode 100644 url-shortener/01-core/project.clj create mode 100644 url-shortener/01-core/src/url_shortener/core.clj create mode 100644 url-shortener/01-core/test/url_shortener/core_test.clj diff --git a/url-shortener/01-core/README.md b/url-shortener/01-core/README.md new file mode 100644 index 0000000..32c860d --- /dev/null +++ b/url-shortener/01-core/README.md @@ -0,0 +1 @@ +# url-shortener diff --git a/url-shortener/01-core/project.clj b/url-shortener/01-core/project.clj new file mode 100644 index 0000000..180b5db --- /dev/null +++ b/url-shortener/01-core/project.clj @@ -0,0 +1,7 @@ +(defproject url-shortener "0.1.0-SNAPSHOT" + :description "FIXME: write description" + :url "http://example.com/FIXME" + :license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0" + :url "https://www.eclipse.org/legal/epl-2.0/"} + :dependencies [[org.clojure/clojure "1.12.0"]] + :repl-options {:init-ns url-shortener.core}) diff --git a/url-shortener/01-core/src/url_shortener/core.clj b/url-shortener/01-core/src/url_shortener/core.clj new file mode 100644 index 0000000..e91027f --- /dev/null +++ b/url-shortener/01-core/src/url_shortener/core.clj @@ -0,0 +1,44 @@ +(ns url-shortener.core + (:require [clojure.string :as string])) + +;; Consts +(def ^:const alphabet-size 62) + +(def ^:const alphabet + "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz") + +;; Logic +(defn- get-idx [i] + (Math/floor (/ i alphabet-size))) + +(defn- get-character-by-idx [i] + (get alphabet (rem i alphabet-size))) + +(defn int->id [id] + (if (< id alphabet-size) + (str (get-character-by-idx id)) + (let [codes (->> (iterate get-idx id) + (take-while pos?) + (map get-character-by-idx))] + (string/join (reverse codes))))) + +(comment + (int->id 0) ; => "0" + (int->id alphabet-size) ; => "10" + (int->id 9999999999999) ; => "2q3Rktod" + (int->id Long/MAX_VALUE)) ; => "AzL8n0Y58W7" + + +(defn id->int [id] + (reduce (fn [id ch] + (+ (* id alphabet-size) + (string/index-of alphabet ch))) + 0 + id)) + +(comment + (id->int "0") ; => 0 + (id->int "z") ; => 61 + (id->int "clj") ; => 149031 + (id->int "Clojure")) ; => 725410830262 + diff --git a/url-shortener/01-core/test/url_shortener/core_test.clj b/url-shortener/01-core/test/url_shortener/core_test.clj new file mode 100644 index 0000000..dce7379 --- /dev/null +++ b/url-shortener/01-core/test/url_shortener/core_test.clj @@ -0,0 +1,7 @@ +(ns url-shortener.core-test + (:require [clojure.test :refer :all] + [url-shortener.core :refer :all])) + +(deftest a-test + (testing "FIXME, I fail." + (is (= 0 1)))) From cda0c91d3664ef1d572d5d0ceb45d61d39bff9bd Mon Sep 17 00:00:00 2001 From: Gregory Tereshko Date: Thu, 21 Nov 2024 04:13:24 +0300 Subject: [PATCH 8/8] remove shadowed fn used for dev --- otus-04/src/otus_04/homework/magic_square.clj | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/otus-04/src/otus_04/homework/magic_square.clj b/otus-04/src/otus_04/homework/magic_square.clj index e4f6bd4..974a2c7 100644 --- a/otus-04/src/otus_04/homework/magic_square.clj +++ b/otus-04/src/otus_04/homework/magic_square.clj @@ -13,17 +13,6 @@ {:row (next-index-row row) :col (next-index-col col)}) -(defn build-square-map - [square-map coords next-coords-fn value square-size] - (let [new-value (inc value) - next-map (assoc square-map coords new-value) - next-coords (next-coords-fn coords) - cell-empty? (nil? (get next-map next-coords)) - next-coords (if cell-empty? next-coords {:row (inc (:row coords)) :col (:col coords)})] - - (if (zero? square-size) - next-map (build-square-map next-map next-coords next-coords-fn new-value (dec square-size))))) - (defn build-square-map [square-map coords next-coords-fn value square-size] (if (zero? square-size)