From 026f35867f5176200cbd924a0b7dec6fb107abd3 Mon Sep 17 00:00:00 2001 From: rkelkka Date: Sun, 10 Sep 2017 20:19:36 +0300 Subject: [PATCH 1/5] Structured data: exercises 1 - 18 --- src/structured_data.clj | 56 +++++++++++++++++++++++++++-------------- 1 file changed, 37 insertions(+), 19 deletions(-) diff --git a/src/structured_data.clj b/src/structured_data.clj index ebfe1ce4..ca6a58d9 100644 --- a/src/structured_data.clj +++ b/src/structured_data.clj @@ -1,16 +1,20 @@ (ns structured-data) (defn do-a-thing [x] - :-) + (let [plus (+ x x)] + (Math/pow plus plus))) (defn spiff [v] - :-) + (let [fst (get v 0) + trd (get v 2)] + (+ fst trd))) (defn cutify [v] - :-) + (conj v "<3")) (defn spiff-destructuring [v] - :-) + (let [[x y z] v] + (+ x z))) (defn point [x y] [x y]) @@ -19,52 +23,66 @@ [bottom-left top-right]) (defn width [rectangle] - :-) + (let [[blx bly] (get rectangle 0) + [trx try] (get rectangle 1)] + (- trx blx))) + + + + (defn height [rectangle] - :-) + (let [[blx bly] (get rectangle 0) + [trx try] (get rectangle 1)] + (- try bly))) (defn square? [rectangle] - :-) + (= (height rectangle) (width rectangle))) (defn area [rectangle] - :-) + (* (height rectangle) (width rectangle))) (defn contains-point? [rectangle point] - :-) + (let [[blx bly] (get rectangle 0) + [trx try] (get rectangle 1) + [px py] point] + (and (<= blx px trx) (<= bly py try)))) (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] - :-) + (let [with_new_author (conj (:authors book) new-author)] + (assoc book :authors with_new_author))) (defn alive? [author] - :-) + (not (contains? author :death-year))) (defn element-lengths [collection] - :-) + (map count collection)) (defn second-elements [collection] - :-) + (let [get_second (fn [x] (get x 1))] + (map get_second collection))) (defn titles [books] - :-) + (map :title books)) (defn monotonic? [a-seq] :-) (defn stars [n] - :-) + (apply str (repeat n "*"))) (defn toggle [a-set elem] :-) From 776877950e48e4488020f8de71c27fc3fa47b64a Mon Sep 17 00:00:00 2001 From: rkelkka Date: Sun, 5 Nov 2017 19:01:26 +0200 Subject: [PATCH 2/5] Structured data: exercises 19 - 25 --- src/structured_data.clj | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/structured_data.clj b/src/structured_data.clj index ca6a58d9..ce93fa28 100644 --- a/src/structured_data.clj +++ b/src/structured_data.clj @@ -79,28 +79,33 @@ (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] - :-) + (< (count (set a-seq)) (count 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] - :-) + (let [auths (map :authors books)] + (apply clojure.set/union auths))) + (defn all-author-names [books] - :-) + (set (map :name (authors books)))) (defn author->string [author] :-) From 1157d9b2144795e27557e70ac02d826ab187211f Mon Sep 17 00:00:00 2001 From: rkelkka Date: Sun, 28 Jan 2018 21:08:10 +0200 Subject: [PATCH 3/5] Structured data: exercises 26 - 34 (all done) --- src/structured_data.clj | 54 ++++++++++++++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 9 deletions(-) diff --git a/src/structured_data.clj b/src/structured_data.clj index ce93fa28..a5d779d9 100644 --- a/src/structured_data.clj +++ b/src/structured_data.clj @@ -107,31 +107,67 @@ (defn all-author-names [books] (set (map :name (authors books)))) + +(defn a-birth-and-death-years [b-y, d-y] + (cond + (nil? b-y) "" + (nil? d-y) (format " (%s - )" b-y) + :else (format " (%s - %s)" b-y, d-y)) +) + (defn author->string [author] - :-) + (let [a-name (author :name)] + (let [b-y (author :birth-year)] + (let [d-y (author :death-year)] + (format "%s%s" a-name (a-birth-and-death-years b-y d-y)) + ) + ) + ) + ) + (defn authors->string [authors] - :-) + (apply str(interpose ", " (map author->string authors)))) (defn book->string [book] - :-) + (let [b-title (book :title)] + (let [b-strings (authors (list book))] + (format "%s, written by %s" b-title (authors->string b-strings)) + ) + ) +) (defn books->string [books] - :-) + (let [b-string (apply str(interpose ", " (map book->string books)))] + (cond + (= 0 (count books)) "No books." + (= 1 (count books)) (format "1 book. %s." b-string) + :else (format "%d books. %s." (count books) b-string)) + + ) + ) (defn books-by-author [author books] - :-) + (filter (fn [b] (has-author? b author)) books) + + ) (defn author-by-name [name authors] - :-) + (first (filter (fn [a] (= name (a :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 d0c1604a0146e141eb862f6cb01d79c3e0ae8121 Mon Sep 17 00:00:00 2001 From: rkelkka Date: Sun, 14 Oct 2018 16:10:56 +0300 Subject: [PATCH 4/5] Structured data: exercises 26 - 34 (all done) rerun --- src/structured_data.clj | 1 + 1 file changed, 1 insertion(+) diff --git a/src/structured_data.clj b/src/structured_data.clj index a5d779d9..faf24e06 100644 --- a/src/structured_data.clj +++ b/src/structured_data.clj @@ -168,6 +168,7 @@ (defn books-by-living-authors [books] (filter has-a-living-author? books) + ) ; %________% From ae963748d11d1edf90ade0942a62fe7999f42366 Mon Sep 17 00:00:00 2001 From: rkelkka Date: Sun, 14 Oct 2018 16:42:25 +0300 Subject: [PATCH 5/5] Fix travis build script --- .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: