Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Counter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package statistics

//Counter counter
type Counter struct {
sample []float64
data map[float64]int
}

//NewCounter initializae a Counter
func NewCounter(sample []float64) Counter {
counter := Counter{}
counter.sample = sample
counter.data = map[float64]int{}
make(counter)

return counter
}

func make(counter Counter) {
for _, key := range counter.sample {
if _, ok := counter.data[key]; ok {
counter.data[key]++
} else {
counter.data[key] = 1
}
}
}

//Values Return the Values counted
func (counter Counter) Values() []int {
values := []int{}
for _, value := range counter.data {
values = append(values, value)
}
return values

}
23 changes: 23 additions & 0 deletions counter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package statistics

import (
"reflect"
"testing"
)

func TestValues(t *testing.T) {
cases := []struct {
sample []float64
wanted []int
}{
{[]float64{1.0, 1.0}, []int{2}},
{[]float64{1.0, 2.0}, []int{1, 1}},
{[]float64{}, []int{}},
}
for _, c := range cases {
got := NewCounter(c.sample).Values()
if !reflect.DeepEqual(got, c.wanted) {
t.Errorf("Counter.values(%v) want: %v; got %v", c.sample, c.wanted, got)
}
}
}