Reputation: 460
I'm trying to count a set of characters, 'qed' in a string. My idea is to iterate through each character in a given string and if N(i), N(i-1), N(i-2) matches 'qed', update the count but failed so far. Any suggestions? Thanks!
def test(N):
s = ('qed')
count = 0
for i in range(len(N)):
if N[i] + N[i-1] + N[i-2] == s:
count = count + 1
return print(count)
test('qedmlqedlolqed')
Upvotes: 0
Views: 87
Reputation: 28596
>>> 'qedmlqedlolqed'.count('qed')
3
Edit: Why the downvote? The question asks "Any suggestions?" and I think this is a good one.
Upvotes: 2
Reputation: 347
though Stefan's answer is simplest and lucid, here's another way to do it using list comprehension
s = 'qedmlqedlolqed'
result = len([1 for i in range(len(s)) if s[i:i+3] == 'qed']) (thanks Stefan)
Upvotes: 2
Reputation: 29896
Fixing your code:
def test(N):
s = 'qed'
count = 0
for i in range(2, len(N)):
if N[i-2] + N[i-1] + N[i] == s:
count = count + 1
return count
print(test('qedmlqedlolqed'))
Or you could count how many suffixes of the string starts with qed:
def test2(word, sub):
return sum(1 for i,_ in enumerate(word) if word[i:].startswith(sub))
print(test2('qedmlqedlolqed', 'qed'))
Or you could just literally count all the substrings, and check the number of yours:
import collections
def all_substrings(s):
for i in range(len(s)):
for j in range(i, len(s)):
yield s[i:j+1]
def test3(word, sub):
return collections.Counter(all_substrings(word))[sub]
print(test3('qedmlqedlolqed', 'qed'))
Upvotes: 1
Reputation: 28596
Using find
repeatedly:
>>> text = 'qedmlqedlolqed'
>>> count = index = 0
>>> while True:
index = text.find('qed', index) + 1
if not index:
break
count += 1
>>> count
3
I went with + 1
instead of + 3
to also support overlapping occurrences (can't happen with 'qed'
or with any other "set of characters", assuming what you meant with that is a string without duplicates).
Upvotes: 0
Reputation: 87054
Fixing your code:
def test(N):
s = 'qed'
count = 0
for i in range(len(N)-2):
if N[i:i+3] == s:
count += 1
return count
>>> test('qedmlqedlolqed')
3
Or more generally:
def test(N, s):
count = 0
if s:
for i in range(len(N)-len(s)+1):
if N[i:i+len(s)] == s:
count += 1
return count
>>> test('qedmlqedlolqed', 'qed')
3
>>> test('qedmlqedlolqed', 'ed')
3
>>> test('qedmlqedlolqed', 'd')
3
>>> test('qedmlqedlolqed', '')
0
>>> test('qedmlqedlolqed', 'lol')
1
>>> test('qedmlqedlolqed', 'rofl')
0
Or, much easier, using str.count()
:
>>> 'qedmlqedlolqed'.count('qed')
3
'
Upvotes: 2