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.2.1.56 tracks/go/exercises/nucleotide-count/example.go
trackler-2.2.1.55 tracks/go/exercises/nucleotide-count/example.go
trackler-2.2.1.54 tracks/go/exercises/nucleotide-count/example.go
trackler-2.2.1.53 tracks/go/exercises/nucleotide-count/example.go
trackler-2.2.1.52 tracks/go/exercises/nucleotide-count/example.go
trackler-2.2.1.51 tracks/go/exercises/nucleotide-count/example.go
trackler-2.2.1.50 tracks/go/exercises/nucleotide-count/example.go
trackler-2.2.1.49 tracks/go/exercises/nucleotide-count/example.go
trackler-2.2.1.48 tracks/go/exercises/nucleotide-count/example.go
trackler-2.2.1.47 tracks/go/exercises/nucleotide-count/example.go
trackler-2.2.1.46 tracks/go/exercises/nucleotide-count/example.go
trackler-2.2.1.45 tracks/go/exercises/nucleotide-count/example.go
trackler-2.2.1.44 tracks/go/exercises/nucleotide-count/example.go
trackler-2.2.1.43 tracks/go/exercises/nucleotide-count/example.go
trackler-2.2.1.42 tracks/go/exercises/nucleotide-count/example.go
trackler-2.2.1.41 tracks/go/exercises/nucleotide-count/example.go
trackler-2.2.1.40 tracks/go/exercises/nucleotide-count/example.go
trackler-2.2.1.39 tracks/go/exercises/nucleotide-count/example.go
trackler-2.2.1.38 tracks/go/exercises/nucleotide-count/example.go
trackler-2.2.1.37 tracks/go/exercises/nucleotide-count/example.go