Home > OS >  combinational sum- how to write test correctly JavaScript
combinational sum- how to write test correctly JavaScript

Time:07-02

I need to test Combinational Sum I algorithm using JavaScript. I have done all things in html, but I don't know how to call function (in script.js), which contain Combinational Sum I algorithm, correctly. Does anybody know how to call it? how to calculate? how to write test?

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;
  };
  

CodePudding user response:

You call it like in the snippet below, with some basic HTML. Or using the console of the browser. The function is not working right. You can read about implementation here 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">

CodePudding user response:

There is solved problem. Task: input array of integer, input target then calculate Combinational Sum and print the smallest array of Combinational Sum. For example: Input: [2,4,6] Target:4. Output will be 1, because Combinational Sum prints (2 2),(4). Smaller array is 4 and it contains one element so 1 will be output!

Code:

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>`

    }


  
}
  • Related