-3

folks, I am using the count function for counting the occurrence of substring in the main string it works fine but in one test case when I rented BANANA its give me one logical error as in BANANA substring ANA occurs twice but show only one time here is my code and result. Need guidance for a better understanding of AS you can see in ANA has only one count number it should be two as ANA occurred twice in BANANA-> B-AN-A-NAwhy its hoping

enter code here

stuart=[]
kevin=[]


def minion_game(string):
    score_stuart=0
    score_kevin=0
    data=['A','E','I','O','U']
    result=[string[i:j] for i in range(len(string)+1) for j in range(i+1,len(string)+1)]
    for i in range(len(result)):
        if result[i].startswith(tuple(data)):
            if result[i] not in Kevin:
                kevin.append(result[i])                
        else:
            if result[i] not in stuart:
                stuart.append(result[i])

    for i in range(len(kevin)):
        str_temp=string.count(kevin[i])
        print(kevin[i],str_temp)
        score_kevin += int(str_temp)
    
    for i in range(len(stuart)):
        str_temp=string.count(stuart[i])
        score_stuart += int(str_temp)

    print('Kevin' ,score_kevin)
    print('Stuart',score_stuart)


if __name__ == '__main__':
    s = input()
    minion_game(s)
Milan
  • 46
  • 7
  • 2
    See the docs: https://docs.python.org/3/library/stdtypes.html#str.count: _"Return the number of **non-overlapping occurrences** of substring sub [...]"_ – Selcuk Jan 10 '22 at 06:16

1 Answers1

1

If for some reason you need overlapping occurrences, you can use regex module(not to be confused with re module)

import regex

s = 'BANANA'
matches = regex.findall('ANA', s, overlapped=True)
print(len(matches))

Output:

2
bottledmind
  • 603
  • 3
  • 10