Sha256: 9afc41f4fc2f023f3ffd03b40550d7ef1da8aded7a264886517c5e0d7896951b

Contents?: true

Size: 1 KB

Versions: 262

Compression:

Stored size: 1 KB

Contents

package dna

import (
	"errors"
	"strings"
)

const testVersion = 2

// Histogram is a mapping from nucleotide to its count in given DNA
type Histogram map[byte]int

// DNA is a list of nucleotides
type DNA string

const validNucleotides = "ACGT"

// Count counts number of occurrences of given nucleotide in given DNA
func (dna DNA) Count(nucleotide byte) (count int, err error) {
	if !strings.Contains(validNucleotides, string(nucleotide)) {
		return 0, errors.New("dna: invalid nucleotide " + string(nucleotide))
	}

	return strings.Count(string(dna), string(nucleotide)), nil
}

// Counts generates a histogram of valid nucleotides in given DNA.
// Returns error if DNA contains invalid nucleotide.
func (dna DNA) Counts() (Histogram, error) {
	var total int
	h := Histogram{}
	for i := range validNucleotides {
		nucleotide := validNucleotides[i]
		h[nucleotide], _ = dna.Count(nucleotide)
		total += h[nucleotide]
	}
	if total != len(dna) {
		return nil, errors.New("dna: contains invalid nucleotide")
	}
	return h, nil
}

Version data entries

262 entries across 262 versions & 1 rubygems

Version Path
trackler-2.1.0.39 tracks/go/exercises/nucleotide-count/example.go
trackler-2.1.0.38 tracks/go/exercises/nucleotide-count/example.go
trackler-2.1.0.37 tracks/go/exercises/nucleotide-count/example.go
trackler-2.1.0.36 tracks/go/exercises/nucleotide-count/example.go
trackler-2.1.0.34 tracks/go/exercises/nucleotide-count/example.go
trackler-2.1.0.33 tracks/go/exercises/nucleotide-count/example.go
trackler-2.1.0.32 tracks/go/exercises/nucleotide-count/example.go
trackler-2.1.0.31 tracks/go/exercises/nucleotide-count/example.go
trackler-2.1.0.30 tracks/go/exercises/nucleotide-count/example.go
trackler-2.1.0.29 tracks/go/exercises/nucleotide-count/example.go
trackler-2.1.0.28 tracks/go/exercises/nucleotide-count/example.go
trackler-2.1.0.27 tracks/go/exercises/nucleotide-count/example.go
trackler-2.1.0.26 tracks/go/exercises/nucleotide-count/example.go
trackler-2.1.0.25 tracks/go/exercises/nucleotide-count/example.go
trackler-2.1.0.24 tracks/go/exercises/nucleotide-count/example.go
trackler-2.1.0.23 tracks/go/exercises/nucleotide-count/example.go
trackler-2.1.0.22 tracks/go/exercises/nucleotide-count/example.go
trackler-2.1.0.21 tracks/go/exercises/nucleotide-count/example.go
trackler-2.1.0.20 tracks/go/exercises/nucleotide-count/example.go
trackler-2.1.0.19 tracks/go/exercises/nucleotide-count/example.go