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 adb44b8

Browse files
author
Openset
committed
Add: Flipping an Image
1 parent 3190c41 commit adb44b8

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
11
package flipping_an_image
2+
3+
func flipAndInvertImage(A [][]int) [][]int {
4+
for _, row := range A {
5+
for i, j := 0, len(row)-1; i <= j; i, j = i+1, j-1 {
6+
row[i], row[j] = row[j]^1, row[i]^1
7+
}
8+
}
9+
return A
10+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,48 @@
11
package flipping_an_image
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
type caseType struct {
9+
input [][]int
10+
expected [][]int
11+
}
12+
13+
func TestFlipAndInvertImage(t *testing.T) {
14+
tests := [...]caseType{
15+
{
16+
input: [][]int{
17+
{1, 1, 0},
18+
{1, 0, 1},
19+
{0, 0, 0},
20+
},
21+
expected: [][]int{
22+
{1, 0, 0},
23+
{0, 1, 0},
24+
{1, 1, 1},
25+
},
26+
},
27+
{
28+
input: [][]int{
29+
{1, 1, 0, 0},
30+
{1, 0, 0, 1},
31+
{0, 1, 1, 1},
32+
{1, 0, 1, 0},
33+
},
34+
expected: [][]int{
35+
{1, 1, 0, 0},
36+
{0, 1, 1, 0},
37+
{0, 0, 0, 1},
38+
{1, 0, 1, 0},
39+
},
40+
},
41+
}
42+
for _, tc := range tests {
43+
output := flipAndInvertImage(tc.input)
44+
if !reflect.DeepEqual(output, tc.expected) {
45+
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
46+
}
47+
}
48+
}

0 commit comments

Comments
(0)

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