Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
254 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package stats | ||
|
||
import "math" | ||
|
||
// RemoveDuplicates removes duplicate numbers | ||
// in a slice which also means it removes NaN | ||
func RemoveDuplicates(input []float64) []float64 { | ||
seen := make(map[float64]bool) | ||
pos := 0 | ||
for _, f := range input { | ||
if math.IsNaN(f) { | ||
continue | ||
} | ||
if _, ok := seen[f]; !ok { | ||
seen[f] = true | ||
input[pos] = f | ||
pos++ | ||
} | ||
} | ||
input = input[:pos] | ||
return input | ||
} | ||
|
||
// FindUniques finds any unique numbers in a | ||
// slice which are not repeated while ignoring | ||
// NaN since they are always unique | ||
func FindUniques(input []float64) []float64 { | ||
size := len(input) | ||
freq := []int{} | ||
for i := 0; i < size; i++ { | ||
freq = append(freq, -1) | ||
} | ||
output := []float64{} | ||
for i := 0; i < size; i++ { | ||
if math.IsNaN(input[i]) { | ||
continue | ||
} | ||
count := 1 | ||
for j := i + 1; j < size; j++ { | ||
if input[i] == input[j] { | ||
count++ | ||
freq[j] = 0 | ||
} | ||
} | ||
if freq[i] != 0 { | ||
freq[i] = count | ||
} | ||
} | ||
for i := 0; i < size; i++ { | ||
if freq[i] == 1 { | ||
output = append(output, input[i]) | ||
} | ||
} | ||
return output | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package stats | ||
|
||
import ( | ||
"math" | ||
"testing" | ||
) | ||
|
||
func equalSlice(a, b []float64) bool { | ||
if len(a) != len(b) { | ||
return false | ||
} | ||
for i, v := range a { | ||
if v != b[i] && !(math.IsNaN(v) && math.IsNaN(b[i])) { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
func TestRemoveDuplicates(t *testing.T) { | ||
var testCases = []struct { | ||
in []float64 | ||
out []float64 | ||
}{ | ||
{ | ||
[]float64{-4, 2, -4, -2, 2, 4, 4}, | ||
[]float64{-4, 2, -2, 4}, | ||
}, | ||
{ | ||
[]float64{59, math.Inf(-1), -959.3, math.NaN(), -784, 59, 74.3, math.NaN(), 38.2, math.Inf(-1), 905, math.Inf(1)}, | ||
[]float64{59, math.Inf(-1), -959.3, -784, 74.3, 38.2, 905, math.Inf(1)}, | ||
}, | ||
} | ||
for _, tc := range testCases { | ||
output := RemoveDuplicates(tc.in) | ||
if !equalSlice(output, tc.out) { | ||
t.Errorf("got %v, want %v", output, tc.out) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkRemoveDuplicatesSmallFloatSlice(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
RemoveDuplicates(makeFloatSlice(5)) | ||
} | ||
} | ||
|
||
func BenchmarkRemoveDuplicatesLargeFloatSlice(b *testing.B) { | ||
lf := makeFloatSlice(100000) | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
RemoveDuplicates(lf) | ||
} | ||
} | ||
|
||
func TestFindUniques(t *testing.T) { | ||
var testCases = []struct { | ||
in []float64 | ||
out []float64 | ||
}{ | ||
{ | ||
[]float64{-4, -42, 2, math.Inf(-1), -4, -2, 2, math.Inf(1), 4, math.Inf(1), 4, 5}, | ||
[]float64{-42, math.Inf(-1), -2, 5}, | ||
}, | ||
{ | ||
[]float64{59, math.Inf(-1), math.NaN(), -959.7485, -784, 59, 74.3, 238.2, math.Inf(-1), 905, math.Inf(1)}, | ||
[]float64{-959.7485, -784, 74.3, 238.2, 905, math.Inf(1)}, | ||
}, | ||
{ | ||
[]float64{1, math.NaN(), 2, 3, 4, 1, 2, 3, math.NaN()}, | ||
[]float64{4}, | ||
}, | ||
{ | ||
[]float64{1, math.NaN(), 2, 3, 4, 1, 2, 3}, | ||
[]float64{4}, | ||
}, | ||
} | ||
for _, tc := range testCases { | ||
output := FindUniques(tc.in) | ||
if !equalSlice(output, tc.out) { | ||
t.Errorf("got %v, want %v", output, tc.out) | ||
} | ||
} | ||
} | ||
func BenchmarkFindUniquesSmallFloatSlice(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
FindUniques(makeFloatSlice(5)) | ||
} | ||
} | ||
|
||
func BenchmarkFindUniquesLargeFloatSlice(b *testing.B) { | ||
lf := makeFloatSlice(100000) | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
FindUniques(lf) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters