user8813607
user8813607

Reputation:

Counting the number of specific characters ignoring duplicates: Python

I have an input like this: BFFBFBFFFBFBBBFBBBBFF . I want to count 'B's and the answer should be 6.(ignore the duplicate ones)

How to do it in python?

Upvotes: 2

Views: 215

Answers (7)

jabargas
jabargas

Reputation: 210

Alternative solution:

s = 'BFFBFBFFFBFBBBFBBBBFF'
l = [c for i,c in enumerate(s) if s[i-1] != c]
l.count('B') #or use counter
>>>6

Upvotes: 0

Mickaël Bucas
Mickaël Bucas

Reputation: 691

You want to count when the letters change from F to B, and another function can do that : split. It removes all Fs, but create empty strings for consecutive Fs, so we must remove them from the count.

s = "BFFBFBFFFBFBBBFBBBBFF"                                                                                                                                                             
t = s.split('F')                                                                                                                                                                        
n = sum([1 for b in t if len(b) > 0])                                                                                                                                                   
print(n)

Upvotes: 0

Anton vBR
Anton vBR

Reputation: 18916

Another solution by first removing duplicates using RE-library:

import re

l1 = "BFFBFBFFFBFBBBFBBBBFF"
l2 = re.sub(r'([A-z])\1+', r'\1', l1) # Remove duplicates

l2.count("B") # 6

Upvotes: 0

Kaushik NP
Kaushik NP

Reputation: 6781

Use itertools.groupby :

>>> from itertools import groupby
>>> l = [k for k,v in groupby(s)]

>>> l
=> ['B', 'F', 'B', 'F', 'B', 'F', 'B', 'F', 'B', 'F', 'B', 'F']

>>> l.count('B')
=> 6

#driver values :

IN : s = 'BFFBFBFFFBFBBBFBBBBFF

EDIT : Also, for more extensive use, its better to use collections.Counter to get count for all the characters.

>>> from collections import Counter
>>> Counter(l)
=> Counter({'B': 6, 'F': 6})

Upvotes: 2

Nir Alfasi
Nir Alfasi

Reputation: 53535

We can remove consecutive dups and use collections.Counter to count the B's that are left:

from collections import Counter

def remove_conseq_dups(s):
    res = ""
    for i in range(len(s)-1):
        if s[i] != s[i+1]:
            res+= s[i]
    return res

s = "BFFBFBFFFBFBBBFBBBBFF"
print(Counter(remove_conseq_dups(s))['B']) # 6

And a groupby solution:

from itertools import groupby

s = "BFFBFBFFFBFBBBFBBBBFF"
print(sum(map(lambda x: 1 if x == 'B' else 0, [x for x, v in groupby(s)])))

Or

print(len(list(filter(lambda x: x == 'B', [x for x, v in groupby(s)]))))

Upvotes: 1

not_python
not_python

Reputation: 902

s = "BFFBFBFFFBFBBBFBBBBFF"
f = False
count = 0
for i in s:
    if f and i == 'B':
        continue
    elif i == 'B':
        count += 1
        f = True
    else:
        f = False
print(count)

another

from itertools import groupby
count = 0
for i,_ in groupby(s):
    if i == 'B':
        count += 1
print(count)

Upvotes: 2

Chen A.
Chen A.

Reputation: 11280

You should set a counter and a flag variable. Then count only occurences which are not duplicates, and flip the flag. The logic is simple: if current letter is 'B', and you the letter before isn't 'B' (dup = False), then count it + flip the boolean:

s = 'BFFBFBFFFBFBBBFBBBBFF'

count = 0
dup = False
for l in s:
    if l == 'B' and not dup:
        count += 1
        dup = True
    elif l != 'B':
        dup = False

# count: 6

Upvotes: 1

Related Questions