Skip to content

Commit 827ba28

Browse files
committed
Added hard/vertex_covering
1 parent 3bf0355 commit 827ba28

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

hard/vertex_covering.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* Using the JavaScript language, have the function vertexCovering(strArr) take
3+
* strArr which will be an array of length three. The first part of the array
4+
* will be a list of vertices in a graph in the form (A,B,C,...), the second
5+
* part of the array will be the edges connecting the vertices in the form
6+
* (A-B,C-D,...) where each edge is bidirectional. The last part of the array
7+
* will be a set of vertices in the form (X,Y,Z,...) and your program will have
8+
* to determine whether or not the set of vertices given covers every edge in
9+
* the graph such that every edge is incident to at least one vertex in the set.
10+
*
11+
* For example: if strArr is ["(A,B,C,D)","(A-B,A-D,B-D,A-C)","(A,B)"] then the
12+
* vertices (A,B) are in fact one of the endpoints of every edge in the graph,
13+
* so every edge has been accounted for. Therefore your program should return
14+
* the string yes. But, if for example the last part of the array was (C,B) then
15+
* these vertices don't cover all the edges because the edge connecting A-D is
16+
* left out. If this is the case your program should return the edges given in
17+
* the second part of the array that are left out in the same order they were
18+
* listed, so for this example your program should return (A-D).
19+
*
20+
* The graph will have at least 2 vertices and all the vertices in the graph
21+
* will be connected. The third part of the array listing the vertices may be
22+
* empty, where it would only contain the parenthesis with no values within it:
23+
* "()"
24+
*
25+
* https://www.coderbyte.com/results/bhanson:Vertex%20Covering:JavaScript
26+
*
27+
* @param {array} strArr
28+
* @return {string}
29+
*/
30+
function vertexCovering(strArr) {
31+
let [, edges, vertices] = strArr;
32+
33+
edges = edges.substr(1, edges.length - 2).split(',');
34+
vertices = vertices.substr(1, vertices.length - 2).split(',');
35+
36+
const missingEdges = edges.filter(edge => {
37+
return !vertices.some(vertice => {
38+
if (vertice === '') {
39+
return false;
40+
}
41+
return edge.includes(vertice);
42+
});
43+
});
44+
45+
if (missingEdges.length === 0) {
46+
return 'yes';
47+
}
48+
49+
return `(${missingEdges.join(',')})`;
50+
}
51+
52+
module.exports = vertexCovering;

hard/vertex_covering.test.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
const vertexCovering = require('./vertex_covering');
2+
3+
describe('vertexCovering()', () => {
4+
test('passes Coderbyte.com tests', () => {
5+
expect(
6+
vertexCovering(['(A,B,C,D)', '(A-B,A-D,B-D,A-C)', '(A,B)'])
7+
).toBe('yes');
8+
9+
expect(
10+
vertexCovering(['(A,B,C,D)', '(A-B,A-D,B-D,A-C)', '(C,B)'])
11+
).toBe('(A-D)');
12+
13+
expect(vertexCovering(['(A,B,C,D)', '(A-B,A-D,B-D,A-C)', '(C)'])).toBe(
14+
'(A-B,A-D,B-D)'
15+
);
16+
17+
expect(vertexCovering(['(X,Y,Z,Q)', '(X-Y,Y-Q,Y-Z)', '(Z,Y,Q)'])).toBe(
18+
'yes'
19+
);
20+
21+
expect(vertexCovering(['(X,Y,Z,Q)', '(X-Y,Y-Q,Y-Z,X-Q)', '(X)'])).toBe(
22+
'(Y-Q,Y-Z)'
23+
);
24+
25+
expect(vertexCovering(['(X,Y,Z,Q)', '(X-Y,Y-Q,Y-Z,X-Q)', '(Q)'])).toBe(
26+
'(X-Y,Y-Z)'
27+
);
28+
29+
expect(vertexCovering(['(X,Y,Z,Q)', '(X-Y,Y-Q,Y-Z,X-Q)', '()'])).toBe(
30+
'(X-Y,Y-Q,Y-Z,X-Q)'
31+
);
32+
33+
expect(
34+
vertexCovering([
35+
'(A,B,C,D,E,F)',
36+
'(A-B,A-D,B-D,B-C,C-F,E-D)',
37+
'(B,F)'
38+
])
39+
).toBe('(A-D,E-D)');
40+
41+
expect(
42+
vertexCovering([
43+
'(A,B,C,D,E,F)',
44+
'(A-B,A-D,B-D,B-C,C-F,E-D)',
45+
'(D,B,F)'
46+
])
47+
).toBe('yes');
48+
49+
expect(
50+
vertexCovering([
51+
'(A,B,C,D,E,F,G)',
52+
'(A-B,A-D,B-D,B-C,C-F,E-D,F-E,G-F)',
53+
'(B,F)'
54+
])
55+
).toBe('(A-D,E-D)');
56+
});
57+
});

0 commit comments

Comments
 (0)