liveBook · Manning

Nucleotide

As explained in Classic Computer Science Problems in Swift: Essential techniques for practicing programmers

A nucleotide is a molecular unit of DNA, with the DNA nucleotides in this section represented by A, C, G, or T.

A nucleotide is one of four DNA symbols: A, C, G, or T. Three nucleotides form a codon, and multiple codons form a gene. section 2.1

For DNA search, model a nucleotide as a Swift enum with four cases and Comparable conformance. Comparing raw Character values supplies an ordering for codons and genes. section 2.1

Use the nucleotide representation to build larger DNA structures:

  • Define Codon as a tuple of three Nucleotides. section 2.1
  • Define Gene as an array of Codons. section 2.1
  • Convert a DNA string into codons in groups of three, skipping a final incomplete group. section 2.1
  • Encode A, C, G, and T as 00, 01, 10, and 11 when compact storage matters. section 1.2

Searching a gene

Convert a sequence such as ACGTGGCTCTCTAACGTACGTACGTACGGGGTTTATATATACCCTAGGACTCCCTTT into a Gene, then search for the codon (.A, .C, .G). Linear search scans codons sequentially; binary search requires a sorted gene. section 2.1

Compressed storage

A sequence such as ATGAATGCC can be stored as a two-bit-per-nucleotide bit string and decompressed back to its original letters. section 1.2

Figure 2.1. A nucleotide is represented by one of the letters A, C, G, and T. A codon is composed of three nucleotides, and a gene is composed of multiple codons.

Use the diagram to connect individual nucleotides with codons and genes. section 2.1

How Classic Computer Science Problems in Swift: Essential techniques for practicing programmers covers it

How deep it goes
Defines enum, ordering, grouping, and two-bit encoding, but stops short of biological interpretation.
Examples
Uses A, C, G, T sequences, codon tuples, gene arrays, and the ATGAATGCC compression example.
What it assumes
Assumes intermediate Swift knowledge while teaching DNA representation and bit-width reasoning through concrete examples.
What you can do after
Build it from scratch as a Swift enum, group it into genes, search codons, or compress sequences into bits.
How it connects
Connects nucleotides to codons, genes, bit strings, linear search, and binary search.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Classic Computer Science Problems in Swift: Essential techniques for practicing programmers. The section links point to the parts of the book it draws on.