-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpset3a.py
More file actions
47 lines (43 loc) · 1.38 KB
/
pset3a.py
File metadata and controls
47 lines (43 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#divide and conquer
from string import *
def countSubStringMatch(target,key):
start = 0
inst = []
found = True
for x in target:
i = find(target,key,start)
if i == -1 and start == 0:
print "No instance of \"%s\" found"%(key)
found = False
break
elif i == -1 and start > 0:
break
else:
inst.append(i)
start = i + len(key)
if found:
return "%i instances found" % len(inst)
else:
return "No Instances found"
instances = 0
def countSubStringMatchRecursive(target,key,start=0):
global instances
if len(target) < len(key):
return "Search term can't be bigger than search string"
end = len(key) + start
i = find(target[start:end],key)
if i == -1:
countSubStringMatchRecursive(target[start+1:],key)
else:
instances += 1
countSubStringMatchRecursive(target[start+end:],key)
return "%i instances found" % instances
DNA_strand = "atgacatgcacaagtatgcat"
sequence = "at"
print "-"*51
print "Searching for \"%s\" from \"%s\"" % (sequence,DNA_strand)
print "-"*51
print "Using countSubStringMatchRecursive(): ",
print countSubStringMatchRecursive(DNA_strand,sequence)
print "Using countSubStringMatch(): ",
print countSubStringMatch(DNA_strand,sequence)