diff options
| author | Michael Manring <michael@manring> | 2024-01-22 22:04:02 +1100 |
|---|---|---|
| committer | Michael Manring <michael@manring> | 2024-01-22 23:54:45 +1100 |
| commit | ed8eb80129d059030fff266afadd62fdabf44068 (patch) | |
| tree | b5e8769571649e15519b2574fe3ea5823ed92cbe | |
| parent | 8b20006ad65464ae2ffdc77b7ebc1f4ba2cfde9b (diff) | |
| download | perlweeklychallenge-club-ed8eb80129d059030fff266afadd62fdabf44068.tar.gz perlweeklychallenge-club-ed8eb80129d059030fff266afadd62fdabf44068.tar.bz2 perlweeklychallenge-club-ed8eb80129d059030fff266afadd62fdabf44068.zip | |
pwc253 solution in go
| -rw-r--r-- | challenge-253/pokgopun/go/ch-1.go | 64 | ||||
| -rw-r--r-- | challenge-253/pokgopun/go/ch-2.go | 126 |
2 files changed, 190 insertions, 0 deletions
diff --git a/challenge-253/pokgopun/go/ch-1.go b/challenge-253/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..2946e5dd1e --- /dev/null +++ b/challenge-253/pokgopun/go/ch-1.go @@ -0,0 +1,64 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-253/ +/*# + +Task 1: Split Strings + +Submitted by: [43]Mohammad S Anwar + __________________________________________________________________ + + You are given an array of strings and a character separator. + + Write a script to return all words separated by the given character + excluding empty string. + +Example 1 + +Input: @words = ("one.two.three","four.five","six") + $separator = "." +Output: "one","two","three","four","five","six" + +Example 2 + +Input: @words = ("$perl$$", "$$raku$") + $separator = "$" +Output: "perl","raku" + +Task 2: Weakest Row +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + "strings" + + "github.com/google/go-cmp/cmp" +) + +type words []string + +func (ws words) split(sep string) words { + var r words + for _, v := range ws { + for _, w := range strings.Split(v, sep) { + if w != "" { + r = append(r, w) + } + } + } + return r +} + +func main() { + for _, data := range []struct { + wrds, otpt words + sep string + }{ + {words{"one.two.three", "four.five", "six"}, words{"one", "two", "three", "four", "five", "six"}, "."}, + {words{"$perl$$", "$$raku$"}, words{"perl", "raku"}, "$"}, + } { + io.WriteString(os.Stdout, cmp.Diff(data.wrds.split(data.sep), data.otpt)) //blank if ok, otherwise show the difference + } +} diff --git a/challenge-253/pokgopun/go/ch-2.go b/challenge-253/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..f8f532ebd1 --- /dev/null +++ b/challenge-253/pokgopun/go/ch-2.go @@ -0,0 +1,126 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-253/ +/*# + +Task 2: Weakest Row + +Submitted by: [44]Mohammad S Anwar + __________________________________________________________________ + + You are given an m x n binary matrix i.e. only 0 and 1 where 1 always + appear before 0. + + A row i is weaker than a row j if one of the following is true: +a) The number of 1s in row i is less than the number of 1s in row j. +b) Both rows have the same number of 1 and i < j. + + Write a script to return the order of rows from weakest to strongest. + +Example 1 + +Input: $matrix = [ + [1, 1, 0, 0, 0], + [1, 1, 1, 1, 0], + [1, 0, 0, 0, 0], + [1, 1, 0, 0, 0], + [1, 1, 1, 1, 1] + ] +Output: (2, 0, 3, 1, 4) + +The number of 1s in each row is: +- Row 0: 2 +- Row 1: 4 +- Row 2: 1 +- Row 3: 2 +- Row 4: 5 + +Example 2 + +Input: $matrix = [ + [1, 0, 0, 0], + [1, 1, 1, 1], + [1, 0, 0, 0], + [1, 0, 0, 0] + ] +Output: (0, 2, 3, 1) + +The number of 1s in each row is: +- Row 0: 1 +- Row 1: 4 +- Row 2: 1 +- Row 3: 1 + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 28th January + 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "cmp" + "io" + "os" + "slices" + + gocmp "github.com/google/go-cmp/cmp" +) + +type row []uint8 + +func (rw row) count() (c uint) { + for _, v := range rw { + if v > 0 { + c++ + } + } + return c +} + +type matrix []row + +func (mt matrix) weakestRow() []uint { + l := len(mt) + r := make([]uint, l) + s := make([]uint, l) + for i, rw := range mt { + r[i] = rw.count() + s[i] = uint(i) + } + slices.SortStableFunc(s, func(a, b uint) int { + return cmp.Compare(r[a], r[b]) + }) + return s +} + +func main() { + for _, data := range []struct { + input matrix + output []uint + }{ + { + matrix{ + row{1, 1, 0, 0, 0}, + row{1, 1, 1, 1, 0}, + row{1, 0, 0, 0, 0}, + row{1, 1, 0, 0, 0}, + row{1, 1, 1, 1, 1}, + }, + []uint{2, 0, 3, 1, 4}, + }, + { + matrix{ + row{1, 0, 0, 0}, + row{1, 1, 1, 1}, + row{1, 0, 0, 0}, + row{1, 0, 0, 1}, + }, + []uint{0, 2, 3, 1}, + }, + } { + io.WriteString(os.Stdout, gocmp.Diff(data.input.weakestRow(), data.output)) //blank if ok, otherwise show the difference + } +} |
