I really wanted to create a short answer for this, so this is what I came up with!
See code in use here
s = 'AAGTCCTAG'
d = 'ACGT'
c = len(d)
while c <= len(s):
x,c = s[:c],c+1
if all(l in x for l in d):
print(x)
s,c = s[1:],len(d)
It works as follows:
c
is set to the length of the string of characters we are ensuring exist in the string (d = ACGT
)
- The
while
loop iterates over each possible substring of s
such that c
is smaller than the length of s
.
- This works by increasing
c
by 1 upon each iteration of the while
loop.
- If every character in our string
d
(ACGT
) exist in the substring, we print the result, reset c
to its default value and slice the string by 1 character from the start.
- The loop continues until the string
s
is shorter than d
Result:
AAGTC
AGTC
GTCCTA
TCCTAG
CCTAG
CTAG
To get the output in a list instead (see code in use here):
s = 'AAGTCCTAG'
d = 'ACGT'
c,r = len(d),[]
while c <= len(s):
x,c = s[:c],c+1
if all(l in x for l in d):
r.append(x)
s,c = s[1:],len(d)
print(r)
Result:
['AAGTC', 'AGTC', 'GTCCTA', 'TCCTAG', 'CCTAG', 'CTAG']