-
Notifications
You must be signed in to change notification settings - Fork 7
Home Work 2 Boriskin #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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))))) | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. это похоже не используется? |
||
| 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. filter identity обычно можно заменить на вызов функции remove |
||
| (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))))))) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. а для чего используется some-> ? кажется лишним |
||
|
|
||
| ;;(time (common-child-length "HARRY" "SALLY")) | ||
| ;;=> "Elapsed time: 0.132041 msecs" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,5 +2,10 @@ | |
| (:require [clojure.string :as string])) | ||
|
|
||
|
|
||
| (defn is-palindrome [test-string]) | ||
|
|
||
| (defn is-palindrome | ||
| [test-string] | ||
| (as-> test-string p | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Хороший подход. решение верное 👍 |
||
| (re-seq #"[\w+]" p) | ||
| (string/join "" p) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. такой вопрос - а зачем джойнить список обратно в строку? списки и вектора можно сравнивать напрямую |
||
| (string/lower-case p) | ||
| (= (string/reverse p) p))) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,5 +2,11 @@ | |
| (:require [clojure.string :as string])) | ||
|
|
||
|
|
||
| (defn is-pangram [test-string]) | ||
|
|
||
| (defn is-pangram | ||
| [test-string] | ||
| (as-> test-string p | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. как и в примере с палиндромом можно брать |
||
| (string/lower-case p) | ||
| (re-seq #"[\w+]" p) | ||
| (set p) | ||
| (string/join "" p) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. тут джойн в строку тоже выглядит как лишняя операция |
||
| (= (count p) 26))) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
вот тут не совсем понятка задумка. зачем тут loop? в теле нет вызова recur значит итерации не будет. вместо этого используется рекурсия