Created
April 9, 2019 10:08
-
-
Save julianfrank/7d77bd3924b60812ac0c074a1895bb33 to your computer and use it in GitHub Desktop.
cg recursion try
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
/*process.stdin.resume(); | |
process.stdin.setEncoding('ascii'); | |
var input_stdin = ""; | |
var input_stdin_array = ""; | |
var input_currentline = 0; | |
process.stdin.on('data', function (data) {input_stdin += data;}); | |
process.stdin.on('end', function () { | |
input_stdin_array = input_stdin.split("\n"); | |
*/ | |
var T=5//parseInt(input_stdin_array.shift()) //Test Case Count | |
let input=[ | |
{N:5,v:[-1, 7, 8, -5, 4]}, | |
{N:4,v:[3, 2, 1, -1]}, | |
{N:4,v:[11, 12, -2, -1]}, | |
{N:4,v:[4, 5, 4, 3]}, | |
{N:4,v:[5, 10, 4, -1]} | |
] | |
/*for(var i=0;i<T;i++){ //Loop for each Test Case | |
input[i]={} | |
input[i].N=parseInt(input_stdin_array.shift()) //Neighbour Count | |
input[i].v=input_stdin_array.shift().trim().split(" ") | |
if (input[i].v.length > input[i].N)input[i].v.pop() | |
input[i].v=input[i].v.map(v=>parseInt(v)) | |
}*/ | |
//console.log("input",input) | |
//Actual Code Starts here | |
/****************************************************/ | |
//console.log(T,input) | |
//var failSafe=10 | |
for (let t=0;t<T;t++){ work(input[t].N,input[t].v) } | |
function work(N,tickets){ | |
console.log(`work(${N},${JSON.stringify(tickets)})`) | |
let done=[].fill(0) | |
function nextFreePath(pos){ | |
let nxt=tickets.findIndex((v,i)=>(i>pos && i<N && !(v in done))) | |
return nxt>0?nxt:Math.min(pos,N-1) | |
} | |
function path(strPos,curPath,curSum){ | |
console.log(`path(${strPos},${curPath},${curSum})`) | |
if (strPos <= N-1){ | |
if (strPos==N-1){ | |
return {path:`${curPath}${tickets[strPos]}`, sum:curSum+tickets[strPos]} | |
}else{ | |
return path(strPos+1,`${curPath}${tickets[strPos]}`, curSum+tickets[strPos]) | |
} | |
}else{ | |
return {path:curPath, sum:curSum} | |
} | |
} | |
console.log("Final:",path(0,[],0)) | |
} | |
/*************************************************** */ | |
function hasNoZero(testNum){ | |
//console.log("hasNoZero",testNum,testNum.toString(10).split(``).indexOf(`0`)) | |
return (testNum.toString(10).split(``).indexOf(`0`) == -1) | |
} | |
//Code Ends here | |
//}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment