 # 4Clojure

Clojure problems.

# Notes

## Problem 14

4 different ways to create a function:

``````(fn add-five [x] (+ x 5))
(fn [x] (+ x 5))
#(+ % 5)
(partial + 5)

(defn f [s] (clojure.string/join (re-seq #"[A-Z]+" s)))

["wow"]

(f "Wow This Extracts The Capitals")
``````
 #function[user/eval6280/add-five--6281] #function[user/eval6284/fn--6285] #function[user/eval6290/fn--6291] #function[clojure.core/partial/fn--5839] #'user/f ["wow"] "WTETC"

## Problem 27 Palindrom Detector

``````(fn [s] (loop [x s]
(if (= (first x) (first (reverse x)))
()
false
)
))
``````

## Problem 39 Interleave

Heres the input: `[1 2 3] [:a :b :c]`

This is a map, When you pass two sequences to map, it will pass [1, :a] together, [2, :b] together etc. Basically it zips the columns together.

``````(map (fn [x y] (list [x y]) ) [1 2 3] [:a :b :c])
``````
 (1 :a) (2 :b) (3 :c)

Now we need to put all of these together and flatten the list.

``````(flatten(map (fn [x y] (list [x y]) ) [1 2 3] [:a :b]))

(defn my-zip [a b]
(flatten(map (fn [x y] (list [x y]) ) a b)))

(my-zip [1 2 3] [:a :b :c])
``````
 (1 :a 2 :b) #'user/my-zip (1 :a 2 :b 3 :c)

Now we need to get this case to work:

``````(def a [1 2])
(def b [3 4 5 6])

(my-zip a b)

``````
 #'user/a #'user/b (1 3 2 4)

Now for this case:

``````(def a [1 2 3 4])
(def b )

(my-zip a b)
``````
 #'user/a #'user/b (1 5)

## Problem 48 Sum

``````(some #{2 7 6} [5 6 7 8])
``````
`6`

## Problem 30 Compress a Sequence

Write a function which removes consecutive duplicates from a sequence.

``````; (= (apply str (__ "Leeeeeerrroyyy")) "Leroy")
(def test1 "Leeeeeerrrroyyyy")
``````
`#'user/test1`

Will reduce break the string apart?

``````(distinct test1)
(distinct [1 1 2 2])
``````
 (\L \e \r \o \y) (1 2) false

distinct takes out any duplicats, we just want the ones in a sequence.

I think we will have to use reduce here, we add each item to the accumulator, if the last item does not match the current

``````(defn my-reducer [acc cur] (if (= (last acc) cur) acc (conj acc cur)))
(fn [x] (let [x (reduce (fn [a c] (if (= (last a) c) a (conj a c))) [] x)]
(if (char? (first x)) (apply str x) x)))
(apply str (reduce my-reducer [] [1 1 2 3 3 2 2 3]))
(reduce my-reducer [] [[1 2] [1 2] [3 4] [1 2]])

(= (apply str ( "Leeeeeerrroyyy")) "Leroy")
``````
 #'user/my-reducer "leroy" "12323" [[1 2] [3 4] [1 2]]

## Problem 34 Implement Range

Write a function which creates a list of all integers in a given range. Dont use range!

## Problem 28 flatten

``````(def x '((1 2) 3 [4 [5 6]]))

(defn extract-values [l]
)

(reduce (fn [acc curr] ) () x)
``````
 #'user/x ((1 2) 3 [4 [5 6]])

## Problem 34 Implement Range

Write a function which creates a list of all integers in a given range.

Tests:

``````(= (__ 1 4) '(1 2 3))
(= (__ -2 2) '(-2 -1 0 1))
(= (__ 5 8) '(5 6 7))
``````

Given a start index and an end index, fill a sequence up with values

``````(defn my_range
([start end]
(my_range start end '()))
([start end coll]
(print start end)
(if (=  start end) coll
(my_range (inc start) end (concat coll [start])))))

(my_range 1 4)
(my_range -2 2)
(my_range 5 8)

; success!

(fn mr ([s e] (mr s e '()))
([s e c] (if (= s e) e (mr (inc s) e (concat c [s])))))

``````
 #'user/my_range (1 2 3) (-2 -1 0 1) (5 6 7) #function[user/eval7787/mr--7788] class clojure.lang.Compiler\$CompilerException

## Problem 91 Graph Connectivity

Given a graph, determine whether the graph is connected. A connected graph is such that a path exists between any two given nodes.

-Your function must return true if the graph is connected and false otherwise.

-You will be given a set of tuples representing the edges of a graph. Each member of a tuple being a vertex/node in the graph.

-Each edge is undirected (can be traversed either direction).

``````(def t1 #{[:a :a]}) ;; if theres only one node -> true
(def t2 #{[:a :b]}) ;; true
(def f1 #{[1 2] [2 3] [3 1]
[4 5] [5 6] [6 4]})
(def t3 #{[1 2] [2 3] [3 1]
[4 5] [5 6] [6 4] [3 4]})

(count #{[:a :a]})

;; (defn connected?
;;   [graph]
;;   (if (= (count graph) 1) true
;;       (reduce (fn [acc currentNode]
;;                 ) graph)))

;; (defn my_comp
;;   [c1 c2]
;;   (reduce #(%1 %2) c1))

(some (set [1 2]) [3 2])

(connected? t3)
(connected? t1)
(connected? t2)
(connected? f1)
(connected? t3)
``````
`Please reevaluate when nREPL is connected`

Get all the nodes in the graph.

Get all the paths of each node

## Problem 42

Write a function which calculates factorials.

``````(fn [factorial]
)
``````
`64`