-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Week 1 #1
base: master
Are you sure you want to change the base?
Week 1 #1
Changes from 1 commit
80d81e8
d1f3fb9
c7fd756
37ca466
863560a
96759b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,8 +21,121 @@ | |
(facts "some facts" | ||
(second [2 3 4]) => 3) | ||
|
||
|
||
;; Day 3 | ||
|
||
(re-seq #"jam" "I like jam in my jam") | ||
|
||
(apply str [1 2 3]) | ||
|
||
(apply str (re-seq #"A-Z+" "AadfBdfasZaa")) | ||
|
||
(.toUpperCase (str (first [:cat :dog :fish]))) | ||
|
||
(-> [:cat :dog :fish] | ||
first | ||
str | ||
.toUpperCase) | ||
|
||
(->> [1 2 3 4 5 6 7 8] | ||
(filter odd?) | ||
(take 2)) | ||
|
||
(for [x (range 40) | ||
:when (= 1 (rem x 4))] | ||
x) | ||
|
||
(for [x (iterate #(+ 4 %) 0) | ||
:let [z (inc x)] | ||
:while (< z 40)] | ||
x) | ||
|
||
(for [[x y] (partition 2 (range 20))] | ||
[x y]) | ||
|
||
;; Day 4 | ||
|
||
(defn second-to-last [col] | ||
(if (< (count col) 2) | ||
nil | ||
(nth col (- (count col) 2)))) | ||
|
||
(facts "should return the second to last" | ||
(second-to-last (list 1 2 3 4 5)) => 4 | ||
(second-to-last ["a" "b" "c"]) => "b" | ||
(second-to-last [[1 2] [2 3]]) => [1 2] | ||
(second-to-last []) => nil | ||
(second-to-last [1]) => nil) | ||
|
||
(defn sum [col] | ||
(reduce + col)) | ||
|
||
(facts "should return the sum" | ||
(sum [1 2 3]) => 6) | ||
|
||
(defn odd-numbers [col] | ||
(filter odd? col)) | ||
|
||
(facts "should return the odd numbers" | ||
(odd-numbers #{1 2 3 4 5}) => '(1 3 5)) | ||
|
||
(defn palindrome [col] | ||
(let [size (count col) | ||
half-count (quot size 2) | ||
nr-of-dropped (if (even? size) half-count (inc half-count)) | ||
first-half (take half-count col) | ||
second-half (drop nr-of-dropped col)] | ||
(= first-half (reverse second-half)))) | ||
|
||
(facts "should return true for palindrome" | ||
(palindrome "racecar") => true | ||
(palindrome [1 2 3 4 5]) => false | ||
(palindrome [1 2 3 2 1]) => true) | ||
|
||
(defn dupl [col] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. flatten works on all levels,
|
||
(flatten (map (fn [x] [x x]) col))) | ||
|
||
(facts "should duplicate elements" | ||
(dupl [1 2 3]) => '(1 1 2 2 3 3)) | ||
|
||
(palindrome [1 2 4 5]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
|
||
;; Day 5 | ||
|
||
(defn compress [col] | ||
(if (empty? col) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This check is excessive. Try to design the termination check inside loop so that it catches empty input as well. |
||
col | ||
(loop [elem (first col) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest renaming elem -> prev-elem to incease readability |
||
rest-col (rest col) | ||
result []] | ||
(if (empty? rest-col) | ||
(conj result elem) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this conj be avoided? We already have conj call below, there should be only one enough |
||
(let [new-elem (first rest-col) | ||
new-result (if (= elem new-elem) | ||
result | ||
(conj result elem))] | ||
(recur new-elem (rest rest-col) new-result)))))) | ||
|
||
(facts "should compress a seq" | ||
(compress [1]) => [1] | ||
(compress []) => [] | ||
(compress [1 1 2 2 3 3 3 4 4 5 3]) => [1 2 3 4 5 3]) | ||
|
||
(defn drop-nth [col n] | ||
(filter #(not= 0 (rem % n)) col)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should check indexes, not values. This will fail: |
||
|
||
(facts "should drop every nth element" | ||
(drop-nth [1 2 3 4 5 6 7 8] 3) => [1 2 4 5 7 8]) | ||
|
||
|
||
(take 5 (iterate inc 1)) | ||
|
||
(defn replic [col n] | ||
(flatten (map #(take n (iterate identity %)) col))) | ||
|
||
(facts "should replace each element a number of times" | ||
(replic [1 2 3] 2) => [1 1 2 2 3 3]) | ||
|
||
(def second-to-last | ||
(fn [coll] | ||
(second (reverse coll)))) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, a simpler solution exists, it's enough to compare the original sequence to its reversed version