Text to DNA golf


Convert input into a DNA output.


  • Convert text into ASCII code points (e.g. codegolf -> [99, 111, 100, 101, 103, 111, 108, 102])
  • String the ASCII codes together (e.g. 99111100101103111108102)
  • Convert to binary (e.g. 10100111111001101001011010001000011001101011011110000110010111111011000000110)
  • Pad 0s onto the end to make an even number of characters (e.g. 101001111110011010010110100010000110011010110111100001100101111110110000001100)
  • Replace 00 with A, 01 with C, 10 with G, and 11 with T (e.g. GGCTTGCGGCCGGAGACGCGGTCTGACGCCTTGTAAATA)
  • Output

Test Cases

} > TTGG (padding)


  • This is
  • Your program must accept spaces in input.
  • Your program must work for codegolf.
  • 2
    \$\begingroup\$ I think you should add a test case that requires the padding behaviour. The lazy choice would be } which I believe becomes TTGG. \$\endgroup\$ – FryAmTheEggman May 2 '16 at 19:22
  • 3
    \$\begingroup\$ How large of input do we need to support? 99111100101103111108102 for example is larger than uint-64, so some languages may struggle with bigger conversions. \$\endgroup\$ – AdmBorkBork May 2 '16 at 19:46
  • 4
    \$\begingroup\$ That is not how you string ASCII codes together if you want to ever be able to decode them again. \$\endgroup\$ – user253751 May 4 '16 at 3:22
  • \$\begingroup\$ @immibis I know. \$\endgroup\$ – NoOneIsHere May 4 '16 at 3:51

22 Answers 22


Jelly, 15 13 bytes


Try it online! or verify all test cases.

How it works

OVBs2UḄị“GCTA    Main link. Argument: s (string)

O                Ordinal; replace each character with its code point.
 V               Eval. This converts the list to a string before evaluating, so it
                 returns the integer that results of concatenating all the digits.
  B              Binary; convert from integer to base 2.
   s2            Split into chunks of length 2.
     U           Upend; reverse the digits of each chunk.
                 Reversing means that we would have to conditionally PREPEND a zero
                 to the last chunk, which makes no difference for base conversion.
      Ḅ          Unbinary; convert each chunk from base 2 to integer.
                 `UḄ' maps:
                     [0, 1   ] -> [1,    0] -> 2
                     [1, 0(?)] -> [0(?), 1] -> 1
                     [1, 1   ] -> [1,    1] -> 3
                     [0, 0(?)] -> [0(?), 0] -> 0
       ị“GCTA    Replace each number by the character at that index.
                 Indexing is 1-based, so the indices are [1, 2, 3, 0].

CJam, 24 23 bytes

Thanks to Dennis for saving 1 byte in a really clever way. :)


Test it here.


Very direct implementation of the specification. The only interesting bit is the padding to an even number of zeros (which was actually Dennis's idea). Instead of treating the digits in each pair in the usual order, we make the second bit the most significant one. That means, ending in a single bit is identical to appending a zero to it, which means we don't have to append the zero at all.

l          e# Read input.
:i         e# Convert to character codes.
si         e# Convert to flat string and back to integer.
2b         e# Convert to binary.
2/         e# Split into pairs.
Wf%        e# Reverse each pair.
2fb        e# Convert each pair back from binary, to get a value in [0 1 2 3].
"AGCT"f=   e# Select corresponding letter for each number.
  • \$\begingroup\$ I don't know anything about CJam, but why do you need to reverse each pair? Can you not convert them directly back from binary? \$\endgroup\$ – Value Ink May 3 '16 at 1:41
  • \$\begingroup\$ @KevinLau-notKenny Reversing each pair avoids appending zeroes to get an even length. In the reversed pairs, you'd have to prepend zeroes, which doesn't matter for base conversion. \$\endgroup\$ – Dennis May 3 '16 at 1:52
  • \$\begingroup\$ Nice trick! It would probably have saved a ton of bytes on my own solution if I had thought about that trick \$\endgroup\$ – Value Ink May 3 '16 at 2:05

Python 2, 109 103 bytes

lambda s,j=''.join:j('ACGT'[int(j(t),2)]for t in
zip(*[iter(bin(int(j(`ord(c)`for c in s))*2)[2:])]*2))

Test it on Ideone.


Ruby, 59 bytes


A full program. Run with the -p flag.

  • \$\begingroup\$ how did you even... i don't understand \$\endgroup\$ – Value Ink May 3 '16 at 18:45

Python 3, 130 bytes.

Saved 2 bytes thanks to vaultah.
Saved 6 bytes thanks to Kevin Lau - not Kenny.

I hate how hard it is to convert to binary in python.

def f(x):c=bin(int(''.join(map(str,map(ord,x)))))[2:];return''.join('ACGT'[int(z+y,2)]for z,y in zip(*[iter(c+'0'*(len(c)%2))]*2))

Test cases:

assert f('ppcg') == 'GGCTAATTGTCGCACTT'
  • \$\begingroup\$ Looks like you have 1 extra pair of brackets after the second ''.join \$\endgroup\$ – vaultah May 2 '16 at 19:54
  • \$\begingroup\$ @vaultah Oops, yup, you're right. \$\endgroup\$ – Morgan Thrapp May 2 '16 at 19:55
  • \$\begingroup\$ Use 'ACGT'[int(z+y,2)] instead, converting directly out of binary instead of using your longer string and converting from base 10. Also, unsure how much difference it would make but look at using re.sub instead of your messy join trick? \$\endgroup\$ – Value Ink May 2 '16 at 19:56
  • \$\begingroup\$ @KevinLau-notKenny Oooo, thanks. I forgot you can specify a base with int. I'll look into re.sub, thanks for the suggestion. \$\endgroup\$ – Morgan Thrapp May 2 '16 at 19:58
  • \$\begingroup\$ Nice approach, I came up with (almost) exactly the same code without having looked at yours. :) \$\endgroup\$ – Byte Commander May 3 '16 at 11:48

Ruby, 80 bytes

->s{s=s.bytes.join.to_i.to_s 2;s+=?0*(s.size%2)
s.gsub(/../){"ACGT"[$&.to_i 2]}}
  • \$\begingroup\$ As straightforward as the problem is, it's possible to squeeze a lot more bytes out of this :) \$\endgroup\$ – xsot May 3 '16 at 12:41

Mathematica, 108 bytes


Takes a string as input, and outputs a list of bases.


Python 3, 126 bytes

lambda v:"".join(["ACGT"[int(x,2)]for x in map(''.join,zip(*[iter((bin(int("".join([str(ord(i))for i in v])))+"0")[2:])]*2))])
  • \$\begingroup\$ Welcome to Programming Puzzles & Code Golf! In case you're wondering about the downvote, this is what happened. \$\endgroup\$ – Dennis May 15 '16 at 2:44

Pyth, 25 bytes


Try it here!


Burrowing the padding trick from Martins CJam answer.

sm@"ACGT"id2Pc.B*4sjkCMQ2    # Q = input

                     CMQ     # Map each character of Q to its character code
                  sjk        # Join into one string and convert to an integer
              .B*4           # Mulitply with 4 and convert to binary
             c          2    # Split into pairs
            P                # Discard the last pair
 m                           # Map each pair d
         id2                 # Convert pair from binary to decimal
  @"ACGT"                    # Use the result ^ as index into a lookup string
s                            # Join the resulting list into on string

05AB1E, 23 bytes



Uses CP-1252 encoding. Try it online!.


Java, 194 bytes

String a(int[]a){String s="",r=s;for(int i:a)s+=i;s=new BigInteger(s).toString(2)+0;for(int i=0,y,n=48;i<(s.length()/2)*2;r+=s.charAt(i++)==n?y==n?'A':'G':y==n?'C':'T')y=s.charAt(i++);return r;}


String a(int[] a) {
    String s = "", r = s;
    for (int i : a) s += i;
    s = new BigInteger(s).toString(2) + 0;
    for (int i = 0, y, n = 48; i < (s.length() / 2) * 2; 
        r += s.charAt(i++) == n 
                 ? y == n 
                 ? 'A' 
                 : 'G' 
                 : y == n 
                 ? 'C' 
                 : 'T')
        y = s.charAt(i++);
    return r;


  • Input is an array of chars (which should count as a form of String), parameter is of type int[] because thats one byte saved over char[].


Input:  codegolf

Input:  .
Output: GTG

Input:  }
Output: TTGG

Input:  wow

Input:  programming puzzles

MATL, 21 bytes


Try it online!


'CGTA'   % Push string to be indexed into
j        % Take input string
o        % Convert each char to its ASCII code
V        % Convert to string (*). Numbers are separated by spaces
4Y2      % Push the string '0123456789'
H        % Push number 2
Za       % Convert string (*) from base '0123456789' to base 2, ignoring spaces
2e       % Reshape into a 2-column matrix, padding with a trailing 0 if needed
!        % Transpose
XB       % Convert from binary to decimal
)        % Index into string with the DNA letters. Indexing is 1-based and modular

Pyth, 23 bytes


Try it online!


Borrowing the trick from Dennis' Jelly answer.

                   CMQ   convert each character to its byte value
                sjk      convert to a string and then to integer
              .B         convert to binary
             c        2  chop into pairs
 m         d             for each pair:
          _                  reverse it
         i  2                convert from binary to integer
  @"AGCT"                    find its position in "AGCT"
s                        join the string

Groovy, 114 bytes

{s->'ACGT'[(new BigInteger(((Byte[])s).join())*2).toString(2).toList().collate(2)*.with{0.parseInt(it.join(),2)}]}


    'ACGT'[ //access character from string
        (new BigInteger( //create Big Integer from string
           ((Byte[])s).join() //split string to bytes and then join to string
        ) * 2) //multiply by 2 to add 0 at the end in binary
        .toString(2) //change to binary string
        .toList() //split to characters
        .collate(2) //group characters by two
            0.parseInt(it.join(),2) //join every group and parse to decimal
  • \$\begingroup\$ Great answer! Can you please add an explanation please? \$\endgroup\$ – NoOneIsHere May 3 '16 at 17:46
  • \$\begingroup\$ First version was not working, because I forgot to append 0. I fixed it, and went down with bytes btw. \$\endgroup\$ – Krzysztof Atłasik May 3 '16 at 19:08

Julia 0.4, 77 bytes


This anonymous function takes a character array as input and returns a string.

Try it online!


Python 2.7, 135 bytes

def f(A):g=''.join;B=bin(int(g(map(str,map(ord,A)))))[2:];B+=len(B)%2*'0';return g('ACGT'[int(B[i:i+2],2)] for i in range(len(B))[::2])


def f(A):
    g = ''.join
    B = bin(int(g(map(str,map(ord,A)))))[2:] # convert string input to binary
    B += len(B)%2 * '0' # add extra 0 if necessary
    return g('ACGT'[int(B[i:i+2],2)] for i in range(len(B))[::2]) # map every two characters into 'ACGT'


  • \$\begingroup\$ @DrGreenEggsandHamDJ I have the g(...) function in there twice, so I believe replacing it with the join would add 2 bytes? \$\endgroup\$ – deustice May 3 '16 at 19:55
  • \$\begingroup\$ Ah, I missed that. My bad! \$\endgroup\$ – James May 3 '16 at 19:56

Javascript ES7, 105 103 bytes

s=>((+[for(c of s)c.charCodeAt()].join``).toString(2)+'0').match(/../g).map(x=>"ACGT"['0b'+x-0]).join``

The ES7 part is the for(c of s) part.

ES6 version, 107 105 bytes


Ungolfed code

dna = (str)=>{
  var codes = +[for(c of str)c.charCodeAt()].join``;
  var binaries = (codes.toString(2)+'0').match(/../g);
  return binaries.map(x=>"ACGT"['0b'+x-0]).join``

This is my first try at golfing on PPCG, feel free to correct me if something's wrong.

Thanks @AlexA for the small improvement.

  • 1
    \$\begingroup\$ This is a nice first golf! Since the function isn't recursive and we don't require functions to be named, so you should be able to remove f=, saving 2 bytes. :) \$\endgroup\$ – Alex A. May 15 '16 at 21:16

J, 52 bytes

 3 :'''ACGT''{~#._2,\#:".,&''x''":(,&:(":"0))/3&u:y'

Usage: 3 :'''ACGT''{~#._2,\#:".,&''x''":(,&:(":"0))/3&u:y' 'codegolf' ==> GGCTTGCGGCCGGAGACGCGGTCTGACGCCTTGTAAATA


Common Lisp (Lispworks), 415 bytes

(defun f(s)(labels((p(e f)(concatenate'string e f)))(let((b"")(d""))(dotimes(i(length s))(setf b(p b(write-to-string(char-int(elt s i))))))(setf b(write-to-string(parse-integer b):base 2))(if(oddp #1=(length b))(setf b(p b"0")))(do((j 0(+ j 2)))((= j #1#)d)(let((c(subseq b j(+ j 2))))(cond((#2=string="00"c)(setf d(p d"A")))((#2#"01"c)(setf d(p d"C")))((#2#"10"c)(setf d(p d"G")))((#2#"11"c)(setf d(p d"T")))))))))


(defun f (s)
  (labels ((p (e f)
             (concatenate 'string e f)))
  (let ((b "") (d ""))
    (dotimes (i (length s))
      (setf b
            (p b
                (char-int (elt s i))))))
    (setf b (write-to-string (parse-integer b) :base 2))
    (if (oddp #1=(length b))
        (setf b (p b "0")))
      (do ((j 0 (+ j 2)))
          ((= j #1#) d)
        (let ((c (subseq b j (+ j 2))))
          (cond ((#2=string=  "00" c)
                 (setf d (p d "A")))
                ((#2# "01" c)
                 (setf d (p d "C")))
                ((#2# "10" c)
                 (setf d (p d "G")))
                ((#2# "11" c)
                 (setf d (p d "T")))))))))


CL-USER 2060 > (f "}")

CL-USER 2061 > (f "golf")

Perl, 155 148 137 + 1 (-p flag) = 138 bytes

#!perl -p
s/./ord$&/sge;while($_){/.$/;$s=$&%2 .$s;$t=$v="";$t.=$v+$_/2|0,$v=$_%2*5
for/./g;s/^0// if$_=$t}$_=$s;s/(.)(.)?/([A,C],[G,T])[$1][$2]/ge

Test it on Ideone.


Perl 6, 57 + 1 (-p flag) = 58 bytes

$_=(+[~] .ords).base(2);s:g/..?/{<A G C T>[:2($/.flip)]}/

Step by step explanation:

-p flag causes Perl 6 interpreter to run code line by line, put current line $_, and at end put it back from $_.

.ords - If there is nothing before a period, a method is called on $_. ords method returns list of codepoints in a string.

[~] - [] is a reduction operator, which stores its reduction operator between brackets. In this case, it's ~, which is a string concatenation operator. For example, [~] 1, 2, 3 is equivalent to 1 ~ 2 ~ 3.

+ converts its argument to a number, needed because base method is only defined for integers.

.base(2) - converts an integer to a string in base 2

$_= - assigns the result to $_.

s:g/..?/{...}/ - this is a regular expression replacing any (:g, global mode) instance of regex ..? (one or two characters). The second argument is a replacement pattern, which in this case in code (in Perl 6, curly brackets in strings and replacement patterns are executed as code).

$/ - a regex match variable

.flip - inverts a string. It implicitly converts $/ (a regex match object) to a string. This is because a single character 1 should be expanded to 10, as opposed to 01. Because of that flip, order of elements in array has G and C reversed.

:2(...) - parses a base-2 string into an integer.

<A G C T> - array of four elements.

...[...] - array access operator.

What does that mean? The program gets list of all codepoints in a string, concatenates them together, converts them to base 2. Then, it replaces all instances of two or one character into one of letters A, G, C, T depending on flipped representation of a number in binary.


Hoon, 148 138 bytes

(scan (reel +< |=({a/@ b/tape} (weld <a> b))) dem)
`tape`(flop (turn (rip 1 (mul - +((mod (met 0 -) 2)))) |=(@ (snag +< "ACGT"))))

"abc" is a list of atoms. Interpolate them into strings (<a>) while folding over the list, joining them together into a new string. Parse the number with ++dem to get it back to an atom.

Multiply the number by (bitwise length + 1) % 2 to pad it. Use ++rip to disassemble every two byte pair of the atom into a list, map over the list and use the number as an index into the string "ACGT".

> =a |*
  (scan (reel +< |=({a/@ b/tape} (weld <a> b))) dem)
  `tape`(flop (turn (rip 1 (mul - +((mod (met 0 -) 2)))) |=(@ (snag +< "ACGT"))))
> (a "codegolf")
> (a "ppcg")
> (a "}")

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.