aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Manring <michael@manring>2024-02-27 01:37:02 +1100
committerMichael Manring <michael@manring>2024-02-27 01:37:02 +1100
commit771dae0212804b162ea3d34db423e921b91efe0d (patch)
treefcc7f3046d6dfffc40a2a41c6a2f7c154be5f13b
parent659cdb7fad452141e1b2ec4a2bb14a9765501a6d (diff)
downloadperlweeklychallenge-club-771dae0212804b162ea3d34db423e921b91efe0d.tar.gz
perlweeklychallenge-club-771dae0212804b162ea3d34db423e921b91efe0d.tar.bz2
perlweeklychallenge-club-771dae0212804b162ea3d34db423e921b91efe0d.zip
pwc258 solution in go
-rw-r--r--challenge-258/pokgopun/go/ch-1.go78
-rw-r--r--challenge-258/pokgopun/go/ch-2.go92
2 files changed, 170 insertions, 0 deletions
diff --git a/challenge-258/pokgopun/go/ch-1.go b/challenge-258/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..83f4c84c0b
--- /dev/null
+++ b/challenge-258/pokgopun/go/ch-1.go
@@ -0,0 +1,78 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-258/
+/*#
+
+Task 1: Count Even Digits Number
+
+Submitted by: [42]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a array of positive integers, @ints.
+
+ Write a script to find out how many integers have even number of
+ digits.
+
+Example 1
+
+Input: @ints = (10, 1, 111, 24, 1000)
+Output: 3
+
+There are 3 integers having even digits i.e. 10, 24 and 1000.
+
+Example 2
+
+Input: @ints = (111, 1, 11111)
+Output: 0
+
+Example 3
+
+Input: @ints = (2, 8, 1024, 256)
+Output: 1
+
+Task 2: Sum of Values
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+type num int
+
+func (nm num) countDigit() int {
+ c := 0
+ for nm > 0 {
+ nm /= 10
+ c++
+ }
+ return c
+}
+
+type nums []num
+
+func (ns nums) cedn() int {
+ c := 0
+ for _, v := range ns {
+ if v.countDigit()%2 == 0 {
+ c++
+ }
+ }
+ return c
+}
+
+func main() {
+ for _, data := range []struct {
+ input nums
+ output int
+ }{
+ {nums{10, 1, 111, 24, 1000}, 3},
+ {nums{111, 1, 11111}, 0},
+ {nums{2, 8, 1024, 256}, 1},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(data.input.cedn(), data.output)) // blank if ok, otherwise show the differences
+ }
+}
diff --git a/challenge-258/pokgopun/go/ch-2.go b/challenge-258/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..14a352f7f0
--- /dev/null
+++ b/challenge-258/pokgopun/go/ch-2.go
@@ -0,0 +1,92 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-258/
+/*#
+
+Task 2: Sum of Values
+
+Submitted by: [43]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given an array of integers, @int and an integer $k.
+
+ Write a script to find the sum of values whose index binary
+ representation has exactly $k number of 1-bit set.
+
+Example 1
+
+Input: @ints = (2, 5, 9, 11, 3), $k = 1
+Output: 17
+
+Binary representation of index 0 = 0
+Binary representation of index 1 = 1
+Binary representation of index 2 = 10
+Binary representation of index 3 = 11
+Binary representation of index 4 = 100
+
+So the indices 1, 2 and 4 have total one 1-bit sets.
+Therefore the sum, $ints[1] + $ints[2] + $ints[3] = 17
+
+Example 2
+
+Input: @ints = (2, 5, 9, 11, 3), $k = 2
+Output: 11
+
+Example 3
+
+Input: @ints = (2, 5, 9, 11, 3), $k = 0
+Output: 2
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 1st March 2024.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+type num int
+
+func (nm num) countOn() int {
+ c := 0
+ for nm > 0 {
+ if nm%2 == 1 {
+ c++
+ }
+ nm /= 2
+ }
+ return c
+}
+
+type nums []int
+
+func (ns nums) sov(n int) int {
+ s := 0
+ for i, v := range ns {
+ if num(i).countOn() == n {
+ s += v
+ }
+ }
+ return s
+}
+
+func main() {
+ for _, data := range []struct {
+ ints nums
+ k int
+ output int
+ }{
+ {nums{2, 5, 9, 11, 3}, 1, 17},
+ {nums{2, 5, 9, 11, 3}, 2, 11},
+ {nums{2, 5, 9, 11, 3}, 0, 2},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(data.ints.sov(data.k), data.output)) // blank if ok, otherwise show the differences
+ }
+}