How do I find and count all occurrences of a substring in a string using only find and replace?
The entry should be below, and at the end the program should print the number of occurrences. For example mem.
smthing = str(input()) if (smthing == smthing.lower()): smthing.find(mem)
I am completely deceived so I could not go far.
I forgot to say that I cannot use an account or a list.
source to share
Something like
string = "hello world hello".lower()
replace = "hello".lower()
count = 0
while string.find(replace) != -1:
string = string.replace(replace, "", 1)
count += 1
print count
# => Output
# => 2
To take care of overlapping strings instead of replacing the entire substring, we could just replace one character, preferably the first, replace[0]
from the original string
string = "boboobobobobob".lower()
replace = "bob".lower()
count = 0
while string.find(replace) != -1:
string = string.replace(replace[0], "", 1)
count += 1
print count
# Output
# => 6
source to share
If you have overlapping lines, you will need to replace the aa character by the time:
sub = "bob"
smthing = input()
count = 0
for i in iter(lambda: smthing.find(sub), -1):
smthing = smthing.replace(sub[0], "", 1)
count += 1
print(count)
So for boboobobobobob
you get 6 instead of 3.
If you cannot use the count, but can use one or the other, you can use the substitution yourself, although this will not include overlap:
print((len(smthing) - len(smthing.replace(sub,""))) / len(sub))
source to share
Try this code:
smthing = "blablabla"
mem = "bl"
count = 0
if (smthing == smthing.lower()):
bkpsmthing = smthing # maybe you'll need it later
while (smthing.find(mem) > -1):
smthing = smthing.replace(mem, '', 1)
count += 1
print count
It takes advantage of the fact that it str.find
returns -1 when it finds nothing, or an index to the bottommost record to stop:
Return the lowest index in s where the substring sub is found, so that sub is entirely contained in s [start: end]. Return -1 on error.
It also takes advantage of the fact that it str.replace
can delete one entry (the lowest one) using the thrid ( maxreplace
) argument , so we are constantly deleting the entries we have counted:
... If the optional maxreplace argument is given, the first occurrences of maxreplace are replaced.
The process can be described as follows:
find "bl" in "blablabla" -> found at index 0
replace "bl" in "blablabla" -> gives "ablabla"
find "bl" in "ablabla" -> found at index 1
replace "bl" in "ablabla" -> gives "aabla"
find "bl" in "aabla" -> found at index 2
replace "bl" in "aabla" -> gives "aaa"
find "bl" in "aaa" -> not found, returns -1
stop
To do the same with a variable count
, use this simple recursion (amke is sure you are checking the lowercase string before using my_count
):
def my_count(my_string, my_substring):
if my_string.find(my_substring) > -1:
new_string = my_string.replace(my_substring, '', 1)
return my_count(new_string, my_substring) + 1
return 0
Output:
>>> my_count("blablabla", "bl")
3
The recursion unfolds as follows:
my_count("blablabla", "bl") =
= my_count("ablabla", "bl") + 1 =
= my_count("aabla", "bl") + 1 + 1 =
= my_count("aaa", "bl") + 1 + 1 + 1 =
= 0 + 1 + 1 + 1 = 3
source to share
You don't; you need additional tools, maybe just basic arithmetic. For example, if you replace your substring with another, the length of the result can be compared to the original to count the number of occurrences. Another option is to use the start argument to find additional occurrences. I'm curious to know what you have tried; the code you are showing just doesn't give any result.
source to share
For the record, you can do it with string.replace()
:
smthing = str(input()) word='mem' x=0 while word in smthing: smthing=smthing.replace(word,'',1) x+=1 print x
Demo:
>>>
memory memory memory
3
source to share