-
Notifications
You must be signed in to change notification settings - Fork 577
Expand file tree
/
Copy pathEngMiguelCavaco_solutions.js
More file actions
281 lines (237 loc) · 5.96 KB
/
EngMiguelCavaco_solutions.js
File metadata and controls
281 lines (237 loc) · 5.96 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Function identity
// Write a function identity that takes an argument and returns that argument
const identity = (a) => a;
// Function addb
// Write a binary function addb that takes two numbers and returns their sum
const addb = (a, b) => a + b;
// Function subb
// Write a binary function subb that takes two numbers and returns their difference
const subb = (a, b) => a - b;
// Function mulb
// Write a binary function mulb that takes two numbers and returns their product
const mulb = (a, b) => a * b;
// Function minb
// Write a binary function minb that takes two numbers and returns the smaller one
const minb = (a, b) => Math.min(a, b);
// Function maxb
// Write a binary function maxb that takes two numbers and returns the larger one
const maxb = (a, b) => Math.max(a, b);
// Function add
// Write a function add that is generalized for any amount of arguments
const add = (...nums) => {
let sum = 0;
for (num of nums) sum += num;
return sum;
}
// Function sub
// Write a function sub that is generalized for any amount of arguments
const sub = (...nums) => {
let diff = 0;
nums.forEach((num, index) => {
(index === 0) ? diff = num : diff -= num;
})
return diff;
}
// Function mul
// Write a function mul that is generalized for any amount of arguments
const mul = (...nums) => {
let result = 0;
nums.forEach((num, index) => {
(index === 0) ? result = num : result *= num;
})
return result;
}
// Function min
// Write a function min that is generalized for any amount of arguments
const min = (...nums) => {
return Math.min(...nums);
}
// Function max
// Write a function max that is generalized for any amount of arguments
const max = (...nums) => {
return Math.max(...nums);
}
// Function addRecurse
// Write a function addRecurse that is the generalized add function but uses recursion
const addRecurse = (sum=0,...nums) => {
if (nums.length > 0) {
sum += nums.shift();
return addRecurse(sum, ...nums)
}
return sum;
}
// Function mulRecurse
// Write a function mulRecurse that is the generalized mul function but uses recursion
const mulRecurse = (result=1, ...nums) => {
if (nums.length > 0) {
result *= nums.shift();
return mulRecurse(result, ...nums);
}
return result;
}
// Function minRecurse
// Write a function minRecurse that is the generalized min function but uses recursion
const minRecurse = (min, ...nums) => {
if (nums.length > 0) {
let num = nums.shift();
if (num < min) min = num;
return minRecurse(min, ...nums);
}
return min;
}
// Function maxRecurse
// Write a function maxRecurse that is the generalized max function but uses recursion
const maxRecurse = (max, ...nums) => {
if (nums.length > 0) {
let num = nums.shift();
if (num > max) max = num;
return maxRecurse(max, ...nums);
}
return max;
}
// Function not
// Write a function not that takes a function and returns the negation of its result
const not = (func) => {
return !func();
}
// Function acc
// Write a function acc that takes a function and an initial value and returns a function
// that runs the initial function on each argument, accumulating the result
const acc = (func, ...initial) => {
let acc = 0;
for (let value of initial) {
acc += func(value);
}
return acc;
}
// Function accPartial
// Write a function accPartial that takes in a function, a start index, and an end index,
// and returns a function that accumulates a subset of its arguments by applying the given
// function to all elements between start and end.
const accPartial = (func, startIndex, endIndex, ...args) => {
let accPt = 0;
args = args.slice(startIndex, endIndex + 1);
for (let arg of args) accPt += arg;
return accPt;
}
// Function accRecurse
// Write a function accRecurse that does what acc does but uses recursion
const accRecurse = (func, acc, ...initial) => {
if (initial.length > 0) {
let value = initial.shift();
acc += func(value);
return accRecurse(func, acc, ...initial);
}
return acc;
}
// Function fill
// Write a function fill that takes a number and returns an array with that many numbers
// equal to the given number
const fill = (number) => {
let arr = [];
for(let i=0; i < number; i++) {
arr.push(number);
}
return arr;
}
// Function fillRecurse
// Write a function fillRecurse that does what fill does but uses recursion
const fillRecurse = (number, arr=[number]) => {
if (number > 1) {
arr.push(arr[0]);
return fillRecurse(number - 1, arr);
}
return arr;
}
// Function set
// Write a function set that is given a list of arguments and returns an array with all duplicates removed
const set = (...args) => {
let arr = [];
let arg;
while (args.length > 0) {
arg = args.shift();
if (!arr.includes(arg)) arr.push(arg);
}
return arr;
}
module.exports = {
identity,
addb,
subb,
mulb,
minb,
maxb,
add,
sub,
mul,
min,
max,
addRecurse,
mulRecurse,
minRecurse,
maxRecurse,
not,
acc,
accPartial,
accRecurse,
fill,
fillRecurse,
set,
identityf,
addf,
liftf,
pure,
curryb,
curry,
inc,
twiceUnary,
doubl,
square,
twice,
reverseb,
reverse,
composeuTwo,
composeu,
composeb,
composeTwo,
compose,
limitb,
limit,
genFrom,
genTo,
genFromTo,
elementGen,
element,
collect,
filter,
filterTail,
concatTwo,
concat,
concatTail,
gensymf,
gensymff,
fibonaccif,
counter,
revocableb,
revocable,
extract,
m,
addmTwo,
addm,
liftmbM,
liftmb,
liftm,
exp,
expn,
addg,
liftg,
arrayg,
continuizeu,
continuize,
vector,
exploitVector,
vectorSafe,
pubsub,
mapRecurse,
filterRecurse,
};