From 8c900325b814e39950ce8a4d1722c9c5cad89987 Mon Sep 17 00:00:00 2001 From: Jani-Matti Tirkkonen Date: Sun, 10 Feb 2019 22:58:42 +0200 Subject: [PATCH 1/3] do exercices 1-27 --- src/structured_data.clj | 71 +++++++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 28 deletions(-) diff --git a/src/structured_data.clj b/src/structured_data.clj index ebfe1ce4..467dc994 100644 --- a/src/structured_data.clj +++ b/src/structured_data.clj @@ -1,16 +1,17 @@ (ns structured-data) (defn do-a-thing [x] - :-) + (let [twox (+ x x)] + (Math/pow twox twox))) (defn spiff [v] - :-) + (+ (first v)(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,76 +20,90 @@ [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] - :-) + (= (height rectangle) (width rectangle))) (defn area [rectangle] - :-) + (* (height rectangle) (width 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 [[[xo1 yo1] [xo2 yo2]] outer + [[xi1 yi1] [xi2 yi2]] inner] + (and (<= xo1 xi1 xi2 xo2) (<= yo1 yi1 yi2 yo2)))) (defn title-length [book] - :-) + (count (:title book))) (defn author-count [book] - :-) + (count (:authors book))) (defn multiple-authors? [book] - :-) + (< 1 (author-count book))) (defn add-author [book new-author] - :-) + (assoc book :authors (conj (book :authors) new-author))) + (defn alive? [author] - :-) + (nil? (get author :death-year))) (defn element-lengths [collection] - :-) + (map count collection)) (defn second-elements [collection] - :-) + (map fnext 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 (book :authors)))) (defn has-author? [book author] - :-) + (contains? (book :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 [name (author :name) + birth (author :birth-year) + death (author :death-year)] + (str name + (if birth (str " (" birth " - " death ")"))))) (defn authors->string [authors] - :-) + (clojure.string/join ", " (map author->string authors))) (defn book->string [book] :-) From be5f7d63b723c9d40667cea0fcf07e7203f48415 Mon Sep 17 00:00:00 2001 From: Jani-Matti Tirkkonen Date: Mon, 11 Feb 2019 08:31:14 +0200 Subject: [PATCH 2/3] solve all the exercises --- src/structured_data.clj | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/src/structured_data.clj b/src/structured_data.clj index 467dc994..1d27816a 100644 --- a/src/structured_data.clj +++ b/src/structured_data.clj @@ -106,24 +106,35 @@ (clojure.string/join ", " (map author->string authors))) (defn book->string [book] - :-) + (let [title (book :title) + authors (authors->string (book :authors))] + (str title ", written by " authors))) (defn books->string [books] - :-) + (str + (if (= (count books) 0) + "No books." + (str (count books) + (if (= (count books) 1) + (str " book. " + (str (apply book->string books)) ".") + (str " books. " + (clojure.string/join "" (map #(str (book->string %) ". ") books)))))))) + (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? (living-authors (:authors book))))) (defn books-by-living-authors [books] - :-) + (filter has-a-living-author? books)) ; %________% From 4d8470ccb134105a45f467122b16338f0e301cc5 Mon Sep 17 00:00:00 2001 From: Jani-Matti Tirkkonen Date: Thu, 30 May 2019 14:38:28 +0300 Subject: [PATCH 3/3] fix travis build --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 455f3c0e..45c29f60 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ language: clojure -lein: lein2 -script: lein2 midje :config .midje-grading-config.clj +lein: lein +script: lein midje :config .midje-grading-config.clj jdk: - openjdk7 notifications: