aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-11-21 14:20:04 +0000
committerGitHub <noreply@github.com>2023-11-21 14:20:04 +0000
commit0bef6336fe398d9a9b4aebb7d953aea705bccf2d (patch)
tree04983ed404928b024130c8a4a425e06ecb443b8d
parent4861800a599b9fc0aee3ba98c9880beb68da2999 (diff)
parent671c45ddf995803cf80faa4ffb0cb365369b9022 (diff)
downloadperlweeklychallenge-club-0bef6336fe398d9a9b4aebb7d953aea705bccf2d.tar.gz
perlweeklychallenge-club-0bef6336fe398d9a9b4aebb7d953aea705bccf2d.tar.bz2
perlweeklychallenge-club-0bef6336fe398d9a9b4aebb7d953aea705bccf2d.zip
Merge pull request #9116 from pokgopun/pwc244
pwc244 solution in go
-rw-r--r--challenge-244/pokgopun/go/ch-1.go71
-rw-r--r--challenge-244/pokgopun/go/ch-2.go99
2 files changed, 170 insertions, 0 deletions
diff --git a/challenge-244/pokgopun/go/ch-1.go b/challenge-244/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..8a781161fb
--- /dev/null
+++ b/challenge-244/pokgopun/go/ch-1.go
@@ -0,0 +1,71 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-244/
+/*#
+
+Task 1: Count Smaller
+
+Submitted by: [45]Mohammad S Anwar
+ __________________________________________________________________
+
+ You are given an array of integers.
+
+ Write a script to calculate the number of integers smaller than the
+ integer at each index.
+
+Example 1
+
+Input: @int = (8, 1, 2, 2, 3)
+Output: (4, 0, 1, 1, 3)
+
+For index = 0, count of elements less 8 is 4.
+For index = 1, count of elements less 1 is 0.
+For index = 2, count of elements less 2 is 1.
+For index = 3, count of elements less 2 is 1.
+For index = 4, count of elements less 3 is 3.
+
+Example 2
+
+Input: @int = (6, 5, 4, 8)
+Output: (2, 1, 0, 3)
+
+Example 3
+
+Input: @int = (2, 2, 2)
+Output: (0, 0, 0)
+
+Task 2: Group Hero
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "fmt"
+ "reflect"
+)
+
+type mArr []int
+
+func (ma mArr) conv() (r mArr) {
+ l := len(ma)
+ r = make(mArr, l)
+ for i, v := range ma {
+ for j := 0; j < l; j++ {
+ if ma[j] < v {
+ r[i]++
+ }
+ }
+ }
+ return r
+}
+
+func main() {
+ for _, data := range []struct {
+ input, output mArr
+ }{
+ {mArr{8, 1, 2, 2, 3}, mArr{4, 0, 1, 1, 3}},
+ {mArr{6, 5, 4, 8}, mArr{2, 1, 0, 3}},
+ {mArr{2, 2, 2}, mArr{0, 0, 0}},
+ } {
+ fmt.Println(reflect.DeepEqual(data.input.conv(), data.output))
+ }
+}
diff --git a/challenge-244/pokgopun/go/ch-2.go b/challenge-244/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..69b3a4c228
--- /dev/null
+++ b/challenge-244/pokgopun/go/ch-2.go
@@ -0,0 +1,99 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-244/
+/*#
+
+Task 2: Group Hero
+
+Submitted by: [46]Mohammad S Anwar
+ __________________________________________________________________
+
+ You are given an array of integers representing the strength.
+
+ Write a script to return the sum of the powers of all possible
+ combinations; power is defined as the square of the largest number in a
+ sequence, multiplied by the smallest.
+
+Example 1
+
+Input: @nums = (2, 1, 4)
+Output: 141
+
+Group 1: (2) => square(max(2)) * min(2) => 4 * 2 => 8
+Group 2: (1) => square(max(1)) * min(1) => 1 * 1 => 1
+Group 3: (4) => square(max(4)) * min(4) => 16 * 4 => 64
+Group 4: (2,1) => square(max(2,1)) * min(2,1) => 4 * 1 => 4
+Group 5: (2,4) => square(max(2,4)) * min(2,4) => 16 * 2 => 32
+Group 6: (1,4) => square(max(1,4)) * min(1,4) => 16 * 1 => 16
+Group 7: (2,1,4) => square(max(2,1,4)) * min(2,1,4) => 16 * 1 => 16
+
+Sum: 8 + 1 + 64 + 4 + 32 + 16 + 16 => 141
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 26th November
+ 2023.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "fmt"
+)
+
+type mArr []int
+
+func (ma mArr) power() int {
+ mx := ma.m(true)
+ return mx * mx * ma.m(false)
+}
+
+func (ma mArr) m(b bool) (m int) {
+ l := len(ma)
+ f := func(x, y int) int {
+ if b {
+ return max(x, y)
+ }
+ return min(x, y)
+ }
+ if l > 0 {
+ m = ma[0]
+ for l > 1 {
+ l--
+ m = f(m, ma[l])
+ }
+ }
+ return m
+}
+
+func (ma mArr) sumOfPower() (s int) {
+ l := len(ma)
+ ma0 := make(mArr, l)
+ var d, i, j int
+ for n := 1; n < 1<<l; n++ {
+ d, i, j = n, 0, 0
+ for d > 0 {
+ if d%2 > 0 {
+ ma0[j] = ma[i]
+ j++
+ }
+ i++
+ d /= 2
+ }
+ s += ma0[:j].power()
+ clear(ma0)
+ }
+ return s
+}
+
+func main() {
+ for _, data := range []struct {
+ input mArr
+ output int
+ }{
+ {mArr{2, 1, 4}, 141},
+ } {
+ fmt.Println(data.input.sumOfPower() == data.output)
+ }
+}