From 88b49f714788ec2f6e2024f3feb692d3aecb8a87 Mon Sep 17 00:00:00 2001 From: leaen Date: Tue, 29 Oct 2019 17:12:24 +1100 Subject: [PATCH] Exercises for structured-data --- src/structured_data.clj | 95 ++++++++++++++++++++++++++--------------- 1 file changed, 60 insertions(+), 35 deletions(-) diff --git a/src/structured_data.clj b/src/structured_data.clj index ebfe1ce4..432842d4 100644 --- a/src/structured_data.clj +++ b/src/structured_data.clj @@ -1,16 +1,21 @@ (ns structured-data) (defn do-a-thing [x] - :-) + (let [double (+ x x)] + (Math/pow double double))) (defn spiff [v] - :-) + (if (>= (count v) 3) + (+ (get v 0) (get v 2)) + nil + )) (defn cutify [v] - :-) + (conj v "<3")) (defn spiff-destructuring [v] - :-) + (let [[a _ b] v] + (+ a b))) (defn point [x y] [x y]) @@ -19,96 +24,116 @@ [bottom-left top-right]) (defn width [rectangle] - :-) + (let [[[x1 y1] [x2 y2]] rectangle] + (- x2 x1))) (defn height [rectangle] - :-) + (let [[[x1 y1] [x2 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 + [px py] point] + (and (<= x1 px x2) (<= y1 py y2)))) (defn contains-rectangle? [outer inner] - :-) + (let [[p1 p2] inner] + (and (contains-point? outer p1) + (contains-point? outer p2)) + )) (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] - :-) + (assoc book :authors (conj (:authors book) new-author))) (defn alive? [author] - :-) + (nil? (:death-year author))) (defn element-lengths [collection] - :-) + (map count collection)) (defn second-elements [collection] - :-) + (map second 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] - :-) + (assoc book :authors (set (:authors book)))) (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] - :-) + (str (:name author) + (when (:birth-year author) + (str " (" (:birth-year author) " - " (:death-year author) ")")) + )) (defn authors->string [authors] - :-) + (apply str (interpose ", " (map author->string authors)))) (defn book->string [book] - :-) + (str (:title book) + ", written by " + (authors->string (:authors book)))) (defn books->string [books] - :-) + (cond + (== (count books) 0) "No books." + (== (count books) 1) (str "1 book. " (book->string (first books)) ".") + :else (let [num_books (str (count books) " books. ") + books_str (apply str (interpose ". " (map book->string books)))] + (str num_books books_str ".")))) (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 x) name)) authors))) (defn living-authors [authors] - :-) + (filter (fn [x] (not (contains? x :death-year))) 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)) ; %________%