def count_substring(string, sub_string):
count = 0
for i in range(0 , len(string)):
if ( string[i: ].find(sub_string)) == True:
count = count +1
return count
STRING = 'ininini'
SUB_STRING = 'ini'
CORRECT OUTPUT : 3
MY OUTPUT : 2
it is not detecting the last substring.
the problem is that
string[i:].find(sub_string)
returns -1 if not found or the position if found. You want to test for 0 you're testing for position 1 (aka True) (https://docs.python.org/3/library/stdtypes.html#str.find).
It's not "not detecting the last substring", it's detecting bogus matches.
You could use startswith instead:
def count_substring(string, sub_string):
count = 0
for i in range(0,len(string)):
if string[i:].startswith(sub_string):
count += 1
return count
Note that using find isn't a bad idea at all, since you don't have to slice the string (it's faster), there's a start position parameter which is handy here:
def count_substring(string, sub_string):
count = 0
for i in range(0,len(string)):
if string.find(sub_string,i) == i:
count += 1
return count
or in one line:
def count_substring(string, sub_string):
return sum(1 for i in range(len(string)) if string.find(sub_string,i) == i)
note that string.count(sub_string) doesn't yield the same result because it doesn't consider overlapping strings, like your solution does.
Related
I want to know if my string contains more numbers or more alphabets.
I have tried using regex in python and putting a condition in between.
search_3 = '(\d) > (\D)'
words["aplha_or_numeric_mark"] = words["Words"].str.findall(search_3)
print(words)
The actual result is just an empty list on each row
Expected results :
123ABCD should output 1 since alphabets > numbers.
1234ABC should output 0 since alphabets < numbers.
This should work.
string = "ABCD12345"
num_count = 0
word_count = 0
for i in string:
if i.isalpha():
word_count += 1
elif i.isdigit():
num_count += 1
if word_count > num_count:
print(1)
else:
print(0)
You can do using zip on a generator:
def is_alpha_more(s):
total_alphas, total_nums = zip(*((x.isalpha(), x.isdigit()) for x in s))
return 1 if sum(total_alphas) >= sum(total_nums) else 0
Sample run:
>>> s = '12,"BCD'
>>> is_alpha_more(s)
1
>>> s = '1234A,":B'
>>> is_alpha_more(s)
0
Why not just use re.findall to find the count of both and get the results?
import re
s = '123ABCD'
numAlphabets = len(re.findall('[a-zA-Z]', s))
numDigits = len(re.findall('\d', s))
if numAlphabets > numDigits:
print('More alphabets then digits')
elif numDigits > numAlphabets:
print('More digits then alphabets')
else:
print('Same numbers for both')
For this case it prints,
More alphabets then digits
Also, if all you want to return 1 if more alphabets and 0 if less alphabets then digits, you can use this function,
import re
def has_more_alphabets(s):
if len(re.findall('[a-zA-Z]', s)) > len(re.findall('\d', s)):
return 1
else:
return 0
print(has_more_alphabets('123ABCD'))
print(has_more_alphabets('123##334ABCD'))
print(has_more_alphabets('123###ad553353455ABCD'))
print(has_more_alphabets('123BCD'))
Prints following,
1
0
0
0
There are many ways to accomplish what you ask. Regular expressions are meant for "search" or "search and replace" in strings. You need to count. One example would be something like:
def test_string(text):
count_letters = 0
count_digits = 0
for character in text:
if character.isalpha():
count_letters += 1
elif character.isdigit():
count_digits += 1
if count_letters > count_digits:
return 1
return 0
You still haven't defined what should happen if the two numbers are equal, but that should be easy case to add.
For example:
list_strings = 'dietcoke', 'dietpepsi', 'sprite'
Here's what I did:
count = 0
list =
for ch in list_strings:
if ch == sub:
count += 1
print((list_strings, 'diet') == 2) is suppose to return True but it returns False.
I hope I understood you correctly.
Just use in to check if your substring is present in the mainstring.
list = ['dietcoke', 'dietpepsi', 'sprite']
Your function should look like this:
def myfuncname(list_strings, sub_string):
count = 0
for ch in list_strings:
if sub_string in ch:
count += 1
return count
If we call count now, we get count == 2
>>> print(myfuncname(list_strings, 'diet') == 2)
True
Hope that solved your problem.
This is in reference to problem that is done in Java:
Finding the intersection between two list of string candidates.
I tried to find an equivalent solution in python:
def solution(S):
length = len(S)
if length == 0:
return 1
count = 0
i = 0
while i <= length:
prefix = S[0:i:]
suffix = S[length-i:length]
print suffix
if prefix == suffix:
count += 1
i += 1
return count
print solution("")
print solution("abbabba")
print solution("codility")
I know I am not doing the step in terms of suffix.
I am getting the values as 1,4,2 instead of 1,4,0
Your current code runs through giving the following prefix, suffix pairs for the second two examples:
a a
ab ba
abb bba
abba abba
abbab babba
abbabb bbabba
abbabba abbabba
c y
co ty
cod ity
codi lity
codil ility
codili dility
codilit odility
codility codility
I presume you are trying to return the number of times the first n characters of a string are the same as the last n. The reason the word codility is returning 2 is because you are starting the index from zero, so it is matching the empty string and the full string. Try instead:
def solution(S):
length = len(S)
if length == 0:
return 1
count = 0
i = 1 # Don't test the empty string!
while i < length: # Don't test the whole string! Use '<'
prefix = S[:i] # Up to i
suffix = S[length-i:] # length - i to the end
print prefix, suffix
if prefix == suffix:
count += 1
i += 1
return count
print solution("")
print solution("abbabba")
print solution("codility")
This returns 1, 2, 0.
Perhaps you were looking to test if the prefix is the same as the suffix backwards, and only test half the length of the string? In this case you should try the following:
def solution(S):
length = len(S)
if length == 0:
return 1
count = 0
i = 1 # Don't test the empty string!
while i <= (length + 1)/2: # Test up to halfway
prefix = S[:i] # Up to i
suffix = S[:length-i-1:-1] # Reverse string, up to length - i - 1
print prefix, suffix
if prefix == suffix:
count += 1
i += 1
return count
print solution("")
print solution("abbabba")
print solution("codility")
This returns 1, 4, 0.
This question already has answers here:
String count with overlapping occurrences [closed]
(25 answers)
Closed 1 year ago.
Say I have string = 'hannahannahskdjhannahannah' and I want to count the number of times the string hannah occurs, I can't simply use count, because that only counts the substring once in each case. That is, I am expecting to return 4 but only returns 2 when I run this with string.count('hannah').
You could use a running index to fetch the next occurance:
bla = 'hannahannahskdjhannahannah'
cnt = 0
idx = 0
while True:
idx = bla.find('hannah', idx)
if idx >= 0:
cnt += 1
idx += 1
else:
break
print(cnt)
Gives:
>> 4
How about something like this?
>>> d = {}
>>> string = 'hannahannahskdjhannahannah'
>>> for i in xrange(0,len(string)-len('hannah')+1):
... if string[i:i+len('hannah')] == 'hannah':
... d['hannah'] = d.get('hannah',0)+1
...
>>> d
{'hannah': 4}
>>>
This searches the string for hannah by splicing the string iteratively from index 0 all the way up to the length of the string minus the length of hannah
'''
s: main string
sub: sub-string
count: number of sub-strings found
p: use the found sub-string's index in p for finding the next occurrence of next sub-string
'''
count=0
p=0
for letter in s:
p=s.find(sub,p)
if(p!=-1):
count+=1
p+=1
print count
If you want to count also nonconsecutive substrings, this is the way to do it
def subword(lookup,whole):
if len(whole)<len(lookup):
return 0
if lookup==whole:
return 1
if lookup=='':
return 1
if lookup[0]==whole[0]:
return subword(lookup[1:],whole[1:])+subword(lookup,whole[1:])
return subword(lookup,whole[1:])
def Count_overlap(string, substring):
count = 0
start = 0
while start < len(string):
pos = string.find(substring, start)
if pos != -1:
start = pos + 1
count += 1
else:
break
return count
string = "hannahannahskdjhannahannah"
print(Count_overlap(string, "hannah"))
Don't want to answer this for you as it's simple enough to work out yourself.
But if I were you I'd use the string.find() method which takes the string you're looking for and the position to start looking from, combined with a while loop which uses the result of the find method as it's condition in some way.
That should in theory give you the answer.
So the function should count the number of times the letters in uppercase are out of abc order.
>>> abc('ABBZHDL')
2
Above, z and d are out of order.
>>> abc('ABCD')
0
>>> abc('DCBA')
4
My code:
def abc(check):
order=ABCDEFGHIJKLMNOPQRSTUVWXYZ
for c in check:
if check != order:
#then I get stuck here
Pointers?
The question is ill-defined. One solution to a nearby question would be using the builtin sorted():
def abc(s):
count = 0
s = ''.join(i for i in s if i.isupper())
l = sorted(s)
for i,c in enumerate(s):
if l[i] != c:
count += 1
return count
It counts all of the places where the alphabetized string does not match the original.
def abc(check):
last = ''
count = 0
for letter in check:
if not letter.isupper():
continue
if letter < last:
count += 1
last = letter
return count
import string
a = 'acbdefr'
b = 'abdcfe'
assert ''.join(sorted(b)) in string.ascii_letters
assert ''.join(sorted(a)) in string.ascii_letters #should fail
Its really simple everyone seems to be overcomplicating it somewhat?