From 44973f277edfc10584e0d8e23d53b881796a19d3 Mon Sep 17 00:00:00 2001 From: Michael Manring Date: Mon, 27 May 2024 16:48:25 +1000 Subject: pwc271 solution in python --- challenge-271/pokgopun/python/ch-1.py | 69 +++++++++++++++++++++++++++++++++ challenge-271/pokgopun/python/ch-2.py | 72 +++++++++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+) create mode 100644 challenge-271/pokgopun/python/ch-1.py create mode 100644 challenge-271/pokgopun/python/ch-2.py diff --git a/challenge-271/pokgopun/python/ch-1.py b/challenge-271/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..5e2b758e0a --- /dev/null +++ b/challenge-271/pokgopun/python/ch-1.py @@ -0,0 +1,69 @@ +### 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 + +def maxOne(mtx: list): + return 1 - max( + (mtx[e].count(1),-e) for e in range(len(mtx)) + )[1] + +import unittest + +class TestMaxOne(unittest.TestCase): + def test(self): + for otpt, inpt in { + 1: [ [0, 1], + [1, 0], + ], + 2: [ [0, 0, 0], + [1, 0, 1], + ], + 2: [ [0, 0], + [1, 1], + [0, 0], + ], + }.items(): + self.assertEqual(maxOne(inpt),otpt) + +unittest.main() diff --git a/challenge-271/pokgopun/python/ch-2.py b/challenge-271/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..80ae73080e --- /dev/null +++ b/challenge-271/pokgopun/python/ch-2.py @@ -0,0 +1,72 @@ +### 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 + +def countBitOn(n): + c = 0 + while n > 0: + if n % 2 > 0: + c += 1 + n = n // 2 + return c + +def sortBitOn(nums: tuple): + return tuple( + sorted( + nums, + key = lambda x: (countBitOn(x), x) + ) + ) + +import unittest + +class TestSortBitOn(unittest.TestCase): + def test(self): + for inpt, otpt in { + (0, 1, 2, 3, 4, 5, 6, 7, 8): (0, 1, 2, 4, 8, 3, 5, 6, 7), + (1024, 512, 256, 128, 64): (64, 128, 256, 512, 1024), + }.items(): + self.assertEqual(sortBitOn(inpt),otpt) + +unittest.main() -- cgit From e6058f09ecf1f3dc5b9765c64d19c091299dc6d5 Mon Sep 17 00:00:00 2001 From: Michael Manring Date: Mon, 27 May 2024 19:47:38 +1000 Subject: pwc271 solution in go --- challenge-271/pokgopun/go/ch-1.go | 124 ++++++++++++++++++++++++++++++++++++++ challenge-271/pokgopun/go/ch-2.go | 87 ++++++++++++++++++++++++++ 2 files changed, 211 insertions(+) create mode 100644 challenge-271/pokgopun/go/ch-1.go create mode 100644 challenge-271/pokgopun/go/ch-2.go 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 + } +} -- cgit