Necesito probar el algoritmo Combinational Sum I usando JavaScript. Hice todas las cosas en html, pero no sé cómo llamar a la función (en script.js), que contiene el algoritmo Combinational Sum I, correctamente. ¿Alguien sabe cómo llamarlo? ¿como calcular? ¿Cómo escribir prueba?
let botun=document.getElementById('botun'); //including variables botun.onclick=function(){ let niz=document.getElementById('input').value; let target=document.getElementById('target').value; //convert string in array let nizInt=niz.split(' ').map(Number); //convert element of array in Int let nizIntNovi=[]; for(var i=0; i<nizInt.length; i++) { nizInt[i] = parseInt(nizInt[i], 10); nizIntNovi[i]=nizInt[i]; } console.log(nizIntNovi); //calling function let meduRez=combinationalSum(nizIntNovi,target); console.log(meduRez); } // Javascript program to find all combinations that // sum to a given value var combinationalSum=function(candidates,target){ //global result const result=[]; candidates.sort((a,b)=>ab); //dfs recursive helper const dfs=(i,candidates,target,slate)=>{ //backtracking case if(target<0) return; //base case if(target===0){ result.push(slate.slice()); return; } //dfs recursive case for(let j=i;j<candidates.lenght;j++){ slate.push(candidates[j]); dfs(j,candidates,target-candidates[j],slate); slate.pop(); } } dfs(0,candidates,target,[]); return result; };Lo llamas como en el fragmento a continuación, con algo de HTML básico. O usando la consola del navegador. La función no funciona bien. Puede leer sobre la implementación aquí https://www.geeksforgeeks.org/combinational-sum/
let botun = document.getElementById('botun'); //including variables botun.onclick = function() { let niz = document.getElementById('input').value; let target = document.getElementById('target').value; //convert string in array let nizInt = niz.split(' ').map(Number); //convert element of array in Int let nizIntNovi = []; for (var i = 0; i < nizInt.length; i++) { nizInt[i] = parseInt(nizInt[i], 10); nizIntNovi[i] = nizInt[i]; } console.log(nizIntNovi); //calling function let meduRez = combinationalSum(nizIntNovi, target); console.log(meduRez); } // Javascript program to find all combinations that // sum to a given value var combinationalSum = function(candidates, target) { //global result const result = []; candidates.sort((a, b) => a - b); //dfs recursive helper const dfs = (i, candidates, target, slate) => { //backtracking case if (target < 0) return; //base case if (target === 0) { result.push(slate.slice()); return; } //dfs recursive case for (let j = i; j < candidates.lenght; j++) { slate.push(candidates[j]); dfs(j, candidates, target - candidates[j], slate); slate.pop(); } } dfs(0, candidates, target, []); return result; }; input { height: 30px; width: 100px; line-height: 1; } .as-console-wrapper { max-height: 100% !important; } Input: <input id="input" value="1 2 3 4"> Target: <input id="target" value="6"> <input type="button" id="botun" value="click">Hay problema resuelto. Tarea: ingrese la matriz de enteros, ingrese el objetivo, luego calcule la Suma Combinacional e imprima la matriz más pequeña de la Suma Combinacional. Por ejemplo: Entrada: [2,4,6] Destino:4. La salida será 1, porque Combinational Sum imprime (2+2),(4). La matriz más pequeña es 4 y contiene un elemento, ¡así que se generará 1!
Código:
let botun=document.getElementById('botun'); let nizSplitNew=[]; let targetNew; let brojac; let array=[]; let min; var rezultat=document.getElementById("rezl"); //including variables botun.onclick=function(){ let niz=document.getElementById('input').value; let target=document.getElementById('target').value; //convert string in array let nizSplit=niz.split(',').map(Number); //convert element of array in Int for(var i=0; i<nizSplit.length; i++) { nizSplitNew[i] = parseInt(nizSplit[i], 10); } console.log(nizSplitNew); targetNew = parseInt(target, 10); //calling function let meduRez=combinationSum(nizSplitNew,targetNew); // Javascript program to find all combinations that // sum to a given value function combinationSum(arr, sum) { let ans = new Array(); let temp = new Array(); // first do hashing since hashset does not always // sort // removing the duplicates using HashSet and // Sorting the arrayList let set = new Set([...arr]); arr = [...set]; arr.sort() findNumbers(ans, arr, sum, 0, temp); return ans; } function findNumbers(ans, arr, sum, index, temp) { if (sum == 0) { // pushing deep copy of list to ans ans.push([...temp]); return; } for (let i = index; i < arr.length; i++) { // checking that sum does not become negative if ((sum - arr[i]) >= 0) { // pushing element which can contribute to // sum temp.push(arr[i]); findNumbers(ans, arr, sum - arr[i], i, temp); // removing element from list (backtracking) temp.splice(temp.indexOf(arr[i]), 1); } } } // Driver Code // arr.push(5); // arr.push(4); // arr.push(8); //let arr = [] for(let i=0;i<nizSplitNew;i++){ nizSplitNew.push(nizSplitNew[i]); } let sum = targetNew; let ans = combinationSum(nizSplitNew, sum); // If result is empty, then // print all combinations stored in ans for (let i = 0; i < ans.length; i++) { brojac=0; for (let j = 0; j < ans[i].length; j++) { brojac=brojac+1; } array.push(brojac); } console.log(array); min = Math.min(...array); if (array.length == 0) { rezultat.innerHTML=`<p>-1</p>` } else{ rezultat.innerHTML=`<p>${min}</p>` } }