aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Manring <michael@manring>2024-04-24 16:35:52 +1000
committerMichael Manring <michael@manring>2024-04-24 16:38:14 +1000
commita91aab38d898f0eb964c8e1c8e1d47ba9ca8b354 (patch)
tree3829d32860e2089c381781322ee806c5c55e22cd
parenta0f54a5b04a049864cc402fc2f85ee62ef040fb4 (diff)
downloadperlweeklychallenge-club-a91aab38d898f0eb964c8e1c8e1d47ba9ca8b354.tar.gz
perlweeklychallenge-club-a91aab38d898f0eb964c8e1c8e1d47ba9ca8b354.tar.bz2
perlweeklychallenge-club-a91aab38d898f0eb964c8e1c8e1d47ba9ca8b354.zip
pwc266 solution in go
-rw-r--r--challenge-266/pokgopun/go/ch-1.go88
-rw-r--r--challenge-266/pokgopun/go/ch-2.go143
2 files changed, 231 insertions, 0 deletions
diff --git a/challenge-266/pokgopun/go/ch-1.go b/challenge-266/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..b5d697fe59
--- /dev/null
+++ b/challenge-266/pokgopun/go/ch-1.go
@@ -0,0 +1,88 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-266/
+/*#
+
+Task 1: Uncommon Words
+
+Submitted by: [41]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given two sentences, $line1 and $line2.
+
+ Write a script to find all uncommmon words in any order in the given
+ two sentences. Return ('') if none found.
+
+ A word is uncommon if it appears exactly once in one of the
+ sentences and doesn’t appear in other sentence.
+
+Example 1
+
+Input: $line1 = 'Mango is sweet'
+ $line2 = 'Mango is sour'
+Output: ('sweet', 'sour')
+
+Example 2
+
+Input: $line1 = 'Mango Mango'
+ $line2 = 'Orange'
+Output: ('Orange')
+
+Example 3
+
+Input: $line1 = 'Mango is Mango'
+ $line2 = 'Orange is Orange'
+Output: ('')
+
+Task 2: X Matrix
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+ "strings"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+func uncommonWords(line1, line2 string) []string {
+ wc := make(map[string]int)
+ var words []string
+ for _, v := range strings.Split(line1+" "+line2, " ") {
+ wc[v]++
+ if wc[v] == 1 {
+ words = append(words, v)
+ }
+ }
+ //fmt.Println("words =>", words, "wc =>", wc)
+ l := len(words)
+ i := l
+ for i > 0 {
+ if wc[words[i-1]] > 1 {
+ copy(words[i-1:], words[i:])
+ l--
+ }
+ i--
+ }
+ if l == 0 {
+ return []string{""}
+ }
+ return words[:l]
+}
+
+func main() {
+ for _, data := range []struct {
+ line1, line2 string
+ output []string
+ }{
+ {"Mango is sweet", "Mango is sour", []string{"sweet", "sour"}},
+ {"Mango Mango", "Orange", []string{"Orange"}},
+ {"Mango is Mango", "Orange is Orange", []string{""}},
+ {"Mango", "Mango", []string{""}},
+ {"Mango", "Orange Orange", []string{"Mango"}},
+ } {
+ //fmt.Println("=>", data, uncommonWords(data.line1, data.line2))
+ io.WriteString(os.Stdout, cmp.Diff(uncommonWords(data.line1, data.line2), data.output)) // blank if ok, otherwise show the difference
+ }
+}
diff --git a/challenge-266/pokgopun/go/ch-2.go b/challenge-266/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..cee52da9ef
--- /dev/null
+++ b/challenge-266/pokgopun/go/ch-2.go
@@ -0,0 +1,143 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-266/
+/*#
+
+Task 2: X Matrix
+
+Submitted by: [42]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a square matrix, $matrix.
+
+ Write a script to find if the given matrix is X Matrix.
+
+ A square matrix is an X Matrix if all the elements on the main
+ diagonal and antidiagonal are non-zero and everything else are zero.
+
+Example 1
+
+Input: $matrix = [ [1, 0, 0, 2],
+ [0, 3, 4, 0],
+ [0, 5, 6, 0],
+ [7, 0, 0, 1],
+ ]
+Output: true
+
+Example 2
+
+Input: $matrix = [ [1, 2, 3],
+ [4, 5, 6],
+ [7, 8, 9],
+ ]
+Output: false
+
+Example 3
+
+Input: $matrix = [ [1, 0, 2],
+ [0, 3, 0],
+ [4, 0, 5],
+ ]
+Output: true
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 28th April
+ 2024.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "errors"
+ "io"
+ "log"
+ "os"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+type row []int
+
+type matrix []row
+
+func (mtx matrix) isSquare() bool {
+ l := len(mtx)
+ i := l
+ for i > 0 {
+ i--
+ if len(mtx[i]) != l {
+ return false
+ }
+ }
+ return true
+}
+
+type squareMatrix struct {
+ l int
+ m matrix
+}
+
+func newSquareMatrix(mtx matrix) (squareMatrix, error) {
+ if mtx.isSquare() {
+ return squareMatrix{len(mtx), mtx}, nil
+ }
+ return squareMatrix{}, errors.New("not square")
+}
+
+func (sm squareMatrix) onX(r, c int) bool {
+ if r == c || r == sm.l-c-1 || c == sm.l-r-1 {
+ return true
+ }
+ return false
+}
+
+func (sm squareMatrix) isX() bool {
+ var v int
+ for r := 0; r < sm.l; r++ {
+ for c := 0; c < sm.l; c++ {
+ v = sm.m[r][c]
+ if sm.onX(r, c) {
+ if v == 0 {
+ return false
+ }
+ } else {
+ if v != 0 {
+ return false
+ }
+ }
+ }
+ }
+ return true
+}
+
+func main() {
+ for _, data := range []struct {
+ input matrix
+ output bool
+ }{
+ {matrix{
+ row{1, 0, 0, 2},
+ row{0, 3, 4, 0},
+ row{0, 5, 6, 0},
+ row{7, 0, 0, 1},
+ }, true},
+ {matrix{
+ row{1, 2, 3},
+ row{4, 5, 6},
+ row{7, 8, 9},
+ }, false},
+ {matrix{
+ row{1, 0, 2},
+ row{0, 3, 0},
+ row{4, 0, 5},
+ }, true},
+ } {
+ sm, err := newSquareMatrix(data.input)
+ if err != nil {
+ log.Fatal(err)
+ }
+ io.WriteString(os.Stdout, cmp.Diff(sm.isX(), data.output)) // blank if ok, otherwise show the difference
+ }
+}