View Single Post
  #2  
Old 11-19-2007, 09:16 AM
napo_p2p napo_p2p is offline
oh snaps
napo_p2p's Avatar
Join Date: Sep 2003
Location: Pismo Beach, California
Posts: 2,118
napo_p2p has a spectacular aura aboutnapo_p2p has a spectacular aura about
Send a message via AIM to napo_p2p Send a message via MSN to napo_p2p
I have something I put together real quick, but it searches linearly, so someone could most certainly come up with something more efficient.

Also, I assumed that if both arrays are (for example):
{1, 2, 3, 4, 5, 6}

Then they would just have the two sets {1, 2, 3} and {4, 5, 6}.
Instead of {1, 2, 3}, {2, 3, 4}, {3, 4, 5}, {4, 5, 6}.

Also, there is a high chance that there is a case where the algorithm fails, as I haven't really tested it... so feel free to find a case that doesn't work :P.

PHP Code:
//Returns array containing sets of 3 numbers both arrays have ing common
function ex1(arr1arr2) {
  
temp.result = {};
  
temp.pos 0;
  while (
temp.pos temp.arr1.size() - 2) {  //Make sure there's 3 elements left
    
temp.check = {temp.arr1[temp.pos], temp.arr1[temp.pos 1], temp.arr1[temp.pos 2]};
    if (
temp.result.pos(temp.check) < && searchArr(temp.arr2temp.check)) {
      
temp.result.add(temp.check); 
      
temp.pos += 3;  //Set is found, jump over whole set
    
}
    else {
      
temp.pos++;  //Set not found, go to next number
    
}
  }
  return 
temp.result;
}

//True if subarr is in arr.
function searchArr(arrsubarr) {
  
temp.found 0;
  for (
temp.etemp.arr) {
    if (
temp.== temp.subarr[temp.found]) {
      
temp.found++;
      if (
temp.found == temp.subarr.size()) {
        return 
true;
      }
    }
    else {
      
temp.found 0;
    }
  }
  return 
false;

__________________
Scito hoc super omnia.
Haec vita est tua una sola.
Dum vita superest, utere maxime quoque puncto, momento, et hora quae habes.
Tempus neminem non manet.
Noli manere tempus.
Carpe Diem

Seize the Day.
Reply With Quote