-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
269 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
var path = require( 'path' ), | ||
verify = require( 'adventure-verify' ), | ||
tools = require( '../../library/tools' ); | ||
|
||
exports.problem = function () { | ||
return tools.mdProblem( { | ||
mdSource: path.join( __dirname, 'problem-reading-problems.md' ), | ||
pdfName: 'problem-reading-problems.pdf', | ||
sampleSource: path.join( __dirname, 'samples' ), | ||
sampleDest: 'samples-reading-problems' | ||
} ); | ||
}; | ||
|
||
exports.solution = tools.mdSolution( path.join( __dirname, 'solution-reading-problems.js' ) ); | ||
|
||
exports.verify = verify( { modeReset: true }, function checker( args, t ) { | ||
var reader = require( path.resolve( args[ 0 ] ) ), | ||
verifier = require( './solution-reading-problems' ), | ||
sorter = function ( a, b ) { | ||
return a.id.localeCompare( b.id ); | ||
}, | ||
inFile = path.join( __dirname, 'samples', 'sample1.in' ); | ||
|
||
console.log( 'Testing against ' + inFile ); | ||
|
||
reader( inFile, function ( testGraph ) { | ||
verifier( inFile, function ( verifierGraph ) { | ||
var verticesFound = { c: 0, total: 0 }, | ||
correctNbs = { c: 0, total: 0 }; | ||
verifierGraph._vertices.forEach( function ( v ) { | ||
verticesFound.total++; | ||
if ( testGraph.v( v.id ) ) { | ||
verticesFound.c++; | ||
var verifiedNeighbours = v.neighbours().sort( sorter ), | ||
testNeighbours = testGraph.v( v.id ).neighbours().sort( sorter ); | ||
|
||
correctNbs.total++; | ||
if ( testNeighbours.length === verifiedNeighbours.length ) { | ||
if ( verifiedNeighbours.every( function ( el, ix ) { | ||
return el.id == testNeighbours[ ix ].id; | ||
} ) ) { | ||
correctNbs.c++; | ||
} | ||
} | ||
} | ||
} ); | ||
|
||
t.equal( verticesFound.c, verticesFound.total, 'Correct vertices' ); | ||
t.equal( correctNbs.c, correctNbs.total, 'Vertices with correct neighbours' ); | ||
|
||
t.end(); | ||
|
||
} ); | ||
} ); | ||
|
||
|
||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
A | ||
a | ||
1 | ||
is-it-42 | ||
|
||
A a | ||
A 1 | ||
A is-it-42 | ||
1 a |
127 changes: 127 additions & 0 deletions
127
exercises/ex3-reading-problems/solution-reading-problems.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
var fs = require( 'fs' ), | ||
readline = require( 'readline' ); | ||
|
||
/** | ||
* Vertex in a graph | ||
* @param {*} id This vertex's ID | ||
* @returns {Vertex} | ||
*/ | ||
var Vertex = function ( id ) { | ||
|
||
this.id = id; | ||
this._edges = []; | ||
|
||
return this; | ||
}; | ||
Vertex.prototype = { | ||
neighbours: function () { | ||
return this._edges.map( function ( edge ) { | ||
return edge.vTo; | ||
} ); | ||
}, | ||
addEdge: function ( edge ) { | ||
this._edges.push( edge ); | ||
} | ||
}; | ||
|
||
/** | ||
* Directed edge in a graph | ||
* @param {Vertex} vFrom Source vertex | ||
* @param {Vertex} vTo Target vertex | ||
* @returns {Edge} | ||
*/ | ||
var Edge = function ( vFrom, vTo ) { | ||
|
||
/** @type {Vertex} */ | ||
this.vFrom = vFrom; | ||
/** @type {Vertex} */ | ||
this.vTo = vTo; | ||
|
||
this.vFrom.addEdge( this ); | ||
|
||
return this; | ||
}; | ||
|
||
/** | ||
* Graph containing edges and vertices | ||
* @param vertices | ||
* @param edges | ||
* @constructor | ||
*/ | ||
var Graph = function ( vertices, edges ) { | ||
|
||
/** | ||
* We use a ES6 Map for storing vertices. Maps have the advantage over objects that | ||
* they can be iterated over with forEach. | ||
* @type {Map.<Vertex>} | ||
*/ | ||
this._vertices = new Map(); | ||
|
||
/** | ||
* If you do not know why we don't just use `this` in the forEach loop, read this article: | ||
* http://javascriptplayground.com/blog/2012/04/javascript-variable-scope-this/ | ||
*/ | ||
var me = this; | ||
|
||
// Read and store all vertices | ||
vertices.forEach( function ( id ) { | ||
me._vertices.set( id, new Vertex( id ) ); | ||
} ); | ||
|
||
// Create the edges | ||
edges.forEach( function ( pair ) { | ||
var idFrom = pair[ 0 ], | ||
idTo = pair[ 1 ]; | ||
new Edge( me.v( idFrom ), me.v( idTo ) ); | ||
} ); | ||
|
||
}; | ||
Graph.prototype = { | ||
/** | ||
* Retrieve a vertex identified by the given ID. | ||
* @param id Vertex ID | ||
* @returns {Vertex} | ||
*/ | ||
v: function ( id ) { | ||
return this._vertices.get( id ); | ||
} | ||
}; | ||
|
||
/** | ||
* Read a graph from a file. | ||
* @param {String} inFilePath Absolute path to the input file | ||
* @param {function(graph: Graph)} callback Callback function, called when Graph is read. | ||
*/ | ||
function readGraph( inFilePath, callback ) { | ||
|
||
var vertices = [], | ||
edges = [], | ||
verticesRead = false; | ||
|
||
readline.createInterface( { | ||
input: fs.createReadStream( inFilePath ) | ||
} ).on( 'line', function ( line ) { | ||
if ( verticesRead ) { | ||
var edge = line.split( ' ' ); | ||
if ( edge.length == 2 ) { | ||
console.log( 'Edge: ', edge ); | ||
edges.push( edge ); | ||
} else { | ||
console.log( 'No edge: ', line ); | ||
} | ||
} else { | ||
if ( line.length == 0 ) { | ||
console.log( 'All vertices read.' ); | ||
verticesRead = true; | ||
} else { | ||
console.log( 'Vertex: ', line ); | ||
vertices.push( line ); | ||
} | ||
} | ||
} ).on( 'close', function () { | ||
console.log( 'Read.' ); | ||
callback( new Graph( vertices, edges ) ); | ||
} ); | ||
} | ||
|
||
module.exports = readGraph; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters