Hi,
Again I'm a newbie, going through how to think like a computer scientist online tutorial. I have 2 questions in the next lesson that I can't seem to crack, in relation to sub strings.
Here's the questions and my working below:
This one I can remove letters but I can't seem to remove the sub string only. The answer should be mippi and I keep getting mpp.
def remove_all(sub, strng):
new_word = ''
for letters in strng:
print letters
if letters not in sub:
new_word += letters
print new_word
remove_all('iss', 'mississippi')
This next question is again with sub strings and counting how many sub strings in the string. I've tried 2 different ways and neither work.. Here they are:
import string
def c_sub(sub, strng, start=0):
"""
>>> count ('an', 'banana')
2
"""
count = 0
index = string.find(sub, strng)
while index != -1:
count += 1
start = index + 1
return count
print c_sub("an", "banana")
and 2nd working:
def c_sub(sub, strng, start=0):
"""
>>> count ('an', 'banana')
2
"""
count = 0
for l in strng:
for s in sub:
if l == s:
count += 1
return count
print c_sub("an", "banana")