Skip to content
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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions src/new_hope/week1.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Copy link
Owner

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

(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]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

flatten works on all levels,

(dupl [[1][2]]) => [1 1 2 2] ;; instead of [[1] [1] [2] [2]]

(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])
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use comment block to prevent evaluation when the source file is loaded.


;; Day 5

(defn compress [col]
(if (empty? col)
Copy link
Owner

Choose a reason for hiding this comment

The 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)
Copy link
Owner

Choose a reason for hiding this comment

The 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)
Copy link
Owner

Choose a reason for hiding this comment

The 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))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should check indexes, not values. This will fail:
(drop-nth [:a :b :c :d :e :f :g] 3)


(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))))
Expand Down