🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of JohnTheBastard

JohnTheBastard's solution

to RNA Transcription in the Swift Track

Published at Sep 05 2020 · 0 comments
Instructions
Test suite
Solution

Given a DNA strand, return its RNA complement (per RNA transcription).

Both DNA and RNA strands are a sequence of nucleotides.

The four nucleotides found in DNA are adenine (A), cytosine (C), guanine (G) and thymine (T).

The four nucleotides found in RNA are adenine (A), cytosine (C), guanine (G) and uracil (U).

Given a DNA strand, its transcribed RNA strand is formed by replacing each nucleotide with its complement:

  • G -> C
  • C -> G
  • T -> A
  • A -> U

Setup

Go through the project setup instructions for Xcode using Swift:

http://exercism.io/languages/swift
http://exercism.io/languages/swift/tests

Notably from the source directory:

swift test runs tests
swift package generate-xcodeproj creates an Xcode project

Source

Hyperphysics http://hyperphysics.phy-astr.gsu.edu/hbase/Organic/transcription.html

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

LinuxMain.swift

import XCTest
@testable import RnaTranscriptionTests

XCTMain([
    testCase(RnaTranscriptionTests.allTests),
    ])

RnaTranscriptionTests.swift

import XCTest
@testable import RnaTranscription

class RnaTranscriptionTests: XCTestCase {
    func testRnaComplementOfCytosineIsGuanine() {
        XCTAssertEqual("G", try Nucleotide("C").complementOfDNA())
    }

    func testRnaComplementOfGuanineIsCytosine() {
        XCTAssertEqual("C", try Nucleotide("G").complementOfDNA())
    }

    func testRnaComplementOfThymineIsAdenine() {
        XCTAssertEqual("A", try Nucleotide("T").complementOfDNA())
    }

    func testRnaComplementOfAdenineIsUracil() {
        XCTAssertEqual("U", try Nucleotide("A").complementOfDNA())
    }

    func testRnaComplement() {
        XCTAssertEqual("UGCACCAGAAUU", try Nucleotide("ACGTGGTCTTAA").complementOfDNA())
    }

    func testInvalidRnaComplementOfUracil() {
        XCTAssertThrowsError(try Nucleotide("U").complementOfDNA()) { (error) in
            if case let RnaTranscription.TranscriptionError.invalidNucleotide(message) = error {
                XCTAssertTrue(message == "U is not a valid Nucleotide")
            } else {
                XCTFail("Expected error not thrown")
            }
        }
    }

    func testInvalidRnaComplementOfXXX() {
        XCTAssertThrowsError(try Nucleotide("XXX").complementOfDNA()) { (error) in
            if case let RnaTranscription.TranscriptionError.invalidNucleotide(message) = error {
                XCTAssertTrue(message == "X is not a valid Nucleotide")
            } else {
                XCTFail("Expected error not thrown")
            }
        }
    }

    func testInvalidRnaComplementOfACGTXXXCTTAA() {
        XCTAssertThrowsError(try Nucleotide("ACGTXXXCTTAA").complementOfDNA()) { error in
            if case let RnaTranscription.TranscriptionError.invalidNucleotide(message) = error {
                XCTAssertTrue(message == "X is not a valid Nucleotide")
            } else {
                XCTFail("Expected error not thrown")
            }
        }
    }

    static var allTests: [(String, (RnaTranscriptionTests) -> () throws -> Void)] {
        return [
            ("testRnaComplementOfCytosineIsGuanine", testRnaComplementOfCytosineIsGuanine),
            ("testRnaComplementOfGuanineIsCytosine", testRnaComplementOfGuanineIsCytosine),
            ("testRnaComplementOfThymineIsAdenine", testRnaComplementOfThymineIsAdenine),
            ("testRnaComplementOfAdenineIsUracil", testRnaComplementOfAdenineIsUracil),
            ("testRnaComplement", testRnaComplement),
            ("testInvalidRnaComplementOfUracil", testInvalidRnaComplementOfUracil),
            ("testInvalidRnaComplementOfXXX", testInvalidRnaComplementOfXXX),
            ("testInvalidRnaComplementOfACGTXXXCTTAA", testInvalidRnaComplementOfACGTXXXCTTAA)
        ]
    }
}
// John Hearn
// September 5, 2020

import Foundation

// I dislike the nomenclature imposed by the test suite. This struct represents
// an arbitrary number of nucleotides. NucleotideSequence would be better.
struct Nucleotide {
    private let dnaStrand: [DNA]
    private let rnaStrand: [RNA]
    // rnaStrand should be an optional or lazy property, but the test suite does
    // not allow complementOfDNA to be a mutating function.

    init(_ dna: String) throws {
        dnaStrand = try dna.map { try DNA($0) }
        rnaStrand = dnaStrand.map { $0.rnaComplement }
    }

    func complementOfDNA() -> String {
        // [Character] to String is free
        return String(self.rnaStrand.map { $0.rawValue })
    }

    enum RNA: Character {
        case adenine  = "A"
        case cytosine = "C"
        case guanine  = "G"
        case uracil   = "U"
    }

    enum DNA: Character {
        case adenine  = "A"
        case cytosine = "C"
        case guanine  = "G"
        case thymine  = "T"

        init(_ c: Character) throws {
            switch c {
            case "A": self = .adenine
            case "C": self = .cytosine
            case "G": self = .guanine
            case "T": self = .thymine
            default: throw TranscriptionError.invalidNucleotide("\(c) is not a valid Nucleotide")
            }
        }

        var rnaComplement: RNA {
            let _rnaComplement: RNA
            switch self {
            case .adenine:  _rnaComplement = .uracil
            case .cytosine: _rnaComplement = .guanine
            case .guanine:  _rnaComplement = .cytosine
            case .thymine:   _rnaComplement = .adenine
            }
            return _rnaComplement
        }
    }
}

enum TranscriptionError: Error {
    case invalidNucleotide(String)
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?