-
-
Save renzon/0e86be7137d8bbdab9b6cf4e1fff8e9c to your computer and use it in GitHub Desktop.
Problema do Jimmy Neutron
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import deque | |
from itertools import cycle | |
import pytest | |
REPLACEMENTS = ["CCTTGG", "CTATGG", "CCACGG"] | |
def replace_gen_sub_seq(seq, replacements): | |
seq = list(seq) | |
replacements_len = len(replacements[0]) | |
x_pattern = deque('X' * replacements_len) | |
current_substring = deque(maxlen=replacements_len) | |
def append_and_return_exceeding_item(item_to_append): | |
"""Append item do current substring and returns exceding char if its maxlen is reached | |
:param item_to_append: | |
:return: item or None | |
""" | |
exceeding_item = None | |
if len(current_substring) == replacements_len: | |
exceeding_item = current_substring[0] | |
current_substring.append(item_to_append) | |
return exceeding_item | |
replacements_cycle = cycle(replacements) | |
next(replacements_cycle) # advancing one | |
for char, replacement in zip(seq, replacements_cycle): | |
exceding_char = append_and_return_exceeding_item(char) | |
if exceding_char: | |
yield exceding_char | |
if current_substring == x_pattern: | |
yield from replacement | |
current_substring.clear() | |
yield from current_substring | |
@pytest.mark.parametrize( | |
'seq,expected_seq', | |
[ | |
("XXXXXXXXXXXXTC", "CCTTGGCCTTGGTC"), | |
("CXXXXXXXXXXXXT", "CCTATGGCTATGGT"), | |
("CTXXXXXXXXXXXX", "CTCCACGGCCACGG"), | |
("CTXXXXXXXXXXXXXXXXXX", "CTCCACGGCCACGGCCACGG"), | |
("XXXXXXCXXXXXXTXXXXXX", "CCTTGGCCTATGGTCCACGG"), | |
] | |
) | |
def test_repeats_juntas(seq, expected_seq): | |
seq_output = ''.join(replace_gen_sub_seq(seq, REPLACEMENTS)) | |
assert expected_seq == seq_output |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment