diff --git a/Counter.go b/Counter.go new file mode 100644 index 0000000..fca59bd --- /dev/null +++ b/Counter.go @@ -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 + +} diff --git a/counter_test.go b/counter_test.go new file mode 100644 index 0000000..b31df48 --- /dev/null +++ b/counter_test.go @@ -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) + } + } +}