diff options
| author | Michael Manring <michael@manring> | 2023-12-19 05:39:36 +1100 |
|---|---|---|
| committer | Michael Manring <michael@manring> | 2023-12-19 05:39:36 +1100 |
| commit | 53151c3c22e92081a044e36fcf27f9a87feaa541 (patch) | |
| tree | a0c42d4fa8c2118a2b095388070a5e3b92de8e60 | |
| parent | 6d2834f8e078f16934ff9cb34bf8cc5d2d0f6189 (diff) | |
| download | perlweeklychallenge-club-53151c3c22e92081a044e36fcf27f9a87feaa541.tar.gz perlweeklychallenge-club-53151c3c22e92081a044e36fcf27f9a87feaa541.tar.bz2 perlweeklychallenge-club-53151c3c22e92081a044e36fcf27f9a87feaa541.zip | |
pwc248 solution in go
| -rw-r--r-- | challenge-248/pokgopun/go/ch-1.go | 95 | ||||
| -rw-r--r-- | challenge-248/pokgopun/go/ch-2.go | 109 |
2 files changed, 204 insertions, 0 deletions
diff --git a/challenge-248/pokgopun/go/ch-1.go b/challenge-248/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..f697f63cf9 --- /dev/null +++ b/challenge-248/pokgopun/go/ch-1.go @@ -0,0 +1,95 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-248/ +/*# + +Task 1: Shortest Distance + +Submitted by: [58]Mohammad S Anwar + __________________________________________________________________ + + You are given a string and a character in the given string. + + Write a script to return an array of integers of size same as length of + the given string such that: +distance[i] is the distance from index i to the closest occurence of +the given character in the given string. + +The distance between two indices i and j is abs(i - j). + +Example 1 + +Input: $str = "loveleetcode", $char = "e" +Output: (3,2,1,0,1,0,0,1,2,2,1,0) + +The character 'e' appears at indices 3, 5, 6, and 11 (0-indexed). +The closest occurrence of 'e' for index 0 is at index 3, so the distance is abs( +0 - 3) = 3. +The closest occurrence of 'e' for index 1 is at index 3, so the distance is abs( +1 - 3) = 2. +For index 4, there is a tie between the 'e' at index 3 and the 'e' at index 5, +but the distance is still the same: abs(4 - 3) == abs(4 - 5) = 1. +The closest occurrence of 'e' for index 8 is at index 6, so the distance is abs( +8 - 6) = 2. + +Example 2 + +Input: $str = "aaab", $char = "b" +Output: (3,2,1,0) + +Task 2: Submatrix Sum +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +type word string + +func (w word) indexes(r rune) []int { + idxs := []int{} + for i, v := range w { + if v == r { + idxs = append(idxs, i) + } + } + return idxs +} + +func (w word) shortestDistance(r rune) []int { + pos := w.indexes(r) + if len(pos) == 0 { + return []int{} + } + l := len(w) + s := make([]int, l) + var d, p int + for l > 0 { + l-- + s[l] = max(l, pos[0]) - min(l, pos[0]) + for _, p = range pos[1:] { + d = max(l, p) - min(l, p) + if d < s[l] { + s[l] = d + } + } + } + return s +} + +func main() { + for _, data := range []struct { + wrd word + lttr rune + dstnce []int + }{ + {"loveleetcode", 'e', []int{3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0}}, + {"aaab", 'b', []int{3, 2, 1, 0}}, + } { + io.WriteString(os.Stdout, cmp.Diff(data.wrd.shortestDistance(data.lttr), data.dstnce)) // output nothing if ok, otherwise show the difference + } +} diff --git a/challenge-248/pokgopun/go/ch-2.go b/challenge-248/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..f626d45750 --- /dev/null +++ b/challenge-248/pokgopun/go/ch-2.go @@ -0,0 +1,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 + } +} |
