forked from NicMcPhee/CSci-4409-4Clojure-exercise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercises-4clojure.clj
186 lines (117 loc) · 3.93 KB
/
exercises-4clojure.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
;;; Put your solutions to the assigned 4Clojure exercises here and commit
;;; back up to your fork on Github.
;;; What you enter is whatever you'd put in the "Code which fills in the blank"
;;; box in 4Clojure. You're welcome to include other things here (helper definitions,
;;; scratch computations you found helpful, etc.), SO MAKE SURE YOU LABEL YOUR
;;; "ANSWERS" clearly.
;;; I've included a few examples below just to give you a sense of what I'm looking
;;; for.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Problem #162: Logical falsity and true
; What I pasted in the box:
1
; because all the given expressions returned 1, for example:
(= 1 (if [] 1 0))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Problem #15: Double Down
; What I pasted in the box:
#(* 2 %)
; which is an anonymous function that doubles its argument, for example:
(= (#(* 2 %) 11) 22)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; TO DO
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Problem 10 - Intro To Maps
; What I put:
20
; considered it like stored values
; value at :b is 20 for both
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Problem 16 - Hello World
; What I put:
(fn [name] (format "Hello, %s!" name))
; %s designates where to put name in the string
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Problem 17 - Sequences: map
; What I put:
'(6 7 8)
; nameless function takes each number from the list and adds 5 to it
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Problem 18 - Sequences: filter
; What I put:
'(6 7)
; filter checks numbers from list
; if number is > 5 then its in the new list (stays in the list)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Problem 20 - Penultimate Element
; What I put:
#(.get %(- (count %) 2))
; count = length of list
; - 2 to get second to last item
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Problem 21 - Nth Element
; What I put:
#(last (take (+ %2 1) %1))
; take gets all the elements up to what given (n) + 1
; last gets the last element of the new set of elements
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Problem 23 - Reverse a sequence
; What I put:
#(into () %)
; into puts elements at the head of the set individually (reversing it)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Problem 25 - Find the odd numbers
; What I put:
#(filter odd? %)
; Filter goes through whole lists
; if number is odd, it's included
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Problem 37 - Regular Expressions
; What I put:
"ABC"
; Reading the sequence looking for only capital letters
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Problem 40 - Interpose a sequence
; What I put:
#(drop-last (interleave %2 (repeat %1)))
; repeat gives me the inputs, interleave puts things in
; just have to drop the last input and whala!
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Problem 43 - Reverse interleave
;
#((let [x (count %1)
y (/ x %1) ]
(partition y %2) ))
; Get ClassCastException, but I know I'm really close.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Problem 44 - Rotate sequence
; What I put:
#(let
[x (count %2)
a (if (neg? %1)
(take (inc (+ x %1)) %2)
(take %1 %2)
)
b (if (neg? %1)
(reverse (take (* -1 %1) (reverse %2)))
(reverse (take (- x %1) (reverse %2)))
)
]
(concat b a)
)
; Only passes first one.
; Hypothesis:
; My positive rotations work while my negatives don't.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Problem 46 - Flipping out
; What I put:
#(fn ([x y] (% y x)))
; Literally filipping x & y (the two argument) when calling function (%)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Problem 47 - Contain yourself
; What I put:
4
; for #{4 5 6}, there is a 4
; [1 1 1 1 1], there is atleast 4 indices
; [2 3 4], only has 3 indices so false and (not [2 3 4]) is true
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;