-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Count(), CountList(), Mode(), Max(), Min(), Median(), Range(),
MidRange()
- Loading branch information
1 parent
0459cd0
commit 7e0ceae
Showing
18 changed files
with
425 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package goterators | ||
|
||
import "constraints" | ||
|
||
// Count returns number of checking item exists in source list | ||
func Count[T constraints.Ordered](source []T, checkedItem T) (result int) { | ||
for _, item := range source { | ||
if item == checkedItem { | ||
result++ | ||
} | ||
} | ||
return result | ||
} |
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,12 @@ | ||
package goterators | ||
|
||
import "constraints" | ||
|
||
// CountList returns sub-list counter of input sub-list that want to count from source list. | ||
func CountList[T constraints.Ordered](source []T, checkedItems []T) []int { | ||
result := make([]int, len(checkedItems)) | ||
for index, item := range checkedItems { | ||
result[index] = Count(source, item) | ||
} | ||
return result | ||
} |
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,27 @@ | ||
package goterators | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestCountList(t *testing.T) { | ||
testSource := []int{1, 1, 1, 2, 2, 3, 3, 3, 3, 4} | ||
|
||
countingItems := []int{1, 1, 2, 3, 5} | ||
expectedValue := []int{3, 3, 2, 4, 0} | ||
actualValue := CountList(testSource, countingItems) | ||
if len(actualValue) != len(expectedValue) { | ||
t.Fatalf("Expected length = %v , got length = %v", len(expectedValue), len(actualValue)) | ||
} | ||
for index := range expectedValue { | ||
if expectedValue[index] != actualValue[index] { | ||
t.Errorf("Expected = %v , got = %v", expectedValue[index], actualValue[index]) | ||
} | ||
} | ||
} | ||
|
||
func ExampleCountList() { | ||
testSource := []int{1, 1, 1, 2, 2, 3, 3, 3, 3, 4} | ||
fmt.Println("CountList: ", CountList(testSource, []int{1, 1, 2, 3, 5})) | ||
} |
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,29 @@ | ||
package goterators | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestCount(t *testing.T) { | ||
testSource := []int{1, 1, 1, 2, 2, 3, 3, 3, 3, 4} | ||
|
||
findingItem := 3 | ||
expectedValue := 4 | ||
actutalValue := Count(testSource, findingItem) | ||
if actutalValue != expectedValue { | ||
t.Errorf("Expected = %v , got = %v", expectedValue, actutalValue) | ||
} | ||
|
||
findingItem = 5 | ||
expectedValue = 0 | ||
actutalValue = Count(testSource, findingItem) | ||
if actutalValue != expectedValue { | ||
t.Errorf("Expected = %v , got = %v", expectedValue, actutalValue) | ||
} | ||
} | ||
|
||
func ExampleCount() { | ||
testSource := []int{1, 1, 1, 2, 2, 3, 3, 3, 3, 4} | ||
fmt.Println("Count: ", Count(testSource, 3)) | ||
} |
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,17 @@ | ||
package goterators | ||
|
||
import "constraints" | ||
|
||
// Max find largest value from source list | ||
func Max[T constraints.Ordered](source []T) (result T, err error) { | ||
if len(source) == 0 { | ||
return result, ErrNotFound | ||
} | ||
result = source[0] | ||
for _, item := range source { | ||
if item > result { | ||
result = item | ||
} | ||
} | ||
return result, nil | ||
} |
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,22 @@ | ||
package goterators | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestMax(t *testing.T) { | ||
testSource := []int{20, 17, 9, 21, 18, 3, 11, 5} | ||
expectedValue := 21 | ||
actutalValue, _ := Max(testSource) | ||
|
||
if actutalValue != expectedValue { | ||
t.Errorf("Expected = %v , got = %v", expectedValue, actutalValue) | ||
} | ||
} | ||
|
||
func ExampleMax() { | ||
testSource := []int{20, 17, 9, 21, 18, 3, 11, 5} | ||
result, _ := Max(testSource) | ||
fmt.Println("Max: ", result) | ||
} |
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,14 @@ | ||
package goterators | ||
|
||
// Median return a value in the middle of an ordered source list. If number of item in source is even, return right item. Make sure source list are sorted | ||
func Median[T any](source []T) (T, int, error) { | ||
if len(source) == 0 { | ||
var result T | ||
return result, 0, ErrNotFound | ||
} | ||
if len(source) == 1 { | ||
return source[0], 0, nil | ||
} | ||
midNumber := len(source) / 2 | ||
return source[midNumber], midNumber, nil | ||
} |
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,36 @@ | ||
package goterators | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestMedian(t *testing.T) { | ||
testSource := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20} | ||
expectedValue := 11 | ||
expectedIndex := 10 | ||
actutalValue, actualIndex, _ := Median(testSource) | ||
if actutalValue != expectedValue { | ||
t.Errorf("Expected = %v , got = %v", expectedValue, actutalValue) | ||
} | ||
if actualIndex != expectedIndex { | ||
t.Errorf("Expected = %v , got = %v", expectedIndex, actualIndex) | ||
} | ||
|
||
testSource = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19} | ||
expectedValue = 10 | ||
expectedIndex = 9 | ||
actutalValue, actualIndex, _ = Median(testSource) | ||
if actutalValue != expectedValue { | ||
t.Errorf("Expected = %v , got = %v", expectedValue, actutalValue) | ||
} | ||
if actualIndex != expectedIndex { | ||
t.Errorf("Expected = %v , got = %v", expectedIndex, actualIndex) | ||
} | ||
} | ||
|
||
func ExampleMedian() { | ||
testSource := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20} | ||
median, index, _ := Median(testSource) | ||
fmt.Println("Median: ", median, ", with index: ", index) | ||
} |
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,11 @@ | ||
package goterators | ||
|
||
// MidRange return (max + min) / 2 | ||
func MidRange[T Number](source []T) float64 { | ||
if len(source) <= 1 { | ||
return 0.0 | ||
} | ||
max, _ := Max(source) | ||
min, _ := Min(source) | ||
return float64(max+min) / 2.0 | ||
} |
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,21 @@ | ||
package goterators | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestMidRange(t *testing.T) { | ||
testSource := []int{20, 17, 9, 21, 18, 3, 11, 5} | ||
expectedValue := 12.0 | ||
actutalValue := MidRange(testSource) | ||
|
||
if actutalValue != expectedValue { | ||
t.Errorf("Expected = %v , got = %v", expectedValue, actutalValue) | ||
} | ||
} | ||
|
||
func ExampleMidRange() { | ||
testSource := []int{20, 17, 9, 21, 18, 3, 11, 5} | ||
fmt.Println("MidRange: ", MidRange(testSource)) | ||
} |
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,17 @@ | ||
package goterators | ||
|
||
import "constraints" | ||
|
||
// Min find smallest value from source list | ||
func Min[T constraints.Ordered](source []T) (result T, err error) { | ||
if len(source) == 0 { | ||
return result, ErrNotFound | ||
} | ||
result = source[0] | ||
for _, item := range source { | ||
if item < result { | ||
result = item | ||
} | ||
} | ||
return result, nil | ||
} |
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,22 @@ | ||
package goterators | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestMin(t *testing.T) { | ||
testSource := []int{20, 17, 9, 21, 18, 3, 11, 5} | ||
expectedValue := 3 | ||
actutalValue, _ := Min(testSource) | ||
|
||
if actutalValue != expectedValue { | ||
t.Errorf("Expected = %v , got = %v", expectedValue, actutalValue) | ||
} | ||
} | ||
|
||
func ExampleMin() { | ||
testSource := []int{20, 17, 9, 21, 18, 3, 11, 5} | ||
result, _ := Min(testSource) | ||
fmt.Println("Min: ", result) | ||
} |
Oops, something went wrong.