From b1f583939a93886813088ebba1d0e996688437c3 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Fri, 11 Nov 2016 16:43:31 +0100 Subject: Solve Nucleotide Count --- go/nucleotide-count/nucleotide_count.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 go/nucleotide-count/nucleotide_count.go diff --git a/go/nucleotide-count/nucleotide_count.go b/go/nucleotide-count/nucleotide_count.go new file mode 100644 index 0000000..8d2fda6 --- /dev/null +++ b/go/nucleotide-count/nucleotide_count.go @@ -0,0 +1,33 @@ +package dna + +import "errors" + +const testVersion = 2 + +var ErrInvalid = errors.New("invalid") + +type Histogram map[byte]int +type DNA string + +func (d DNA) Count(b byte) (int, error) { + h, err := d.Counts() + if err != nil { + return 0, err + } + if v, ok := h[b]; ok { + return v, nil + } + return 0, ErrInvalid +} + +func (d DNA) Counts() (Histogram, error) { + h := Histogram{'A': 0, 'C': 0, 'G': 0, 'T': 0} + for _, v := range d { + b := byte(v) + if _, ok := h[b]; !ok { + return nil, ErrInvalid + } + h[b]++ + } + return h, nil +} -- cgit v1.2.3