Skip to content

Instantly share code, notes, and snippets.

@jerrypy
Created November 5, 2017 09:56
Show Gist options
  • Save jerrypy/e5687ecbcfc86892825932ceeb675bed to your computer and use it in GitHub Desktop.
Save jerrypy/e5687ecbcfc86892825932ceeb675bed to your computer and use it in GitHub Desktop.
MIT 6.00 Assignments solution
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def subStringMatchExact(target, key):
idx = 0
res = ()
while True:
new_idx = target.find(key, idx)
if new_idx < 0:
return res
else:
idx = new_idx + 1
res += (new_idx, )
# This recursive solution is incorrect!
# the answer is (5, 9) which 9 should plus 5 + 1 for the correct answer
#
# def subStringMatchExactRecursive(target, key):
# new_idx = target.find(key)
# if new_idx < 0:
# return ()
# else:
# return (new_idx, ) + subStringMatchExactRecursive(target[new_idx + 1:], key)
print(subStringMatchExact("atgacatgcacaagtatgcat", "atgc"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment