Skip to content

silviapan/4clojure-solutions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 

Repository files navigation

4clojure-solutions

Solutions for problems on 4clojure (http://www.4clojure.com/)

1. Nothing but the Truth

true

2. Simple Math

4

3. Intro to Strings

"HELLO WORLD"

4. Intro to Lists

:a :b :c

5. Lists: conj

'(1 2 3 4)

6. Intro to Vectors

:a :b :c

7. Vectors: conj

[1 2 3 4]

8. Intro to Sets

#{:a :b :c :d}

9. Sets: conj

2

10. Intro to Maps

20

11. Maps: conj

{:b 2}

12. Intro to Sequences

3

13. Sequences: rest

[20 30 40]

14. Intro to Functions

8

15. Double Down

* 2

16. Hello World

(fn [name]  
  (str "Hello, " name "!"))

17. Sequences: map

'(6 7 8)

18. Sequences: filter

'(6 7)

19. Last Element

#(nth % (dec (count %)))

20. Penultimate Element

#(nth % (- (count %) 2))

21. Nth Element

#(get (into [] %1) %2)

22. Count a Sequence

#(reduce + (vals (frequencies %)))

23. Reverse a Sequence

#(reduce conj () %)

24. Sum It All Up

reduce +

25. Find the odd numbers

filter odd?

27. Palindrome Detector

#(= (seq %) (reverse %))

29. Get the Caps

#(apply str (re-seq #"[A-Z]" %))

30. Compress a Sequence

#(map first (partition-by identity %))

31. Pack a Sequence

partition-by identity

32. Duplicate a Sequence

#(interleave % %)

33. Replicate a Sequence

#(mapcat (fn [x] (repeat %2 x)) %1)

34. Implement range

#(take (- %2 %1) (iterate inc %1))

35. Local Bindings

7

36. Let it Be

[x 7 y 3 z 1]

37. Regular Expressions

"ABC"

38. Maximum value

#(last (sort %&))

39. Interleave Two Seqs

#(flatten (map list %1 %2))

42. Factorial Fun

#(reduce * (range 1 (inc %)))

45. Intro to Iterate

'(1 4 7 10 13)

47. Contain Yourself

4

48. Intro to some

6

49. Split a sequence

#(vector (subvec %2 0 %1) (subvec %2 %1))

51. Advanced Destructuring

[1 2 3 4 5]

52. Intro to Destructuring

[c e]

57. Simple Recursion

'(5 4 3 2 1)

61. Map Construction

#(apply hash-map (interleave %1 %2))

64. Intro to Reduce

+

68. Recurring Theme

[7 6 5 4 3]

71. Rearranging Code: ->

last

72. Rearranging Code: ->>

reduce +

83. A Half-Truth

(fn [& bools]
  (let [true-count (get (frequencies bools) true 0)
        total-count (count bools)]
    (and (> true-count 0) (< true-count total-count))))

99. Product Digits

(fn [x y]
  (->> (* x y) 
       str
       (map (comp read-string str))))

134. A nil key

(fn [k m]
  (if (contains? m k)
    (= (m k) nil)
    false))

143. dot product

(fn [x y]
  (->> (map * x y)
       (reduce +)))

145. For the win

'(1 5 9 13 17 21 25 29 33 37)

156. Map Defaults

#(zipmap %2 (repeat %1))

161. Subset and Superset

#{1 2}

162. Logical falsity and truth

1

About

Solutions for problems on 4clojure (http://www.4clojure.com/)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published