From d8e03f881e356fd318f25f3c0fad8524cf397201 Mon Sep 17 00:00:00 2001 From: Joni Toivonen Date: Tue, 15 Aug 2017 17:01:19 +0300 Subject: [PATCH] Fix code so that tests pass --- src/structured_data.clj | 100 ++++++++++++++++++++++++++-------------- 1 file changed, 65 insertions(+), 35 deletions(-) diff --git a/src/structured_data.clj b/src/structured_data.clj index ebfe1ce4..5e7ce896 100644 --- a/src/structured_data.clj +++ b/src/structured_data.clj @@ -1,16 +1,18 @@ (ns structured-data) (defn do-a-thing [x] - :-) + (let [xx (+ x x)] + (Math/pow xx xx))) (defn spiff [v] - :-) + (+ (get v 0) (get v 2))) (defn cutify [v] - :-) + (conj v "<3")) (defn spiff-destructuring [v] - :-) + (let [[x y z] v] + (+ x z))) (defn point [x y] [x y]) @@ -19,96 +21,124 @@ [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 + [x3 y3] point] + (and (<= x1 x3 x2) (<= y1 y3 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 (:title book))) (defn author-count [book] - :-) + (count (:authors book))) (defn multiple-authors? [book] - :-) + (> (author-count book) 1)) (defn add-author [book new-author] - :-) + (let [new-authors + (conj + (:authors book) + new-author)] + (assoc book :authors new-authors))) (defn alive? [author] - :-) + (not (contains? author :death-year))) (defn element-lengths [collection] - :-) + (map count collection)) (defn second-elements [collection] - :-) + (let [second-element (fn [coll] (get coll 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] - :-) + (if (= (count a-seq) (count (set a-seq))) + false + true)) (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 [author-name (:name author) + birth-year (:birth-year author) + death-year (:death-year author)] + (if birth-year + (str author-name " (" birth-year " - " death-year ")") + (str author-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) + books-authors (interpose ", " (map book->string books))] + (cond + (= book-count 0) "No books." + (= book-count 1) (str (apply str "1 book. " books-authors) ".") + :else (str (apply str book-count " books. " books-authors) ".")))) (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 author) name)) authors))) (defn living-authors [authors] - :-) + (filter (fn [author] (alive? author)) authors)) (defn has-a-living-author? [book] - :-) + (not (empty? (living-authors (:authors book))))) (defn books-by-living-authors [books] - :-) + (filter (fn [book] (has-a-living-author? book)) books)) ; %________%