In a project I am working on, I am checking to see if certain DNA sequences appear in certain genes in E. coli. I have written a program in Java that performs the below features. However, since I wrote my program in Java, the universe needs to be corrected and rid of verbosity by having you write your code in as few bytes as possible.

My program took a DNA sequences and a regex as input (for this challenge, order doesn't matter). The DNA sequence contains only the letters A, C, G, and T. It will be determined if the DNA sequence contains any subsequences that follow the regex, and the output of the program will be the one-indexed location(s) of the first character of each matching subsequence in the DNA sequence in your choice of format. If there is no such location, you may output any reasonable indicator of this.

The regex I used had the following specifications.

  • A, C, G, and T match to themselves.
  • N matches to any of A, C, G, and T
  • (X/Y) means that at the current location in the sequence, the next character(s) in the sequence match X or Y (note that X and Y can be multiple characters long, and that there can be multiple slashes; for example, (AA/AG/GA) matches to AA, AG or GG; however, X and Y must have the same length)

You may assume that the DNA sequence is longer than any sequence that follows the specifications of the regex, and that both the DNA and the regex are valid.

Test cases

Here, the DNA sequence is first, and the regex is second.

==> 1 2 3 4 5 6 7 8

==> 1 5

==> 5 8 15

==> 2 12

This is . The shortest answer in bytes wins. Standard rules apply.

  • \$\begingroup\$ Inb4 someone asks me why I didn't use Java's native regex system; I don't know. \$\endgroup\$
    – Arcturus
    Jun 9 '16 at 21:09

Python 3, 125 bytes

import re,sys
print([i+1 for i in range(len(a[1]))if re.match(s('N','[ACGT]',s('/','|',a[2])),a[1][i:])])
  • \$\begingroup\$ Can you use a=input() instead of the sys stuff? \$\endgroup\$ Jul 2 '16 at 13:36

Pyth, 28 bytes


Simple regex matching.

I hope there had a function which returns the position of each match, but there is not.

Test suite.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.