Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created August 17, 2023 17:52
Show Gist options
  • Save codecademydev/c3816ff8dfe99408d510ea7835188435 to your computer and use it in GitHub Desktop.
Save codecademydev/c3816ff8dfe99408d510ea7835188435 to your computer and use it in GitHub Desktop.
Codecademy export
const testGraph = require('./testGraph.js');
const depthFirstTraversal = (start, visitedVertices = [start]) => {
console.log(start.data);
start.edges.forEach((edge) => {
const neighbor = edge.end;
if (!visitedVertices.includes(neighbor)) {
visitedVertices.push(neighbor);
depthFirstTraversal(neighbor, visitedVertices);
}
});
};
depthFirstTraversal(testGraph.vertices[0]);
class Graph {
constructor(isDirected = false, isWeighted = false) {
this.vertices = [];
this.isDirected = isDirected;
this.isWeighted = isWeighted;
}
addVertex(data) {
const newVertex = new Vertex(data);
this.vertices.push(newVertex);
return newVertex;
}
removeVertex(vertex) {
this.vertices = this.vertices.filter(v => v !== vertex);
}
addEdge(vertexOne, vertexTwo, weight) {
const edgeWeight = this.isWeighted ? weight : null;
if (vertexOne instanceof Vertex && vertexTwo instanceof Vertex) {
vertexOne.addEdge(vertexTwo, edgeWeight);
if (!this.isDirected) {
vertexTwo.addEdge(vertexOne, edgeWeight);
}
} else {
throw new Error('Expected Vertex arguments.');
}
}
removeEdge(vertexOne, vertexTwo) {
if (vertexOne instanceof Vertex && vertexTwo instanceof Vertex) {
vertexOne.removeEdge(vertexTwo);
if (!this.isDirected) {
vertexTwo.removeEdge(vertexOne);
}
} else {
throw new Error('Expected Vertex arguments.');
}
}
getVertexByValue(value) {
return this.vertices.find(vertex => vertex.data === value);
}
print() {
const vertexList = this.vertices;
vertexList.forEach(vertex => vertex.print());
}
}
class Vertex {
constructor(data) {
this.data = data;
this.edges = [];
}
addEdge(vertex, weight) {
if (vertex instanceof Vertex) {
this.edges.push(new Edge(this, vertex, weight));
} else {
throw new Error('Edge start and end must both be Vertex');
}
}
removeEdge(vertex) {
this.edges = this.edges.filter(edge => edge.end !== vertex);
}
print() {
const edgeList = this.edges.map(edge =>
edge.weight !== null ? `${edge.end.data} (${edge.weight})` : edge.end.data) || [];
const output = `${this.data} --> ${edgeList.join(', ')}`;
console.log(output);
}
}
class Edge {
constructor(start, end, weight = null) {
this.start = start;
this.end = end;
this.weight = weight;
}
}
module.exports = {
Graph,
Vertex,
Edge,
};
console.log = function() {};
const { expect } = require('chai');
const rewire = require('rewire');
const Structured = require('structured');
const fs = require('fs');
const { Graph } = require('../Graph.js');
const code = fs.readFileSync('depthFirstTraversal.js', 'utf-8');
describe('depthFirstTraversal', function() {
it('should expect a `callback` parameter.', function() {
const expectedStructure = function() {
const depthFirstTraversal = (start, callback, visitedVertices=[start]) => {}
}
const match = Structured.match(code, expectedStructure)
expect(match !== false, 'Check that the `callback` is set to the second parameter.').to.equal(true)
})
});
describe('depthFirstTraversal', function() {
it('should pass `callback` to the recursive call.', function() {
const expectedStructure = function() {
const depthFirstTraversal = (start, callback, visitedVertices=[start]) => {
depthFirstTraversal(neighbor, callback, visitedVertices)
}
}
const match = Structured.match(code, expectedStructure)
expect(match !== false, 'Check that the `callback` parameter is passed in the recursive call as the second argument.').to.equal(true)
})
});
describe('`depthFirstTraversal`', function() {
it('should run without errors.' , function() {
let dft;
try {
const moduleImport = rewire('../depthFirstTraversal.js');
dft = moduleImport.__get__('depthFirstTraversal');
} catch(e) {
expect(true, 'We encountered an error when running your code. Try checking the output for errors.').to.equal(false);
}
expect(typeof dft === 'function', `Expected \`depthFirstTraversal\` to be a function. Instead it is ${typeof dft}`).to.equal(true);
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment