Skip to content

Instantly share code, notes, and snippets.

@ShvedAction
Created February 27, 2015 12:55
Show Gist options
  • Save ShvedAction/45390250aab7c045694b to your computer and use it in GitHub Desktop.
Save ShvedAction/45390250aab7c045694b to your computer and use it in GitHub Desktop.
algorithmic quest
<script type="text/javascript">
function iteration(max, callback){
var ins = [];
var n = 0;
var r = 0;
do{
while(n <= max){
ins[r] = n;
callback(ins);
n++;
r++;
}
r = r-2;
ins.splice(0,r);
n = ins[r] + 1;
}while(r <= 0)
}
function simpleFound(sum, numbers) {
var result = [];
iteration(numbers.length - 1, function(indexs){
s = 0;
r = [];
for(var i in indexs){
s += numbers[i];
r.push(numbers[i]);
}
if (s == sum)
result.push(r);
});
return result;
}
function assert(a, b, message) {
if (a == b) {
console.log("ok");
return true;
}
console.log(message+"\n"+b);
return false;
}
function start(){
var nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var s = 6;
var except = [ [1,2,3], [4,2], [5, 1], [6]] ;
assert_equal(-1, simpleFound(s,nums), 'ERROR! for 6 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]') ;
}
</script>
<button onclick="start()">go</button>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment