View Single Post
  #3  
Old 03-26-2008, 03:25 AM
DrakilorP2P DrakilorP2P is offline
Registered User
DrakilorP2P's Avatar
Join Date: Apr 2006
Posts: 755
DrakilorP2P is just really niceDrakilorP2P is just really nice
Assuming that the algorithm should find the number of occurrences of a string in another string. Which it shouldn't.

PHP Code:
function findFrequencyOfPattern(haystack, needle)
{
  
temp.occurrences = 0;
  for (
temp.i=0; temp.i<haystack.length() - needle.length() + 1; temp.i++) {
    if (
haystack.substring(temp.i, needle.length()) == needle) temp.occurrences++;
  }
  return 
temp.occurrences;
} 
PHP Code:
temp.haystack = "AUGCCCGTAUACGTA";
temp.needles = {"CGTA", "CGT", "GTA", "CG", "GT", "TA", "AU", "CC"};
sendrpgmessage(temp.haystack);
for (
temp.needle: temp.needles) {
  
sendrpgmessage(temp.needle @ ": " @ findFrequencyOfPattern(temp.haystack, temp.needle));
} 
Outputs:
PHP Code:
AUGCCCGTAUACGTA
CGTA
: 2
CGT
: 2
GTA
: 2
CG
: 2
GT
: 2
TA
: 2
AU
: 2
CC
: 2 

Last edited by DrakilorP2P; 03-26-2008 at 09:57 AM..
Reply With Quote