-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexf.py
44 lines (37 loc) · 879 Bytes
/
lexf.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
import itertools
SAMPLE_DATASET = """A C G T
2"""
SAMPLE_OUTPUT = """AA
AC
AG
AT
CA
CC
CG
CT
GA
GC
GG
GT
TA
TC
TG
TT"""
## NOTE: https://docs.python.org/2/library/itertools.html#itertools.product
# This pattern creates a lexicographic ordering
# For example, product(A, repeat=4) means the same as product(A, A, A, A).
def main(input_text):
alphabet, repeats = input_text.split("\n")
results = [x for x in itertools.product(alphabet.replace(" ", ""), repeat=int(repeats))]
return_string = ''
for pair in results:
return_string += "".join(pair) + "\n"
return return_string.strip()
if __name__ == "__main__":
## Test
# print main(SAMPLE_DATASET)
assert main(SAMPLE_DATASET) == SAMPLE_OUTPUT
## Prod
with open("./datasets/rosalind_lexf.txt", 'r') as fptr:
input_text = fptr.read().strip()
print main(input_text)