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

otus-04 hw #1

Closed
wants to merge 8 commits into from
Closed

otus-04 hw #1

wants to merge 8 commits into from

Conversation

gtrsh
Copy link

@gtrsh gtrsh commented Nov 21, 2024

No description provided.

Copy link

@Ivana- Ivana- left a comment

Choose a reason for hiding this comment

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

Хорошо, но постарайтесь реализовывать более простые и лаконичные решения, и пользоваться готовыми функциями/макросами из стандартной библиотеки

(defn next-coords
[next-index-col next-index-row {:keys [col row]}]
{:row (next-index-row row)
:col (next-index-col col)})
Copy link

Choose a reason for hiding this comment

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

Вопрос вкуса, можно еще (-> m (update :row next-index-row) (update :col next-index-col))

[item n]
(->> item
(repeat n)
(vec)))
Copy link

Choose a reason for hiding this comment

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

Можно просто vec без скобок

(defn reducer [acc val]
(if (acc val)
(update acc val inc)
(assoc acc val 1)))
Copy link

Choose a reason for hiding this comment

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

Откройте для себя fnil : (update acc val (fnil inc 0))


(defn word->map
[word]
(reduce reducer {} word))
Copy link

Choose a reason for hiding this comment

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

Откройте для себя frequencies :)

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)))
Copy link

Choose a reason for hiding this comment

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

Откройте для себя merge-with Итого весь код решения

(defn scramble? [letters word]
  (every? (comp not pos? second)
          (merge-with -
                      (frequencies word)
                      (frequencies (filter (set word) letters)))))

new-row (assoc square-row col square-val)]

(assoc acc row new-row)))
empty-vec (keys square-map))))
Copy link

Choose a reason for hiding this comment

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

Хорошо, но многословно. Сравните, для примера, с вариантом

(defn magic-square [n]
  (let [sq (->> 0 (repeat n) vec (repeat n) vec)
        move (fn [sq [r c]]
               (let [norm (fn [x] (cond (< x 0) (dec n) (>= x n) 0 :else x))
                     rc' [(-> r dec norm) (-> c inc norm)]]
                 (if (zero? (get-in sq rc'))
                   rc'
                   [(-> r inc norm) c])))
        rc-start [0 (quot n 2)]]
    (loop [rc rc-start
           [i & is] (range 2 (inc (* n n)))
           sq (assoc-in sq rc-start 1)]
      (if-not i
        sq
        (let [rc' (move sq rc)]
          (recur rc' is (assoc-in sq rc' i)))))))


;; Logic
(defn- get-idx [i]
(Math/floor (/ i alphabet-size)))
Copy link

Choose a reason for hiding this comment

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

quot

@gtrsh gtrsh closed this by deleting the head repository Jan 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants