Skip to content

Commit

Permalink
submisson
Browse files Browse the repository at this point in the history
  • Loading branch information
stephclay committed Dec 28, 2018
1 parent 0ab503e commit 68058d6
Showing 1 changed file with 54 additions and 39 deletions.
93 changes: 54 additions & 39 deletions src/structured_data.clj
Original file line number Diff line number Diff line change
@@ -1,114 +1,129 @@
(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])

(defn rectangle [bottom-left top-right]
[bottom-left top-right])

(defn width [rectangle]
:-)
(defn width [[[x1 y1] [x2 y2]]]
(- x2 x1))

(defn height [rectangle]
:-)
(defn height [[[x1 y1] [x2 y2]]]
(- y2 y1))

(defn square? [rectangle]
:-)
(= (width rectangle) (height rectangle)))

(defn area [rectangle]
:-)
(* (width rectangle) (height rectangle)))

(defn contains-point? [rectangle point]
:-)
(defn contains-point? [[[rx1 ry1] [rx2 ry2]] [px py]]
(and (<= rx1 px rx2) (<= ry1 py ry2))
)

(defn contains-rectangle? [outer inner]
:-)
(defn contains-rectangle? [outer [inner1 inner2]]
(and (contains-point? outer inner1) (contains-point? outer inner2))
)

(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 [authors (:authors book)
new-authors (conj authors 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 (fn [x] (first (rest x)))]
(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]
:-)
(> (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 [date-suffix (if (contains? author :birth-year) (str " (" (:birth-year author) " - " (:death-year author) ")") "")]
(str (:name author) date-suffix)))

(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]
:-)
(let [num-books (count books)
book-str (cond (== num-books 0) "No books" (== num-books 1) "1 book. " :else (str num-books " books. "))]
(str book-str (apply str (interpose ". " (map book->string books))) ".")))

(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 (:name author))) 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))

; %________%

0 comments on commit 68058d6

Please sign in to comment.