Kevin Silberberg
  • Home
  • Photos
  • Study

Complementing a Strand of DNA

Author

Kevin Silberberg

Published

July 2, 2025

Problem definition

In DNA strings, sysmbols ‘A’ and ‘T’ are complements of each other, as are ‘C’ and ‘G’.

The reverse complement of a DNA string \(s\) is the string \(s^C\) formed by reversing the symbols of \(s\), then taking the complement of each symbol (e.g., the reverse complement of ‘GTCA’ is ‘TGAC’).

Given

A DNA string \(s\) of length at most 1000 base pairs.

Return

The reverse complement \(s^C\) of \(s\).

Sample dataset

AAAACCCGGT

Sample Output

ACCGGGTTTT

Solution

function reverse_complement(s::AbstractString)
    rev_map = Dict{Char, Char}(
        'A' => 'T', 'T' => 'A',
        'G' => 'C', 'C' => 'G'
    )
    io = IOBuffer()
    for char in reverse(s)
        write(io, rev_map[char])
    end
    return String(take!(io))
end

function main()
    first_line = readline(ARGS[1])
    output = reverse_complement(first_line)
    write("output.txt", output)
    println(output)
end

main()

© Copyright 2025 Kevin Silberberg. Except where otherwise noted, all text and images licensed CC-BY-NC 4.0.