-
Notifications
You must be signed in to change notification settings - Fork 41
/
e009-subs.py
executable file
·56 lines (44 loc) · 1.46 KB
/
e009-subs.py
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
48
49
50
51
52
53
54
55
#!/usr/bin/env python
# coding=utf-8
# Finding a Motif in DNA
# ======================
#
# Given two strings s = s1 s2 ... sn and t = t1 t2 ... tm where m ≤ n, t is a
# substring of s if t is contained as a contiguous collection of symbols in s.
#
# The position of a symbol in a string is the total number of symbols found to
# its left, including itself (e.g., the positions of all occurrences of U in
# AUGCUUCAGAAAGGUCUUACG are 2, 5, 6, 15, 17, and 18). The symbol at position i
# of s is denoted by s[i]. For that matter, a substring of s can be represented
# as s[j:k], where j and k represent the starting and ending positions of the
# substring in s.
#
# The location of a substring is its beginning position; note that t will have
# multiple locations in s if it occurs more than once as a substring of s (see
# the Sample sections below).
#
# Given: Two DNA strings s and t (each of length at most 1 kbp).
#
# Return: All locations of t as a substring of s.
#
# Sample Dataset
# --------------
# ACGTACGTACGTACGT
# GTA
#
# Sample Output
# -------------
# 3 7 11
def locations(s_and_t):
results = []
s, t = s_and_t.split()
l = len(t)
for i in range(len(s) - l):
if s[i:i+l] == t:
results.append(i + 1)
return results
if __name__ == "__main__":
small_dataset = "ACGTACGTACGTACGT\nGTA"
large_dataset = open('datasets/rosalind_subs.txt').read()
results = locations(large_dataset)
print ' '.join(map(str, results))