31
\$\begingroup\$

Problem

Create a function that can determine whether or not an arbitrary DNA string is a Watson-Crick palindrome. The function will take a DNA string and output a true value if the string is a Watson-Crick palindrome and a false value if it is not. (True and False can also be represented as 1 and 0, respectively.)

The DNA string may either be in all uppercase or all lowercase depending on your preference.

Also, the DNA string will not be empty.

Explanation

A DNA string is a Watson-Crick palindrome when the complement of its reverse is equal to itself.

Given a DNA string, first reverse it, and then complement each character according to the DNA bases (A ↔ T and C ↔ G). If the original string is equal to the complemented-reverse string, it is a Watson-Crick palindrome.

For more, see this question. It is a different challenge where you must find the longest substring of a DNA string where that substring is a Watson-Crick palindrome.

Goal

This is code-golf and the shortest code wins.

Test Cases

The format is <input> = <output>.

ATCGCGAT = true
AGT = false
GTGACGTCAC = true
GCAGTGA = false
GCGC = true
AACTGCGTTTAC = false
ACTG = false
\$\endgroup\$
  • \$\begingroup\$ Related. \$\endgroup\$ – Martin Ender Apr 25 '16 at 6:39
  • 3
    \$\begingroup\$ Someone should write a program in DNA# that is also a Watson-Crick palindrome. :D (might not be possible) \$\endgroup\$ – mbomb007 Apr 25 '16 at 16:49
  • \$\begingroup\$ Or, if you like, "a word is a Watson–Crick palindrome if it has order 2 in the free group on 2 generators" (or on n generators!). \$\endgroup\$ – wchargin Apr 26 '16 at 3:19
  • \$\begingroup\$ (I guess technically that's "order at most 2.") \$\endgroup\$ – wchargin Apr 26 '16 at 3:19
  • 1
    \$\begingroup\$ @AndrasDeak According to Watsons book, Franklin was apparently mostly a thorn in their side. She repeatedly refused to hand over x-rays showing the helix (as I recall), because she refused to believe it. Its worth a read if you are interested in the discovery at any rate. \$\endgroup\$ – Obsidian Phoenix Apr 27 '16 at 15:19

32 Answers 32

1
\$\begingroup\$

J, 19 bytes

|.-:-&.('+AGCT'i.])

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Wolfram Language (Mathematica), 45 bytes

17-#==Reverse@#&@Mod[ToCharacterCode@#,11,2]&

Try it online!

Converts A to 10, T to 7, C to 12, and G to 5, by taking the ASCII codes mod 11 with offset 2. Then checks if the resulting list and its reverse add to 17 in each coordinate.

\$\endgroup\$

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.