MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming: Lesson 5, HW 1
def countSubStringMatch(target,key):
count = 0
pos = -1
while True:
pos = target.find(key,pos+1)
if pos >= 0:
count += 1
if pos == -1:
break
return count
def countSubStringMatchRecursive(target,key):
if target.find(key) >=0:
bef,sep,target = target.partition(key)
return 1 + countSubStringMatchRecursive(target,key)
return 0
def subStringMatchExact(target,key):
pos = target.find(key)
allpos = []
allpos.append(pos)
while True:
if pos <= -1:
return tuple(allpos)
pos = target.find(key,pos+1)
if pos >= 0:
allpos.append(pos)
return tuple(allpos)
def constrainedMatchPair(firstMatch, secondMatch, length):
allMembers = []
for i in firstMatch:
for j in secondMatch:
if i + length + 1 == j:
allMembers.append(i)
return tuple(allMembers)
mattijle
1 year ago