-
Notifications
You must be signed in to change notification settings - Fork 1
/
solutions.clj
214 lines (127 loc) · 2.25 KB
/
solutions.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
;; Solutions are in order from least to most recent
;; 1
true
;; 2
4
;; 3
"HELLO WORLD"
;; 4
:a :b :c
;; 5
'(1 2 3 4)
;; 6
:a :b :c
;; 7
[1 2 3 4]
;; 8
#{:a :b :c :d}
;; 9
2
;; 10
20
;; 11
{:b 2}
;; 12
3
;; 13
[20 30 40]
;; 14
8
;; 15
(fn [x] (* 2 x))
;; 16
(fn [name] (str "Hello, " name "!"))
;; 17
'(6 7 8)
;; 18
'(6 7)
;; 19
#(nth % (dec (count %)))
;; 20 (yes, sorta cheating on this one)
#(nth % (dec (dec (count %))))
;; 21
#(last (take (inc %2) %1))
;; 22
#(apply + (map (fn [x] (int 1)) %1))
;; 23
(fn [coll]
(if (set? coll)
(map-indexed
(fn [index, item]
(nth (seq coll) (- (dec (count coll)) index))) coll)
(map-indexed
(fn [index, item]
(nth coll (- (dec (count coll)) index))) coll)
))
;; 24
#(apply + %)
;; 25
filter odd?
;; 26
(fn [n]
(letfn [(fib [i]
(if (>= 2 i)
1
(+ (fib (dec i)) (fib (- i 2)))))]
(map fib (take n (iterate #(inc %) 1)))))
;; 27
#(= (reverse %) (seq %))
;; 29
(fn [x] (apply str (filter #(Character/isUpperCase %) x)))
;; 34
#(take (- %2 %) (iterate inc %))
;; 35
7
;; 36
[x 7
y 3
z 1]
;; 37
"ABC"
;; 42
#(apply * (range 1 (inc %)))
;; 45
'(1 4 7 10 13)
;; 47
4
;; 48
6
;; 57
'(5 4 3 2 1)
;; 64
+
;; 68
[7 6 5 4 3]
;; 97
(fn pascals-triangle [n]
(let [initial-rows {1 [1] 2 [1 1]}
guts (fn [coll]
(let [limit (dec (count coll))
add (fn [c n] (+ (nth c n) (nth c (inc n))))]
(loop [result []
idx 0]
(if (= idx limit)
result
(recur (conj result (add coll idx)) (inc idx))))))
row (fn [coll] (vec (flatten [1 (guts coll) 1])))]
(if (>= 2 n)
(initial-rows n)
(loop [result [1 1]
idx 2]
(if (= idx n)
result
(recur (row result) (inc idx)))))))
;; My favorite solution to this problem is
;; from mysticcabbage. It is clean and
;; very elegantly done. It works by putting
;; zeros on either side of the triangle
;; and then recursing through. Beautiful.
(fn [row]
(nth
(iterate
#(map +
(concat '(0) %)
(concat % '(0)))
'(1)) (dec row)))
;; 107
#(fn [x] (Math/pow x %))