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.
}
which I believe becomesTTGG
. \$\endgroup\$99111100101103111108102
for example is larger than uint-64, so some languages may struggle with bigger conversions. \$\endgroup\$