Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9f2d91c

Browse files
author
Sandy
authored
Merge pull request #166 from openset/develop
Add: sort_array_by_parity
2 parents 79cc076 + ba3bcf2 commit 9f2d91c

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
11
package sort_array_by_parity
2+
3+
import "sort"
4+
5+
func sortArrayByParity(A []int) []int {
6+
sort.Slice(A, func(i, j int) bool {
7+
return A[i]%2 < A[j]%2
8+
})
9+
return A
10+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,26 @@
11
package sort_array_by_parity
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
type caseType struct {
9+
input []int
10+
expected []int
11+
}
12+
13+
func TestSortArrayByParity(t *testing.T) {
14+
tests := [...]caseType{
15+
{
16+
input: []int{3, 1, 2, 4},
17+
expected: []int{2, 4, 3, 1},
18+
},
19+
}
20+
for _, tc := range tests {
21+
output := sortArrayByParity(tc.input)
22+
if !reflect.DeepEqual(output, tc.expected) {
23+
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
24+
}
25+
}
26+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /