From 99c99e6dad59c1abf9aaed162021325e2025660c Mon Sep 17 00:00:00 2001 From: Cameron Candelori Date: Fri, 2 May 2025 17:33:11 -0400 Subject: [PATCH] complete structured data exercises --- src/structured_data.clj | 105 ++++++++++++++++++++++++---------------- 1 file changed, 64 insertions(+), 41 deletions(-) diff --git a/src/structured_data.clj b/src/structured_data.clj index ebfe1ce4..c302e42e 100644 --- a/src/structured_data.clj +++ b/src/structured_data.clj @@ -1,16 +1,17 @@ (ns structured-data) (defn do-a-thing [x] - :-) + (let [xx (+ x x)] + (Math/pow xx xx))) (defn spiff [v] - :-) + (+ (first v) (get v 2))) (defn cutify [v] - :-) + (conj v "<3")) -(defn spiff-destructuring [v] - :-) +(defn spiff-destructuring [[x _ z]] + (+ x z)) (defn point [x y] [x y]) @@ -18,97 +19,119 @@ (defn rectangle [bottom-left top-right] [bottom-left top-right]) -(defn width [rectangle] - :-) +(defn width [[[x0 _] [x1 _]]] + (- x1 x0)) -(defn height [rectangle] - :-) +(defn height [[[_ y0] [_ y1]]] + (- y1 y0)) (defn square? [rectangle] - :-) + (= (height rectangle) (width rectangle))) (defn area [rectangle] - :-) + (* (height rectangle) (width rectangle))) -(defn contains-point? [rectangle point] - :-) +(defn contains-point? [[[x0 y0] [x1 y1]] [px py]] + (and (<= x0 px x1) (<= y0 py y1))) -(defn contains-rectangle? [outer inner] - :-) +(defn contains-rectangle? [outer [[inx0 iny0] [inx1 iny1]]] + (and (contains-point? outer [inx0 iny0]) + (contains-point? outer [inx1 iny1]))) (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] - :-) +(defn add-author [book new-author] + (assoc book + :authors + (conj (:authors book) new-author))) (defn alive? [author] - :-) + (not (contains? author :death-year))) (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] - :-) + (let [name (:name author) + birth (:birth-year author) + death (:death-year author)] + (str name + (if birth + (str " (" birth " - " (or death "") ")") + "")))) (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] - :-) + (if (empty? books) + "No books." + (let [n (count books) + header (str n " " (if (= n 1) "book." "books.")) + bodies (apply str (interpose ". " (map book->string books)))] + (str header " " bodies ".")))) (defn books-by-author [author books] - :-) + (filter #(has-author? % author) books)) (defn author-by-name [name authors] - :-) + (first (filter #(= name (:name %)) authors))) (defn living-authors [authors] - :-) + (filter alive? authors)) (defn has-a-living-author? [book] - :-) + (not (empty? (filter alive? (:authors book))))) (defn books-by-living-authors [books] - :-) + (filter has-a-living-author? books)) ; %________%