0

I am having problems to convert this pseudocode to R language. The output of mine is not the correct answer. Could anyone help me, please?

PatternCount(Text, Pattern)
    count ← 0
    for i ← 0 to |Text| − |Pattern|
        if Text(i, |Pattern|) = Pattern
            count ← count + 1
    return count

I paste here what I have until now:

PatternCount <- function(text, pattern){
   times <- 0
   for (i in c(0:nchar(text) - nchar(pattern))){
      if (substr(text, i, i + nchar(pattern)) == pattern)
      times <- times + 1}
   return(times)}

Thank you in advance!

Sonia C.
  • 1
  • 1
  • 1
    You should also provide sample input and the desired output for that input to make this problem [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Also it would help to explain exactly what `|x|` and `x(a,b)` means in your pseudocode. – MrFlick Oct 27 '17 at 15:39
  • `length(gregexpr("an", "banana")[[1]])` counts occurrences of `an` in `banana`. – G. Grothendieck Oct 27 '17 at 17:23

1 Answers1

0

Your indexing range is wrong, as R indexes by natural counting, aka starts counting at 1, also you have to shorten the substr endmark by 1:

PatternCount <- function(text, pattern){
  times <- 0
  for (i in 1:(nchar(text) - nchar(pattern))) {
    if (substr(text, i, i + nchar(pattern)-1) == pattern)
    times <- times + 1}
  return(times)
}

test:

PatternCount("abaabbbbabbaab", 'aa') #-> 2

Also this is just a progressive pattern match, so catenated sequences might not be resolved as you think they would

PatternCount("abaaaab", 'aa') #-> 3
mzoll
  • 475
  • 3
  • 11