From b6efb63668b26018bf56068d24f7be9357e9e4c5 Mon Sep 17 00:00:00 2001 From: Pedro Freitas Date: Tue, 14 Jan 2020 10:27:30 +0000 Subject: [PATCH] Dead authors society --- src/structured_data.clj | 107 +++++++++++++++++++++++++++------------- 1 file changed, 72 insertions(+), 35 deletions(-) diff --git a/src/structured_data.clj b/src/structured_data.clj index ebfe1ce4..4e63ae85 100644 --- a/src/structured_data.clj +++ b/src/structured_data.clj @@ -1,16 +1,22 @@ (ns structured-data) (defn do-a-thing [x] - :-) + (let [two-x (+ x x)] + (Math/pow two-x two-x))) (defn spiff [v] - :-) + (if (>= (count v) 3) + (+ (get v 0) (get v 2)) + nil)) (defn cutify [v] - :-) + (conj v "<3")) (defn spiff-destructuring [v] - :-) + (if (>= (count v) 3) + (let [[a _ b] v] + (+ a b)) + nil)) (defn point [x y] [x y]) @@ -19,96 +25,127 @@ [bottom-left top-right]) (defn width [rectangle] - :-) + (let [[[x1 _] [x2 _]] rectangle] + (- x2 x1))) (defn height [rectangle] - :-) + (let [[[_ y1] [_ y2]] rectangle] + (- 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 + [xp yp] point] + (and (<= x1 xp x2) (<= y1 yp y2)))) (defn contains-rectangle? [outer inner] - :-) + (let [[bl tr] inner] + (and (contains-point? outer bl) (contains-point? outer tr)))) (defn title-length [book] - :-) + (count (:title book))) (defn author-count [book] - :-) + (count (:authors book))) (defn multiple-authors? [book] - :-) + (> (author-count book) 1)) (defn add-author [book new-author] - :-) + (let [authors (:authors book) + updated-authors (conj authors new-author)] + (assoc book :authors updated-authors))) (defn alive? [author] - :-) + (nil? (:death-year author))) (defn element-lengths [collection] - :-) + (map count collection)) (defn second-elements [collection] - :-) + (let [se (fn [col] (get col 1))] + (map se 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] - :-) + (not= (count a-seq) (count (set a-seq)))) (defn old-book->new-book [book] - :-) + (let [authors-set (set (:authors book))] + (assoc book :authors authors-set))) (defn has-author? [book author] - :-) + (contains? (:authors book) 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 [name (:name author) + birth (:birth-year author) + death (:death-year author)] + (cond + (:death-year author) (str name " (" birth " - " death ")") + (:birth-year author) (str name " (" birth " - )") + :else name))) (defn authors->string [authors] - :-) + (apply str (interpose ", " (map author->string authors)))) (defn book->string [book] - :-) + (let [title (:title book) + authors (authors->string (:authors book))] + (str title ", written by " authors))) (defn books->string [books] - :-) + (let [book-count (count books) + book-str (fn [book] + (str (book->string book) ".")) + book-strs (map book-str books)] + (cond + (= 0 book-count) "No books." + (= 1 book-count) (str "1 book. " (first book-strs)) + :else + (apply str book-count " books. " (interpose " " book-strs))))) + + (defn books-by-author [author books] - :-) + (filter (fn [book] (has-author? book author)) books)) (defn author-by-name [name authors] - :-) + (first (filter (fn [author] (= name (:name author))) authors))) (defn living-authors [authors] - :-) + (filter alive? authors)) (defn has-a-living-author? [book] - :-) + (not (empty? (living-authors (:authors book))))) (defn books-by-living-authors [books] - :-) + (filter has-a-living-author? books)) ; %________%