-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 1619. Mean of Array After Removing Some Elements (#71)
Signed-off-by: ashing <axingfly@gmail.com>
- Loading branch information
Showing
2 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
leetcode/1619/1619. Mean of Array After Removing Some Elements.go
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 _619 | ||
|
||
import "sort" | ||
|
||
func trimMean(arr []int) float64 { | ||
sort.Ints(arr) | ||
n := len(arr) | ||
sum := 0 | ||
for _, x := range arr[n/20 : 19*n/20] { | ||
sum += x | ||
} | ||
return float64(sum*10) / float64(n*9) | ||
} |
31 changes: 31 additions & 0 deletions
31
leetcode/1619/1619. Mean of Array After Removing Some Elements_test.go
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,31 @@ | ||
package _619 | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func Test_trimMean(t *testing.T) { | ||
type args struct { | ||
arr []int | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want float64 | ||
}{ | ||
{ | ||
name: "one", | ||
args: args{ | ||
[]int{1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3}, | ||
}, | ||
want: 2.00000, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := trimMean(tt.args.arr); got != tt.want { | ||
t.Errorf("trimMean() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |