Skip to content

Commit aaf7fd8

Browse files
jo3zephtjgurwara99
andauthored
Added median.go (TheAlgorithms#522)
* Added Roundoff Simple showcase of Roundoff operation in GoLang feat: add roundoff.go test: add roundoff_test.go docs: added comments on both roundoff.go and roundoff_test.go * Revert "Added Roundoff" This reverts commit c50fa89. * Added median.go feat: added median.go test: added median_test.go docs: added comments on both median.go and median_test.go * Performed suggested fixes and improvements on median.go and median_test.go fix median.go on using switch case, formatting, applied generics fix median_test.go on formatting and using edge case doc median_test.go on comments for test cases * Update to math/median.go Co-authored-by: Taj <[email protected]> * Revert "Update to math/median.go" This reverts commit 7c7c4f5. * Fix median.go Fix: median.go (import local packages and rectify newly affected issues) * Update median.go Fix: median.go removed redundant codes * Fix: median.go and median_test.go Fix: median.go (removed all deemed irrelevant comments) Fix: median.go (removed all deemed irrelevant comments and changed package + fixed newly affected issues) * Update median.go fix: median.go (return values directly) Co-authored-by: Taj <[email protected]>
1 parent 2ad5f9c commit aaf7fd8

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

Diff for: go.sum

Whitespace-only changes.

Diff for: math/median.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// author(s) [jo3zeph](https://github.com/jo3zeph)
2+
// description: Find the median from a set of values
3+
// see median_test.go
4+
5+
package math
6+
7+
import (
8+
"github.com/TheAlgorithms/Go/constraints"
9+
"github.com/TheAlgorithms/Go/sort"
10+
)
11+
12+
func Median[T constraints.Number](values []T) float64 {
13+
14+
sort.Bubble(values)
15+
16+
l := len(values)
17+
18+
switch {
19+
case l == 0:
20+
return 0
21+
22+
case l%2 == 0:
23+
return float64((values[l/2-1] + values[l/2]) / 2)
24+
25+
default:
26+
return float64(values[l/2])
27+
}
28+
}

Diff for: math/median_test.go

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// author(s) [jo3zeph](https://github.com/jo3zeph)
2+
// median_test.go
3+
// see median.go
4+
5+
package math_test
6+
7+
import (
8+
"testing"
9+
10+
"github.com/TheAlgorithms/Go/math"
11+
)
12+
13+
func TestMedian(t *testing.T) {
14+
testCases := []struct {
15+
name string
16+
testValues []float64
17+
answer float64
18+
}{
19+
20+
{
21+
name: "Series of numbers in ascending order",
22+
testValues: []float64{12, 14, 16, 18, 19},
23+
answer: 16,
24+
},
25+
26+
{
27+
name: "Series of numbers in random order",
28+
testValues: []float64{21, 10, 22, 33, 11, 88},
29+
answer: 21.5,
30+
},
31+
32+
{
33+
name: "Series of decimals in random order",
34+
testValues: []float64{11.2, 32.5, 2.5, 37.8, 21.8, 5.2},
35+
answer: 16.5,
36+
},
37+
38+
{
39+
testValues: []float64{},
40+
},
41+
}
42+
43+
for _, test := range testCases {
44+
t.Run(test.name, func(t *testing.T) {
45+
46+
returnedMedian := math.Median(test.testValues)
47+
48+
t.Log(test.testValues, " ", returnedMedian)
49+
50+
if returnedMedian != test.answer {
51+
t.Errorf("Test failed. Median should have been %v but received %v",
52+
test.answer, returnedMedian)
53+
}
54+
})
55+
}
56+
}

0 commit comments

Comments
 (0)