These are chat archives for Codewars/codewars.com/kata-solving-help

12th
Oct 2016
Derek Alia
@derekalia
Oct 12 2016 03:54
I'm having trouble with the sum_pairs problem. Apparently its too slow. Can someone take a look?
This message was deleted
This message was deleted
Derek Alia
@derekalia
Oct 12 2016 04:29
var sum_pairs = function(ints,s){
 var goal = 0;
 var pair = [];

 while(ints.length >= 2){   
   goal = s-ints[0];   
   if(ints.indexOf(goal,1) != -1){
     pair.push(ints[0],goal);
     ints = ints.slice(1, ints.indexOf(goal,1))
   }
   ints.shift();
 }
   if(pair.length == 0){    
    return undefined; 
   }    
   return [pair[pair.length-2],pair[pair.length-1]];

}
Giacomo Sorbi
@GiacomoSorbi
Oct 12 2016 07:03
definitely too slow, try to see if you can avoid doing things twice
Derek Alia
@derekalia
Oct 12 2016 12:30
Okay
Derek Alia
@derekalia
Oct 12 2016 12:38
I rewrote it so it no longer calls indexOf twice and it doesnt use slice (ive been told that making a copy of the array is whats taking up so many cycles)
var sum_pairs = function(ints, s) {
  var goal, indexed, i = 0;
  var pair = [];
  var arm = ints.length - 1;

  while (i < arm) {
    goal = s - ints[i];
    indexed = ints.indexOf(goal, i + 1);
    if (indexed != -1 && indexed <= arm) {
      pair.push(ints[i], goal);   
      arm = indexed;
    }
    i++
  }

  if (pair.length == 0) {
    return undefined;
  }
  return [pair[pair.length - 2], pair[pair.length - 1]];

}
Giacomo Sorbi
@GiacomoSorbi
Oct 12 2016 12:38
does it work now? I fear not
Derek Alia
@derekalia
Oct 12 2016 12:38
Still too slow
I'm not sure what the issue is in my main loop.
Giacomo Sorbi
@GiacomoSorbi
Oct 12 2016 12:46
change approach
Derek Alia
@derekalia
Oct 12 2016 12:52
I feel like i'm missing some subject area to get this. Any resources you recommend reading up on?
Derek Alia
@derekalia
Oct 12 2016 13:55
got it, thanks.