Skip to content

Commit

Permalink
added initial puzzles
Browse files Browse the repository at this point in the history
  • Loading branch information
robrighter committed May 27, 2011
1 parent 5ddcf2a commit bfd81e1
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
49 changes: 49 additions & 0 deletions puzzles/array_sort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
function makeArray(){
var length = 5000;
var toreturn = [];
for(var i=0; i<length; i++){
toreturn[i] = Math.floor(Math.random()*(length*2));
}
return toreturn;
}

function sortArray(tosort){
var copy = [];
for(var i=0; i<tosort.length; i++){
copy[i] = parseInt(tosort[i]);
}
return copy.sort(function(a,b){
return parseInt(a) > parseInt(b);
});
}

module.exports={
name:'Array Sort',

createQuestion:function(){
return {
array_to_sort: makeArray(),
solution_template: [0,1,2,3,4,5,6,7,8,9,10]
}
},

validateAnswer:function(question,answer){
if(question.hasOwnProperty('array_to_sort')){
try{
assert.deepEqual(question.array_to_sort, sortArray(answer));
return true;
}
catch(e){ return false }
}else{ return false }
}
}

//test
var question = module.exports.createQuestion();
console.log('Question is: ');
console.log(question);

var answer = sortArray(question.array_to_sort);
console.log(answer);
console.log(module.exports.validateAnswer(question, answer));

17 changes: 17 additions & 0 deletions puzzles/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var fs = require('fs');

var puzzles = fs.readdirSync('./').filter(function(i){ return i !== 'index.js' });


module.exports = puzzles.reduce(function(acc, i){
var puzzle = require('./' + i);
if(puzzle.hasOwnProperty('name')){
acc[puzzle.name] = puzzle;
}
return acc
},{})

console.log('-----------------------------------------');
console.log('Found the following Puzzles:')
console.log(module.exports);
console.log('-----------------------------------------');
27 changes: 27 additions & 0 deletions puzzles/pi.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bfd81e1

Please sign in to comment.