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
|
# Perl Weekly Challenge Club - 127
This is my first time submitting! I had a lot of fun and it was a good excuse
to work on my Perl. I spend most of my time working with Javascript, so I
started out by writing a solution Javascript.
## Task 1 > Disjoint Sets
You are given two sets with unique integers.
Write a script to figure out if they are disjoint.
The two sets are disjoint if they don't have any common members
### EXAMPLE
**Input:**
```javascript
const s1 = [1, 2, 5, 3, 4];
const s2 = [4, 6, 7, 8, 9];
```
**Output:** `0` as the given sets have common member `4`.
**Input:**
```javascript
const s1 = [1, 3, 5, 7, 9];
const s2 = [0, 2, 4, 6, 8];
```
**Output:** `1` as the given two sets do no have a common member.
### SOLUTION
```javascript
function isDisjoint(set1 = [], set2 = []) {
const testSet = [...set1]; // shallow copy of the first set
let disjoint = true; // trust, but verify
while (disjoint && testSet.length) {
const test = testSet.pop();
if (set2.includes(test)) disjoint = false;
}
return disjoint;
}
```
### ch-1.js
Running `./ch-1.js` tests our solution against the following test cases,
printing `Passed` or `Failed` to the console:
#### Case 1:
```javascript
1,2,5,3,4
4,6,7,8,9
0 // false, 4 is not unique
```
#### Case 2:
```javascript
1,3,5,7,9
0,2,4,6,8
1
```
#### Case 3:
```javascript
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
2
1 // Technically, the sets are still disjoint
```
#### Case 4:
```javascript
😺,😸,😹,😻,😼,😽
😽,🙀,😿,😾
0 // False
```
#### Case 5:
```javascript
🙂,🙃,😉,😌,😍,🥰,😘,😗,😙,😚,😋
🤤,😪,😵,🤐,🥴,🤢,🤮,🤧,😷,🤒,🤕
1 // True
```
### Custom Tests
`./ch-1.js` will optionally accept a path to a test file or directory of test
files (ie: `$ ./ch-1.js ./local_test.txt`). Test files must include no more than
2 lines of comma separated values with the last line being either `0` for tests
that should fail or `1` for tests that should pass. Lines beginning with `#`
will be ignored.
## Task 2 > Conflict Intervals
You are given a list of intervals.
Write a script to find out if the current interval conflicts with any of the
previous intervals.
### EXAMPLE
**Input:**
```javascript
const intervals = [[1, 4], [3, 5], [6, 8], [12, 13], [3, 20]];
```
**Output:**
```javascript
[[3, 5], [3, 20]]
```
- The 1st interval `[1, 4]` does not have any previous intervals to compare
with, so skip it.
- The 2nd interval `[3, 5]` does conflict with previous interval `[1, 4]`.
- The 3rd interval `[6, 8]` does not conflict with any of the previous
intervals `[1, 4]` and `[3, 5]`, so skip it.
- The 4th interval `[12, 13]` again does not conflict with any previous
intervals `[1, 4]`, `[3, 5]`, or `[6, 8]` so skip it.
- The 5th interval `[3, 20]` conflicts with the first interval `[1, 4]`.
**Input:**
```javascript
const intervals = [[3, 4], [5, 7], [6, 9], [10, 12], [13, 15]];
```
**Output:**
```javascript
[[6, 9]];
```
### SOLUTION
```javascript
function findConflictIntervals(intervals = [[]]) {
if (!Array.isArray(intervals)) return 'Input must be an 2-dimensional array';
const [conflicts, ] = intervals.reduce(([conflicts, passed], [ a1, a2 ]) => {
const start = a1 < a2 ? a1: a2;
const end = start === a1 ? a2: a1;
if (passed.length === 0) {
passed.push([start, end]);
return [conflicts, passed];
}
const conflict = passed.reduce((isConflict, [ b1, b2 ]) => {
if (
(start >= b1 && start <= b2) ||
(end >= b1 && end <= b2) ||
(start <= b1 && end >= b2)
) {
return true;
};
return isConflict;
}, false);
if (conflict) {
conflicts.push([start, end]);
} else {
passed.push([start, end]);
}
return [conflicts, passed]
}, [[], []]);
return conflicts;
}
```
### ch-2.js
Running `./ch-2.js` tests our solution against the folowing test cases,
printing `Passed` or `Failed` to the console:
#### Case 1:
```javascript
{
input: [[1, 4], [3, 5], [6, 8], [12, 13], [3, 20]],
output: [[3, 5], [3, 20]]
}
```
#### Case 2:
```javascript
{
input: [[3, 4], [5, 7], [6, 9], [10, 12], [13, 15]],
output: [[6, 9]]
}
```
#### Case 3:
```javascript
{
input: [[1.14, 1.56], [2.32, 3], [1.5, 1.72]],
output: [[1.5, 1.72]]
}
```
#### Case 4:
```javascript
{
input: [[-234, 10], [-1.12, 11], [11, 111111]],
output: [[-1.12, 11]]
}
```
#### Case 5:
```javascript
{
input: [[-1, -1], [1, 3], [3.1, 4], [4, 5]],
output: [[4, 5]]
}
```
### Custom Tests
`./ch-2.js` will optionally accept a path to a test file or directory of test
files (ie: `$ ./ch-2.js ./local_test.json`). Test files must be properly
formatted JSON with both an `"input"` and an `"output"` key.
#### Example Test
```json
{
"input": [
[
1,
3
],
[
2,
4
],
[
6,
7
]
],
"output": [
[
2,
4
]
]
}
```
|