Skip to content

Language Shootout

A user-supported site Fastest, Shortest, Simplest

Reverse Complement

Python CPython #1 — Reverse Complement

Read DNA sequences and write out the reverse complement of each.

Time 1,525.5 ms
CPU time 1,512.6 ms
Peak memory 1,008,684 KB
gz 335 bytes — comments removed, gzipped
Style ★★★★☆
Implementation Python — Python 3 (CPython) 3.13.7
Permitted dependency numpy — pinned in the toolchain image
By sysop-
Submitted September 24, 2026

Style assessment

Uses the canonical Python tools for this task — bytes.maketrans/translate for complementing, [::-1] slice reversal, sys.stdin.detach() and stdout.buffer.write for binary I/O — with default-arg binding of hot names as a recognized micro-optimization. Minor deductions for the redundant `[line for line in sys.stdin]` (b''.join(sys.stdin) or a single read suffices), the non-idiomatic `[header, s] =` bracket unpacking, and terse names like `s`; fixing those would raise it.

# The Computer Language Benchmarks Game

# http://benchmarksgame.alioth.debian.org/

#

# contributed by Jacob Lee, Steven Bethard, et al

# 2to3, fixed by Daniele Varrazzo

# modified by Daniel Nanz


import sys


def show(seq, table=bytes.maketrans(b'ACBDGHKMNSRUTWVYacbdghkmnsrutwvy',
                                    b'TGVHCDMKNSYAAWBRTGVHCDMKNSYAAWBR'),
         write=sys.stdout.buffer.write, nl=b'\n'):
    
    [header, s] = seq.split(nl, 1)
    s = s.translate(table, nl)[: : -1]
    
    write(b'>' + header + nl)
    for i in range(0, len(s), 60):
        write(s[i : i + 60] + nl)



def main():
    
    sys.stdin = sys.stdin.detach()
    seqs = b''.join([line for line in sys.stdin]).split(b'>')[1 : ]
    
    for seq in seqs:
        show(seq)        


main()

Back to Reverse Complement