/
ch-2.go
109 lines (90 loc) · 2.04 KB
/
ch-2.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//# https://theweeklychallenge.org/blog/perl-weekly-challenge-248/
/*#
Task 2: Submatrix Sum
Submitted by: [59]Jorg Sommrey
__________________________________________________________________
You are given a NxM matrix A of integers.
Write a script to construct a (N-1)x(M-1) matrix B having elements that
are the sum over the 2x2 submatrices of A,
b[i,k] = a[i,k] + a[i,k+1] + a[i+1,k] + a[i+1,k+1]
Example 1
Input: $a = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]
]
Output: $b = [
[14, 18, 22],
[30, 34, 38]
]
Example 2
Input: $a = [
[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]
]
Output: $b = [
[2, 1, 0],
[1, 2, 1],
[0, 1, 2]
]
__________________________________________________________________
Last date to submit the solution 23:59 (UK Time) Sunday 24th December
2023.
__________________________________________________________________
SO WHAT DO YOU THINK ?
#*/
//# solution by pokgopun@gmail.com
package main
import (
"io"
"os"
"github.com/google/go-cmp/cmp"
)
type cols []int
type rows []cols
func (rws rows) sub() rows {
d := len(rws)
l := len(rws[0])
s := make(rows, d-1)
for r := 0; r < d-1; r++ {
s[r] = make(cols, l-1)
for c := 0; c < l-1; c++ {
s[r][c] = rws[r][c] + rws[r][c+1] + rws[r+1][c] + rws[r+1][c+1]
}
}
return s
}
func main() {
for _, data := range []struct {
input, output rows
}{
{
rows{
cols{1, 2, 3, 4},
cols{5, 6, 7, 8},
cols{9, 10, 11, 12},
},
rows{
cols{14, 18, 22},
cols{30, 34, 38},
},
},
{
rows{
cols{1, 0, 0, 0},
cols{0, 1, 0, 0},
cols{0, 0, 1, 0},
cols{0, 0, 0, 1},
},
rows{
cols{2, 1, 0},
cols{1, 2, 1},
cols{0, 1, 2},
},
},
} {
io.WriteString(os.Stdout, cmp.Diff(data.output, data.input.sub())) // output nothing if ok, otherwise ouput difference
}
}