Skip to content

Commit 2eec620

Browse files
author
v.nesterenko
committed
hw 2
1 parent 4c4cf02 commit 2eec620

5 files changed

Lines changed: 132 additions & 14 deletions

File tree

otus-02/src/otus_02/homework/common_child.clj

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
(ns otus-02.homework.common-child)
22

3-
43
;; Строка называется потомком другой строки,
54
;; если она может быть образована путем удаления 0 или более символов из другой строки.
65
;; Буквы нельзя переставлять.
@@ -15,8 +14,64 @@
1514

1615
;; Еще пример HARRY и SALLY. Ответ будет - 2, так как общий элемент у них AY
1716

17+
; (defn lol [n]
18+
; (loop [i 0 r nil]
19+
; (if (> i n)
20+
; r
21+
; (loop [j 0 s nil]
22+
; (if (< j n) (recur (+ 1 j) (conj s [i j])) s)))))
23+
;
24+
; (lol 10)
25+
;
26+
; (defn look [s1 s2 i j t]
27+
; (let
28+
; [s1-elem (nth s1 (- i 1))
29+
; s2-elem (nth s2 (- j 1))
30+
; i-1j-1 (get-in t [(- i 1) (- j 1)])
31+
; i-1j (get-in t [(- i 1) j])
32+
; ij-1 (get-in t [i (- j 1)])]
33+
; (if (= s1-elem s2-elem) (assoc-in t [i j] (+ i-1j-1 1)) (assoc-in t [i j] (max i-1j ij-1)))))
34+
;
35+
; (get-in [[0,0,0,0,0,0], [0],[0],[0],[0],[0],[0]] [1 0])
36+
; (assoc-in [[0,0,0,0,0,0], [0],[0],[0],[0],[0],[0]] [1 1] 12)
37+
;
38+
; (assoc-in [[0,0,0,0,0,0], [0],[0],[0],[0],[0],[0]] [2 1] 1)
39+
;
40+
; (range 1 (+ 2 (count "HARRY")))
41+
;
42+
; (def i1 (look "HARRY" "SALLY" 1 1 [[0,0,0,0,0,0], [0], [0], [0], [0], [0], [0], [0]]))
43+
; (def i2 (look "HARRY" "SALLY" 1 2 i1))
44+
; (def i3 (look "HARRY" "SALLY" 1 3 i2))
45+
; (def i4 (look "HARRY" "SALLY" 1 4 i3))
46+
; (def i5 (look "HARRY" "SALLY" 1 5 i4))
47+
;
48+
; (def i6 (look "HARRY" "SALLY" 2 1 i5))
49+
; (def i7 (look "HARRY" "SALLY" 2 2 i6))
50+
; (def i8 (look "HARRY" "SALLY" 2 3 i7))
51+
; (def i9 (look "HARRY" "SALLY" 2 4 i8))
52+
; (def i10 (look "HARRY" "SALLY" 2 5 i9))
53+
;
54+
; (def q (vec (repeat (+ 1 (count "HARRY")) 0)))
55+
; (def w (vec (repeat (+ 1 (count "SALLY")) [0])))
56+
; (def e (vec (concat q w)))
57+
1858

19-
(defn common-child-length [first-string second-string])
59+
(defn common-child-length [s1 s2]
60+
(let [look (fn [s1 s2 i j t]
61+
(let
62+
[s1-elem (nth s1 (- i 1))
63+
s2-elem (nth s2 (- j 1))
64+
i-1j-1 (get-in t [(- i 1) (- j 1)])
65+
i-1j (get-in t [(- i 1) j])
66+
ij-1 (get-in t [i (- j 1)])]
67+
(if (= s1-elem s2-elem) (assoc-in t [i j] (+ i-1j-1 1)) (assoc-in t [i j] (max i-1j ij-1)))))
68+
t [(vec (repeat (+ 1 (count s1)) 0))]
69+
r (vec (repeat (+ 1 (count s2)) [0]))
70+
table (vec (concat t r))
71+
irange (range 1 (+ 1 (count s1)))
72+
jrange (range 1 (+ 1 (count s2)))]
73+
(get-in (reduce (fn [t i] (reduce (fn [t1 j] (look s1 s2 i j t1)) t jrange)) table irange) [(count s1) (count s1)])))
2074

75+
(common-child-length "SHINCHAN" "NOHARAAA")
2176

2277

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
11
(ns otus-02.homework.palindrome
2-
(:require [clojure.string :as string]))
2+
(:require [clojure.string :as string]
3+
[clojure.string :as str]))
34

5+
(defn remove-non-alpha [s]
6+
(apply str (re-seq #"[a-zA-Z]+" s)))
47

5-
(defn is-palindrome [test-string])
68

9+
(defn aux [s len pos]
10+
(cond
11+
(>= pos len) true
12+
(= (nth s pos) (nth s (- len pos))) (aux s len (+ 1 pos))
13+
:else false))
14+
15+
(defn is-palindrome [test-string]
16+
(let [
17+
s (-> test-string remove-non-alpha str/lower-case)
18+
len (- (count s) 1)
19+
]
20+
(aux s len 0)
21+
22+
)
23+
)
24+
25+
(-> "Was it a cat I saw?" remove-non-alpha str/lower-case )
26+
27+
28+
(is-palindrome "Was it a cat I saw?")
29+
(print 21)
Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
(ns otus-02.homework.pangram
2-
(:require [clojure.string :as string]))
2+
(:require [clojure.string :as string]
3+
[clojure.string :as str]))
4+
(require '[clojure.string :as str])
35

6+
(def letters
7+
[\a \b \c \d \e \f \g \h \i \j \k \l \m \n \o \p \q \r \s \t \u \v \w \x \y \z])
48

5-
(defn is-pangram [test-string])
9+
(def chars-set (set (map char (range (int \a) (+ (int \a) 26)))))
10+
11+
(defn is-pangram [test-string]
12+
(>= (count (reduce (fn [a e] (conj a e)) (set []) (str/lower-case test-string))) 26)
13+
14+
)
15+
16+
(is-pangram "The quick brown fox jumps over the lazy dog")
617

otus-02/src/otus_02/homework/square_code.clj

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
(ns otus-02.homework.square-code)
1+
(ns otus-02.homework.square-code
2+
(:require
3+
[clojure.string :as str]))
24

35
;; Реализовать классический метод составления секретных сообщений, называемый `square code`.
46
;; Выведите закодированную версию полученного текста.
@@ -11,8 +13,8 @@
1113
;; Например,
1214
"If man was meant to stay on the ground, god would have given us roots."
1315
;; нормализуется в строку:
14-
"ifmanwasmeanttostayonthegroundgodwouldhavegivenusroots"
1516

17+
(def s "ifmanwasmeanttostayonthegroundgodwouldhavegivenusroots")
1618
;; Разбиваем текст в виде прямоугольника.
1719
;; Размер прямоугольника (rows, cols) должен определяться длиной сообщения,
1820
;; так что c >= r и c - r <= 1, где c — количество столбцов, а r — количество строк.
@@ -26,6 +28,31 @@
2628
"vegivenu"
2729
"sroots "
2830

31+
(defn remove-spaces [s]
32+
(apply str (remove #(= % \space) s)))
33+
34+
(defn normalize-text [text]
35+
(-> text
36+
(clojure.string/replace #"\s+|\p{Punct}" "")
37+
clojure.string/lower-case))
38+
39+
(defn encode-string [input]
40+
(let [s (normalize-text input)
41+
n (+ 1 (int (Math/floor (Math/sqrt (double (count s))))))
42+
rows (partition n n nil s)
43+
transposed (map (fn [i] (map #(nth % i \space) rows)) (range n))
44+
res3 (clojure.string/join " " (map (fn [elem] (apply str (map str elem))) transposed))]
45+
res3))
46+
47+
(defn decode-string [s]
48+
(let [a (remove clojure.string/blank? (clojure.string/split s #" "))
49+
n (count (first a))
50+
transposed (map (fn [i] (map #(nth % i \space) a)) (range n))
51+
res (remove-spaces (apply str (map (fn [elem] (apply str (map str elem))) transposed)))]
52+
res))
53+
54+
(decode-string "imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghn sseoau ")
55+
2956
;; Закодированное сообщение получается путем чтения столбцов слева направо.
3057
;; Сообщение выше закодировано как:
3158
"imtgdvsfearwermayoogoanouuiontnnlvtwttddesaohghnsseoau"
@@ -49,8 +76,3 @@
4976
"sseoau "
5077

5178

52-
53-
(defn encode-string [input])
54-
55-
56-
(defn decode-string [input])

otus-02/test/otus_02/homework/fizzbuzz.clj

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,21 @@
22
(:require
33
[clojure.test :refer :all]))
44

5+
(defn is-divisible [n by]
6+
(zero? (mod n by)))
57

68
(defn fizz-buzz [n]
79
"Создайте программу, которая выводит числа от 1 до n.
810
- Если число делится на 3, выведите 'Fizz';
911
- если число делится на 5, выведите 'Buzz';
1012
- если число делится и на 3 и на 5, выведите 'FizzBuzz'."
11-
"implement me")
13+
(map (fn [e] (cond
14+
(is-divisible e 15) "FizzBuzz"
15+
(is-divisible e 5) "Buzz"
16+
(is-divisible e 3) "Fizz" :else e)
17+
) (range 1 (+ 1 n))))
1218

19+
(fizz-buzz 10)
1320

1421
(deftest fizz-buzz-test
1522
(is (= (fizz-buzz 10)

0 commit comments

Comments
 (0)