aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Manring <michael@manring>2024-05-20 22:11:17 +1000
committerMichael Manring <michael@manring>2024-05-20 22:11:17 +1000
commit8909a2f4d1fa2e2b9d2f52e68ff8283be4985d3e (patch)
treeaf0f525a87024d5a4fe7055b136bcd2cb55d8bd7
parent749da7ab7b6c68de590cec57f44243333f2895d9 (diff)
downloadperlweeklychallenge-club-8909a2f4d1fa2e2b9d2f52e68ff8283be4985d3e.tar.gz
perlweeklychallenge-club-8909a2f4d1fa2e2b9d2f52e68ff8283be4985d3e.tar.bz2
perlweeklychallenge-club-8909a2f4d1fa2e2b9d2f52e68ff8283be4985d3e.zip
pwc270 solution in go
-rw-r--r--challenge-270/pokgopun/go/ch-1.go134
-rw-r--r--challenge-270/pokgopun/go/ch-2.go125
2 files changed, 259 insertions, 0 deletions
diff --git a/challenge-270/pokgopun/go/ch-1.go b/challenge-270/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..b9584673f5
--- /dev/null
+++ b/challenge-270/pokgopun/go/ch-1.go
@@ -0,0 +1,134 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-270/
+/*#
+
+Task 1: Special Positions
+
+Submitted by: [43]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a m x n binary matrix.
+
+ Write a script to return the number of special positions in the given
+ binary matrix.
+
+ A position (i, j) is called special if $matrix[i][j] == 1 and all
+ other elements in the row i and column j are 0.
+
+Example 1
+
+Input: $matrix = [ [1, 0, 0],
+ [0, 0, 1],
+ [1, 0, 0],
+ ]
+Output: 1
+
+There is only special position (1, 2) as $matrix[1][2] == 1
+and all other elements in row 1 and column 2 are 0.
+
+Example 2
+
+Input: $matrix = [ [1, 0, 0],
+ [0, 1, 0],
+ [0, 0, 1],
+ ]
+Output: 3
+
+Special positions are (0,0), (1, 1) and (2,2).
+
+Task 2: Distribute Elements
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+type row []int
+
+func (rw row) hasOneAndZeroes() bool {
+ var cnt1, cntNot01 int
+ for _, v := range rw {
+ if v == 1 {
+ cnt1++
+ if cnt1 > 1 {
+ return false
+ }
+ } else if v != 0 {
+ cntNot01++
+ if cntNot01 > 0 {
+ return false
+ }
+ }
+ }
+ if cnt1 == 0 {
+ return false
+ }
+ return true
+}
+
+type matrix []row
+
+func (mtx matrix) cross(r, c int) [2]row {
+ h := mtx[r]
+ l := len(mtx)
+ v := make(row, l)
+ for i := range l {
+ v[i] = mtx[i][c]
+ }
+ return [2]row{h, v}
+}
+
+func (mtx matrix) isSP(r, c int) bool {
+ if mtx[r][c] != 1 {
+ return false
+ }
+ for _, v := range mtx.cross(r, c) {
+ if !v.hasOneAndZeroes() {
+ return false
+ }
+ }
+ return true
+}
+
+func (mtx matrix) countSP() int {
+ count := 0
+ m := len(mtx)
+ n := mtx[0]
+ for r := range m {
+ for c := range n {
+ if mtx.isSP(r, c) {
+ count++
+ }
+ }
+ }
+ return count
+}
+
+func main() {
+ for _, data := range []struct {
+ input matrix
+ output int
+ }{
+ {
+ matrix{
+ row{1, 0, 0},
+ row{0, 0, 1},
+ row{1, 0, 0},
+ }, 1,
+ },
+ {
+ matrix{
+ row{1, 0, 0},
+ row{0, 1, 0},
+ row{0, 0, 1},
+ }, 3,
+ },
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(data.input.countSP(), data.output)) // blank if ok, otherwise show the difference
+ }
+}
diff --git a/challenge-270/pokgopun/go/ch-2.go b/challenge-270/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..fb096121d2
--- /dev/null
+++ b/challenge-270/pokgopun/go/ch-2.go
@@ -0,0 +1,125 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-270/
+/*#
+
+Task 2: Distribute Elements
+
+Submitted by: [44]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are give an array of integers, @ints and two integers, $x and $y.
+
+ Write a script to execute one of the two options:
+Level 1:
+Pick an index i of the given array and do $ints[i] += 1
+
+Level 2:
+Pick two different indices i,j and do $ints[i] +=1 and $ints[j] += 1.
+
+ You are allowed to perform as many levels as you want to make every
+ elements in the given array equal. There is cost attach for each level,
+ for Level 1, the cost is $x and $y for Level 2.
+
+ In the end return the minimum cost to get the work done.
+
+Example 1
+
+Input: @ints = (4, 1), $x = 3 and $y = 2
+Output: 9
+
+Level 1: i=1, so $ints[1] += 1.
+@ints = (4, 2)
+
+Level 1: i=1, so $ints[1] += 1.
+@ints = (4, 3)
+
+Level 1: i=1, so $ints[1] += 1.
+@ints = (4, 4)
+
+We perforned operation Level 1, 3 times.
+So the total cost would be 3 x $x => 3 x 3 => 9
+
+Example 2
+
+Input: @ints = (2, 3, 3, 3, 5), $x = 2 and $y = 1
+Output: 6
+
+Level 2: i=0, j=1, so $ints[0] += 1 and $ints[1] += 1
+@ints = (3, 4, 3, 3, 5)
+
+Level 2: i=0, j=2, so $ints[0] += 1 and $ints[2] += 1
+@ints = (4, 4, 4, 3, 5)
+
+Level 2: i=0, j=3, so $ints[0] += 1 and $ints[3] += 1
+@ints = (5, 4, 4, 4, 5)
+
+Level 2: i=1, j=2, so $ints[1] += 1 and $ints[2] += 1
+@ints = (5, 5, 5, 4, 5)
+
+Level 1: i=3, so $ints[3] += 1
+@ints = (5, 5, 5, 5, 5)
+
+We perforned operation Level 1, 1 time and Level 2, 4 times.
+So the total cost would be (1 x $x) + (3 x $y) => (1 x 2) + (4 x 1) => 6
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 26th May 2024.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+ "slices"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+func distElem(ints []int, x, y int) int {
+ if len(ints) < 2 {
+ return 0
+ }
+ slices.Sort(ints)
+ l := len(ints)
+ mx := ints[l-1]
+ c := 0
+ for {
+ ints = ints[:slices.Index(ints, mx)]
+ l = len(ints)
+ if l == 0 {
+ break
+ }
+ if l == 1 || 2*x < y {
+ for ints[l-1] < mx {
+ for i := range l {
+ ints[i]++
+ c += x
+ }
+ }
+ } else {
+ for ints[l-1] < mx {
+ for i := range 2 {
+ ints[l-1-i]++
+ }
+ c += y
+ }
+ }
+ }
+ return c
+}
+
+func main() {
+ for _, data := range []struct {
+ ints []int
+ x, y, cost int
+ }{
+ {[]int{4, 1}, 3, 2, 9},
+ {[]int{2, 3, 3, 3, 5}, 2, 1, 6},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(distElem(data.ints, data.x, data.y), data.cost)) // blank if ok, otherwise show the difference
+ }
+}