From 3112d6f208fea35c482a7bc0e2aac445b5864393 Mon Sep 17 00:00:00 2001 From: suarela Date: Thu, 10 Aug 2017 14:45:30 +0300 Subject: [PATCH] structured-data done --- src/structured_data.clj | 110 ++++++++++++++++++++++++++-------------- 1 file changed, 73 insertions(+), 37 deletions(-) diff --git a/src/structured_data.clj b/src/structured_data.clj index ebfe1ce4..65d67056 100644 --- a/src/structured_data.clj +++ b/src/structured_data.clj @@ -1,16 +1,20 @@ (ns structured-data) (defn do-a-thing [x] - :-) + (let [xx (+ x x)] + (Math/pow xx xx))) (defn spiff [v] - :-) + (let [a (get v 0) + b (get v 2)] + (+ a b))) (defn cutify [v] - :-) + (conj v "<3")) (defn spiff-destructuring [v] - :-) + (let [[a b c] v] + (+ a c))) (defn point [x y] [x y]) @@ -18,97 +22,129 @@ (defn rectangle [bottom-left top-right] [bottom-left top-right]) -(defn width [rectangle] - :-) +(defn width [[[x1 y1][x2 y2]]] + (- x2 x1)) -(defn height [rectangle] - :-) +(defn height [[[x1 y1][x2 y2]]] + (- y2 y1)) (defn square? [rectangle] - :-) + (== (width rectangle) + (height rectangle))) (defn area [rectangle] - :-) + (* (width rectangle) + (height rectangle))) (defn contains-point? [rectangle point] - :-) + (let [[[x1 y1][x2 y2]] rectangle + [px py] point] + (and (<= x1 px x2) + (<= y1 py y2)))) (defn contains-rectangle? [outer inner] - :-) + (let [[[x1 y1][x2 y2]] inner] + (and (contains-point? outer [x1 y1]) + (contains-point? outer [x2 y2])))) (defn title-length [book] - :-) + (count (get book :title))) (defn author-count [book] - :-) + (count (get book :authors))) (defn multiple-authors? [book] - :-) + (< 1 (author-count book))) (defn add-author [book new-author] - :-) + (let [authors (get book :authors) + new-authors (get new-author :name) + merged (conj authors new-author)] + (assoc book :authors merged))) (defn alive? [author] - :-) + (not (contains? author :death-year))) (defn element-lengths [collection] - :-) + (map count collection)) (defn second-elements [collection] - :-) + (let [second-element (fn [vector] (get vector 1))] + (map second-element collection))) (defn titles [books] - :-) + (map :title books)) (defn monotonic? [a-seq] - :-) + (or (apply <= a-seq) + (apply >= a-seq))) (defn stars [n] - :-) + (apply str (repeat n \*))) (defn toggle [a-set elem] - :-) + (if (contains? a-set elem) + (disj a-set elem) + (conj a-set elem))) (defn contains-duplicates? [a-seq] - :-) + (let [seq-size (count a-seq) + set-size (count (set a-seq))] + (not (== seq-size set-size)))) (defn old-book->new-book [book] - :-) + (let [in-vect (get book :authors) + in-set (set in-vect)] + (assoc book :authors in-set))) (defn has-author? [book author] - :-) + (let [authors (get book :authors)] + (contains? authors author))) (defn authors [books] - :-) + (apply clojure.set/union (map :authors books))) (defn all-author-names [books] - :-) + (set (map :name (authors books)))) (defn author->string [author] - :-) + (let [result (fn [a b d] (str a " (" b " - " d ")")) + author-n (get author :name) + birth-y (get author :birth-year) + death-y (get author :death-year)] + (if (contains? author :birth-year) + (result author-n birth-y death-y) + (str author-n)))) (defn authors->string [authors] - :-) + (apply str (interpose ", " (map author->string authors)))) (defn book->string [book] - :-) + (let [authors (authors->string (get book :authors)) + title (get book :title)] + (str title ", written by " authors))) (defn books->string [books] - :-) + (let [books-count (count books) + books-authors (interpose ", " (map book->string books))] + (cond + (<= 2 books-count) (str (apply str books-count " books. " books-authors) ".") + (== 1 books-count) (str (apply str "1 book. " books-authors) ".") + :else (str "No books.")))) (defn books-by-author [author books] - :-) + (filter (fn [x] (has-author? x author)) books)) (defn author-by-name [name authors] - :-) + (first (filter (fn [x] (= name (get x :name))) authors))) (defn living-authors [authors] - :-) + (filter (fn [x] (alive? x)) authors)) (defn has-a-living-author? [book] - :-) + (not (empty? (living-authors (get book :authors))))) (defn books-by-living-authors [books] - :-) + (filter (fn [x] (has-a-living-author? x)) books)) ; %________%