-2

I want to count the number of instances where consecutive letters are identical in a given string.

For example, my string input is:

EOOOEOEE

I would only like to find the number of occasions where there is more than one consecutive 'O'.

The Output should be:

1

Since, there is only one set of O's that come consecutively.

jpp
  • 159,742
  • 34
  • 281
  • 339
Mahir
  • 400
  • 3
  • 15
  • So a single "O" doesn't count? There have to be two or more? – Aran-Fey Apr 11 '18 at 09:56
  • What have you tried so far? – FHTMitchell Apr 11 '18 at 09:56
  • 1
    Explain your I/O in more details – Rachit kapadia Apr 11 '18 at 09:56
  • 1
    Possible duplicate of [Count consecutive characters](https://stackoverflow.com/questions/34443946/count-consecutive-characters) – l'L'l Apr 11 '18 at 09:57
  • @l'L'l I don't think that's what OP wants. That would output 3 and 1 here. If I understand correctly, the OP wants to count how many runs of two or more Os there are in the string. – Aran-Fey Apr 11 '18 at 09:59
  • @Aran-Fey: The OP should figure that out by looking at the similar question and answers. – l'L'l Apr 11 '18 at 10:04
  • 1
    @l'L'l Tweaking the code from those answers to produce the correct result here would take more effort than writing an answer from scratch. If the OP can't write a solution from scratch, I don't see how they could possibly find the answers in that question useful. I think that's _far_ too different of a question to be a suitable dupe. – Aran-Fey Apr 11 '18 at 10:09
  • @Aran-Fey I agree – Joe Iddon Apr 11 '18 at 10:10
  • @Aran-Fey: The OP didn't try to write a solution — or appear to try anything for that matter. I'll agree maybe the other question is a bit different, although at the very least a MCVE is generally better than appearing to run a free coding service for self entitled users. – l'L'l Apr 11 '18 at 10:15

4 Answers4

3

This is possible with itertools.groupby:

from itertools import groupby

x = 'EOOOEOEE'

res = sum(len(list(j)) > 1 for i, j in groupby(x) if i == 'O')  # 1
jpp
  • 159,742
  • 34
  • 281
  • 339
1

You can use a regex:

>>> import re
>>> s = 'EOOOEOEEOO'
>>> sum(1 for x in re.finditer(r'O{2,}', s))
2
Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
0

Just count with a for-loop:

n = 0
g = 0
s = 'EOOOEOEE'
for c in s:
    if c == 'O':
        g += 1
    elif g:
        if g > 1:
            n += 1
        g = 0

if g:
    n += 1

which gives n as 1.

Joe Iddon
  • 20,101
  • 7
  • 33
  • 54
0

I assume you want to know the number of times that all letters are consecutive in a string and not just for the letter 'O'.

Make a character dictionary that will hold this count as values to keys as the characters in a string. char_dict = {}

The idea is to have two conditions to match

(1) Is the current character same as the previous character

(2) If the first condition is true then is the current pair of consecutive characters part of a larger substring that has the same consecutive characters. Simply put, take for example ABBBCBB. When we encounter the third B we want to check whether it is part of sub-string that is consecutive and already accounted for. i.e. BBB should give consecutive count to be 1 and not 2. To implement this we use a flag variable that checks this conditions.

If we use only the (1)st condition BBB will count as BB and BB and not as a single BBB.

Rest of the code is pretty straight forward.

char_dict = {}
string = "EOOOEOEEFFFOFEOOO"
prev_char = None
flag=0

for char in list(string):
    if char not in char_dict:
        #initialize the count by zero
        char_dict[char] = 0

if char == prev_char and flag !=0:
    char_dict[char] += 1
    flag = 0
else:
    flag = 1

prev_char = char

print(char_dict)