Skip to content
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

HW04 - домашнее задание. #12

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions otus-01/src/otus/homework.clj
Original file line number Diff line number Diff line change
@@ -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" []
(double (/ (+ 5 (+ 4 (- 2 (- 3 (+ 6 (/ 4 5))))))
(* 3 (* (- 2 7) (- 6 2))))))
5 changes: 3 additions & 2 deletions otus-02/src/otus_02/homework/palindrome.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns otus-02.homework.palindrome
(:require [clojure.string :as string]))


(defn is-palindrome [test-string])
(defn is-palindrome [test-string]
(let [normal-str (string/upper-case (string/replace test-string #"[^a-zA-Z0-9]" ""))
invert-str (string/reverse normal-str)] (= normal-str invert-str)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"тело" в let лучше переносить на новую строчку.


17 changes: 15 additions & 2 deletions otus-02/src/otus_02/homework/pangram.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
(ns otus-02.homework.pangram
(:require [clojure.string :as string]))

;;(defn is-pangram [test-string]
;; (let [alphabet "abcdefghijklmnopqrstuvwxyz"
;; alph-vec (map str (vec alphabet))
;; normal-string1 (string/lower-case test-string)
;; normal-string (map str
;; (filter #(> (.indexOf alphabet (str %1)) -1)
;; normal-string1))
;; chr-in-alpha-list (map #(.indexOf normal-string %) alph-vec)]
;; (= (count (filter #(= % -1) chr-in-alpha-list)) 0)))

(defn is-pangram [test-string])

(defn is-pangram [test-string]
(let [alphabet "abcdefghijklmnopqrstuvwxyz"
normal-string (-> test-string
(string/replace #"[\s\W]" "")
string/lower-case)]
(= (set alphabet) (set normal-string))))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Раз уж с множествами работаете, то можно было бы проверить, что пересечение (set alphabet) и (set test-string) равно (set alphabet) - это и есть признак того, что все буквы были использованы :) В этом случае никакие дополнительные действия над входной строкой не нужны.

13 changes: 6 additions & 7 deletions otus-02/test/otus_02/homework/fizzbuzz.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
(:require
[clojure.test :refer :all]))


(defn fizz-buzz [n]
"Создайте программу, которая выводит числа от 1 до n.
- Если число делится на 3, выведите 'Fizz';
- если число делится на 5, выведите 'Buzz';
- если число делится и на 3 и на 5, выведите 'FizzBuzz'."
"implement me")

(map
#(cond
(true? (and (= 0 (mod % 3)) (= 0 (mod % 5)))) "FizzBuzz"
(= 0 (mod % 3)) "Fizz"
(= 0 (mod % 5)) "Buzz"
:else %) (range 1 (+ n 1) 1)))

(deftest fizz-buzz-test
(is (= (fizz-buzz 10)
Expand Down
4 changes: 3 additions & 1 deletion otus-04/src/otus_04/homework/scramblies.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
"Функция возвращает true, если из букв в строке letters
можно составить слово word."
[letters word]
nil)
(let [letters-set (set (mapv str letters))
word-set (set (mapv str word))]
(= (clojure.set/intersection letters-set word-set) word-set)))
Copy link
Contributor

@astynax astynax Nov 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А вот здесь использование множеств не сработает. Потому что, приводя строку к множеству, вы теряете информацию о количестве повторов букв. А чтобы составить word из letters может потребовать более одного экземпляра той или иной буквы.

Вы точно запускали тесты? Среди тестовых примеров точно есть такой, в которому буквы в собираемом слове повторяются.