aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Manring <michael@manring>2024-02-20 23:17:56 +1100
committerMichael Manring <michael@manring>2024-02-20 23:23:22 +1100
commit1e0e0dbc284712e4db9030a8ff594876cfff856f (patch)
treee423baac15108cd6938ac1dc48f28a2b1dad6431
parentf8ff2b6a656aaff4f179b228ecdcd2c0b655ef94 (diff)
downloadperlweeklychallenge-club-1e0e0dbc284712e4db9030a8ff594876cfff856f.tar.gz
perlweeklychallenge-club-1e0e0dbc284712e4db9030a8ff594876cfff856f.tar.bz2
perlweeklychallenge-club-1e0e0dbc284712e4db9030a8ff594876cfff856f.zip
pwc257 solution in go
-rw-r--r--challenge-257/pokgopun/go/ch-1.go77
-rw-r--r--challenge-257/pokgopun/go/ch-2.go228
2 files changed, 305 insertions, 0 deletions
diff --git a/challenge-257/pokgopun/go/ch-1.go b/challenge-257/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..824821a951
--- /dev/null
+++ b/challenge-257/pokgopun/go/ch-1.go
@@ -0,0 +1,77 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-257/
+/*#
+
+Task 1: Smaller than Current
+
+Submitted by: [44]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a array of integers, @ints.
+
+ Write a script to find out how many integers are smaller than current
+ i.e. foreach ints[i], count ints[j] < ints[i] where i != j.
+
+Example 1
+
+Input: @ints = (5, 2, 1, 6)
+Output: (2, 1, 0, 3)
+
+For $ints[0] = 5, there are two integers (2,1) smaller than 5.
+For $ints[1] = 2, there is one integer (1) smaller than 2.
+For $ints[2] = 1, there is none integer smaller than 1.
+For $ints[3] = 6, there are three integers (5,2,1) smaller than 6.
+
+Example 2
+
+Input: @ints = (1, 2, 0, 3)
+Output: (1, 2, 0, 3)
+
+Example 3
+
+Input: @ints = (0, 1)
+Output: (0, 1)
+
+Example 4
+
+Input: @ints = (9, 4, 9, 2)
+Output: (2, 1, 2, 0)
+
+Task 2: Reduced Row Echelon
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+type ints []int
+
+func (is ints) countSmaller() ints {
+ s := make(ints, len(is))
+ for i, v := range is {
+ for _, e := range is {
+ if e < v {
+ s[i]++
+ }
+ }
+ }
+ return s
+}
+
+func main() {
+ for _, data := range []struct {
+ input, output ints
+ }{
+ {ints{5, 2, 1, 6}, ints{2, 1, 0, 3}},
+ {ints{1, 2, 0, 3}, ints{1, 2, 0, 3}},
+ {ints{0, 1}, ints{0, 1}},
+ {ints{9, 4, 9, 2}, ints{2, 1, 2, 0}},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(data.input.countSmaller(), data.output)) // blank if ok, otherwise show the differences
+ }
+}
diff --git a/challenge-257/pokgopun/go/ch-2.go b/challenge-257/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..5a9363df93
--- /dev/null
+++ b/challenge-257/pokgopun/go/ch-2.go
@@ -0,0 +1,228 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-257/
+/*#
+
+Task 2: Reduced Row Echelon
+
+Submitted by: [45]Ali Moradi
+ __________________________________________________________________
+
+ Given a matrix M, check whether the matrix is in reduced row echelon
+ form.
+
+ A matrix must have the following properties to be in reduced row
+ echelon form:
+1. If a row does not consist entirely of zeros, then the first
+ nonzero number in the row is a 1. We call this the leading 1.
+2. If there are any rows that consist entirely of zeros, then
+ they are grouped together at the bottom of the matrix.
+3. In any two successive rows that do not consist entirely of zeros,
+ the leading 1 in the lower row occurs farther to the right than
+ the leading 1 in the higher row.
+4. Each column that contains a leading 1 has zeros everywhere else
+ in that column.
+
+ For example:
+[
+ [1,0,0,1],
+ [0,1,0,2],
+ [0,0,1,3]
+]
+
+ The above matrix is in reduced row echelon form since the first nonzero
+ number in each row is a 1, leading 1s in each successive row are
+ farther to the right, and above and below each leading 1 there are only
+ zeros.
+
+ For more information check out this wikipedia [46]article.
+
+Example 1
+
+ Input: $M = [
+ [1, 1, 0],
+ [0, 1, 0],
+ [0, 0, 0]
+ ]
+ Output: 0
+
+Example 2
+
+ Input: $M = [
+ [0, 1,-2, 0, 1],
+ [0, 0, 0, 1, 3],
+ [0, 0, 0, 0, 0],
+ [0, 0, 0, 0, 0]
+ ]
+ Output: 1
+
+Example 3
+
+ Input: $M = [
+ [1, 0, 0, 4],
+ [0, 1, 0, 7],
+ [0, 0, 1,-1]
+ ]
+ Output: 1
+
+Example 4
+
+ Input: $M = [
+ [0, 1,-2, 0, 1],
+ [0, 0, 0, 0, 0],
+ [0, 0, 0, 1, 3],
+ [0, 0, 0, 0, 0]
+ ]
+ Output: 0
+
+Example 5
+
+ Input: $M = [
+ [0, 1, 0],
+ [1, 0, 0],
+ [0, 0, 0]
+ ]
+ Output: 0
+
+Example 6
+
+ Input: $M = [
+ [4, 0, 0, 0],
+ [0, 1, 0, 7],
+ [0, 0, 1,-1]
+ ]
+ Output: 0
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 25th February
+ 2024.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+type row []int
+
+func (rw row) isAllZero() bool {
+ for _, v := range rw {
+ if v != 0 {
+ return false
+ }
+ }
+ return true
+}
+
+func (rw row) l1Pos() int {
+ for i, v := range rw {
+ if v == 1 {
+ return i
+ }
+ }
+ return -1
+}
+
+type matrix []row
+
+func (mt matrix) noButtomZeroes() matrix {
+ s := mt
+ l := len(mt)
+ lz := l
+ for l > 0 {
+ l--
+ if mt[l].isAllZero() {
+ s = mt[:l]
+ lz--
+ }
+ if l != lz {
+ break
+ }
+ }
+ return s
+}
+
+func (mt matrix) isSingleNonZeroColumn(col int) bool {
+ c := 0
+ for _, v := range mt.noButtomZeroes() {
+ if v[col] != 0 {
+ c++
+ }
+ if c > 1 {
+ return false
+ }
+ }
+ return c == 1
+}
+
+func (mt matrix) isReducedRowEchelon() bool {
+ l1p := -1
+ for _, v := range mt.noButtomZeroes() {
+ l1 := v.l1Pos()
+ if l1 < 0 || l1 < l1p || mt.isSingleNonZeroColumn(l1) == false {
+ return false
+ }
+ l1p = l1
+ }
+ return true
+}
+
+func main() {
+ for _, data := range []struct {
+ input matrix
+ output bool
+ }{
+ {
+ matrix{
+ row{1, 1, 0},
+ row{0, 1, 0},
+ row{0, 0, 0},
+ }, false,
+ },
+ {
+ matrix{
+ row{0, 1, -2, 0, 1},
+ row{0, 0, 0, 1, 3},
+ row{0, 0, 0, 0, 0},
+ row{0, 0, 0, 0, 0},
+ }, true,
+ },
+ {
+ matrix{
+ row{1, 0, 0, 4},
+ row{0, 1, 0, 7},
+ row{0, 0, 1, -1},
+ }, true,
+ },
+ {
+ matrix{
+ row{0, 1, -2, 0, 1},
+ row{0, 0, 0, 0, 0},
+ row{0, 0, 0, 1, 3},
+ row{0, 0, 0, 0, 0},
+ }, false,
+ },
+ {
+ matrix{
+ row{0, 1, 0},
+ row{1, 0, 0},
+ row{0, 0, 0},
+ }, false,
+ },
+ {
+ matrix{
+ row{4, 0, 0, 0},
+ row{0, 1, 0, 7},
+ row{0, 0, 1, -1},
+ }, false,
+ },
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(data.input.isReducedRowEchelon(), data.output)) // blank if ok, otherwise show the difference
+ }
+}