diff options
| author | Michael Manring <michael@manring> | 2024-05-27 19:47:38 +1000 |
|---|---|---|
| committer | Michael Manring <michael@manring> | 2024-05-27 19:47:38 +1000 |
| commit | e6058f09ecf1f3dc5b9765c64d19c091299dc6d5 (patch) | |
| tree | 8acd5bbab1411541de2af468453c22e923e4bbea | |
| parent | 44973f277edfc10584e0d8e23d53b881796a19d3 (diff) | |
| download | perlweeklychallenge-club-e6058f09ecf1f3dc5b9765c64d19c091299dc6d5.tar.gz perlweeklychallenge-club-e6058f09ecf1f3dc5b9765c64d19c091299dc6d5.tar.bz2 perlweeklychallenge-club-e6058f09ecf1f3dc5b9765c64d19c091299dc6d5.zip | |
pwc271 solution in go
| -rw-r--r-- | challenge-271/pokgopun/go/ch-1.go | 124 | ||||
| -rw-r--r-- | challenge-271/pokgopun/go/ch-2.go | 87 |
2 files changed, 211 insertions, 0 deletions
diff --git a/challenge-271/pokgopun/go/ch-1.go b/challenge-271/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..d2e9c3e0ba --- /dev/null +++ b/challenge-271/pokgopun/go/ch-1.go @@ -0,0 +1,124 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-271/ +/*# + +Task 1: Maximum Ones + +Submitted by: [42]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a m x n binary matrix. + + Write a script to return the row number containing maximum ones, in + case of more than one rows then return smallest row number. + +Example 1 + +Input: $matrix = [ [0, 1], + [1, 0], + ] +Output: 1 + +Row 1 and Row 2 have the same number of ones, so return row 1. + +Example 2 + +Input: $matrix = [ [0, 0, 0], + [1, 0, 1], + ] +Output: 2 + +Row 2 has the maximum ones, so return row 2. + +Example 3 + +Input: $matrix = [ [0, 0], + [1, 1], + [0, 0], + ] +Output: 2 + +Row 2 have the maximum ones, so return row 2. + +Task 2: Sort by 1 bits +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +type answer struct { + c, i int +} + +type row []int + +func (rw row) countOne() int { + c := 0 + l := len(rw) + for i := 0; i < l; i++ { + if rw[i] == 1 { + c++ + } + } + return c +} + +type matrix []row + +func (mtx matrix) maxOneRow() int { + mx := len(mtx[0]) + c := mtx[0].countOne() + if c == mx { + return 1 + } + ans := answer{c: c} + l := len(mtx) + for i := 1; i < l; i++ { + c = mtx[i].countOne() + if c == mx { + return i + 1 + } + if c > ans.c { + ans = answer{c, i} + } + } + return ans.i + 1 +} + +func main() { + for _, data := range []struct { + input matrix + output int + }{ + { + matrix{ + row{0, 1}, + row{1, 0}, + }, + 1, + }, + { + matrix{ + row{0, 0, 0}, + row{1, 0, 1}, + }, + 2, + }, + { + matrix{ + row{0, 0}, + row{1, 1}, + row{0, 0}, + }, + 2, + }, + } { + io.WriteString(os.Stdout, cmp.Diff(data.input.maxOneRow(), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-271/pokgopun/go/ch-2.go b/challenge-271/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..9f109be280 --- /dev/null +++ b/challenge-271/pokgopun/go/ch-2.go @@ -0,0 +1,87 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-271/ +/*# + +Task 2: Sort by 1 bits + +Submitted by: [43]Mohammad Sajid Anwar + __________________________________________________________________ + + You are give an array of integers, @ints. + + Write a script to sort the integers in ascending order by the number of + 1 bits in their binary representation. In case more than one integers + have the same number of 1 bits then sort them in ascending order. + +Example 1 + +Input: @ints = (0, 1, 2, 3, 4, 5, 6, 7, 8) +Output: (0, 1, 2, 4, 8, 3, 5, 6, 7) + +0 = 0 one bits +1 = 1 one bits +2 = 1 one bits +4 = 1 one bits +8 = 1 one bits +3 = 2 one bits +5 = 2 one bits +6 = 2 one bits +7 = 3 one bits + +Example 2 + +Input: @ints = (1024, 512, 256, 128, 64) +Output: (64, 128, 256, 512, 1024) + +All integers in the given array have one 1-bits, so just sort them in ascending +order. + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 2nd June 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + "sort" + + "github.com/google/go-cmp/cmp" +) + +func countBitOn(n uint) uint { + var c uint = 0 + for n > 0 { + if n%2 != 0 { + c++ + } + n /= 2 + } + return c +} + +func sortBitOn(uints []uint) []uint { + sort.Slice(uints, func(i, j int) bool { + a, b := countBitOn(uints[i]), countBitOn(uints[j]) + if a == b { + return uints[i] < uints[j] + } + return a < b + }) + return uints +} + +func main() { + for _, data := range []struct { + input, output []uint + }{ + {[]uint{0, 1, 2, 3, 4, 5, 6, 7, 8}, []uint{0, 1, 2, 4, 8, 3, 5, 6, 7}}, + {[]uint{1024, 512, 256, 128, 64}, []uint{64, 128, 256, 512, 1024}}, + } { + io.WriteString(os.Stdout, cmp.Diff(sortBitOn(data.input), data.output)) // blank if ok, otherwise show the difference + } +} |
