1

I want to count the consecutive occurrences of a substring in a mainstring. After that, I want to take the number of consecutive occurrences, and have that as a return value. I wanted to do that recursively, as follows:

def countConsecutive (mainstr, substr, count):
    if substr in mainstr[:len(substr)]:
        count += 1
        mainstr = mainstr[len(substr):]
        countConsecutive (mainstr, substr, count)
    else:
        return count

I have 'count' as input, as it needs to be set to 0 every time the function is called outside the function itself. However, the problem is, the return value is a NoneType, so I can't compare it to the previous 'counts' to take the highest one. Why is it a NoneType?

3 Answers3

1

You get a None type because you actually return a None.

Notice that when you get to the else part you return the value back up the recursion stack, which goes to the previous call, back to the line countConsecutive (mainstr, substr, count) but you need another return statement in order to return count up the recursion stack all the way to the first call.

Simply change the code to:

def countConsecutive (mainstr, substr, count):
    if substr in mainstr[:len(substr)]:
        count += 1
        mainstr = mainstr[len(substr):]
        return countConsecutive (mainstr, substr, count) #added return statement
    else:
        return count
Rweinz
  • 195
  • 2
  • 7
0
     def countConsecutive(mainstr, substr, count):
        if substr in mainstr[:len(substr)]:
            count += 1
            mainstr = mainstr[len(substr):]
            return countConsecutive (mainstr, substr, count) #added return statement
        else:
            return count

This solution will only return the count if the repeat substring is at the start of the mainstr. This example below returns 0 when there are 3 repeats:

    >>> mainstr = "blue redredred blue red blue green red"
    >>> substr = "red"
    >>> count = 0
    >>> print(countConsecutive(mainstr, substr, count))
    0

The follow returns the highest number of substr repeats in the main string:

    def countConsecutive(mainstr, substr, count, best=0):
        if len(mainstr) > 0:               
        # terminal case
            if substr in mainstr[:len(substr)]:
                # hit: move along len(substr), add to count, update best
                count += 1
                mainstr = mainstr[len(substr):]
                if count > best:
                    best = count
            else:
                # miss: move along 1, reset counter
                mainstr = mainstr[1:]
                count = 0

            return countConsecutive(mainstr, substr, count, best=best)

        else:
            return best

The same test case now returns 3:

    >>> mainstr = "blue redredred blue red blue green red"
    >>> substr = "red"
    >>> count = 0
    >>> print(countConsecutive(mainstr, substr, count))
    3 
Peter Curran
  • 382
  • 2
  • 13
-1

There is a simple solution of using default as follows:

def countConsecutive (mainstr, substr, count=0):
Aaj Kaal
  • 1,205
  • 1
  • 9
  • 8
  • 3
    Have you tested this simple solution? I am pretty convinced it doesn't solve the problem. – Stef Oct 31 '20 at 12:26