aboutsummaryrefslogtreecommitdiff
path: root/challenge-252
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-01-16 18:20:27 +0000
committerGitHub <noreply@github.com>2024-01-16 18:20:27 +0000
commit7c939f3b32e5363f639a1a20acd17cf04b9e7fac (patch)
treef2e238344073e155d00d5c8bfec576df1ad6d819 /challenge-252
parent7efb373bb9adffa79f84825217015835805298b5 (diff)
parente35ca085014d2015a5fd5b756ba3058b6f88865c (diff)
downloadperlweeklychallenge-club-7c939f3b32e5363f639a1a20acd17cf04b9e7fac.tar.gz
perlweeklychallenge-club-7c939f3b32e5363f639a1a20acd17cf04b9e7fac.tar.bz2
perlweeklychallenge-club-7c939f3b32e5363f639a1a20acd17cf04b9e7fac.zip
Merge pull request #9402 from pokgopun/pwc252
pwc252 solution
Diffstat (limited to 'challenge-252')
-rw-r--r--challenge-252/pokgopun/go/ch-1.go80
-rw-r--r--challenge-252/pokgopun/go/ch-2.go144
-rw-r--r--challenge-252/pokgopun/python/ch-1.py67
-rw-r--r--challenge-252/pokgopun/python/ch-2.py70
4 files changed, 361 insertions, 0 deletions
diff --git a/challenge-252/pokgopun/go/ch-1.go b/challenge-252/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..af61241188
--- /dev/null
+++ b/challenge-252/pokgopun/go/ch-1.go
@@ -0,0 +1,80 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-252/
+/*#
+
+Task 1: Special Numbers
+
+Submitted by: [45]Mohammad S Anwar
+ __________________________________________________________________
+
+ You are given an array of integers, @ints.
+
+ Write a script to find the sum of the squares of all special elements
+ of the given array.
+An element $int[i] of @ints is called special if i divides n, i.e. n % i == 0.
+Where n is the length of the given array. Also the array is 1-indexed for the ta
+sk.
+
+Example 1
+
+Input: @ints = (1, 2, 3, 4)
+Output: 21
+
+There are exactly 3 special elements in the given array:
+$ints[1] since 1 divides 4,
+$ints[2] since 2 divides 4, and
+$ints[4] since 4 divides 4.
+
+Hence, the sum of the squares of all special elements of given array:
+1 * 1 + 2 * 2 + 4 * 4 = 21.
+
+Example 2
+
+Input: @ints = (2, 7, 1, 19, 18, 3)
+Output: 63
+
+There are exactly 4 special elements in the given array:
+$ints[1] since 1 divides 6,
+$ints[2] since 2 divides 6,
+$ints[3] since 3 divides 6, and
+$ints[6] since 6 divides 6.
+
+Hence, the sum of the squares of all special elements of given array:
+2 * 2 + 7 * 7 + 1 * 1 + 3 * 3 = 63
+
+Task 2: Unique Sum Zero
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+type ints []int
+
+func (is ints) snum() int {
+ l := len(is)
+ r := 0
+ for i, v := range is {
+ if l%(i+1) == 0 {
+ r += v * v
+ }
+ }
+ return r
+}
+
+func main() {
+ for _, data := range []struct {
+ input ints
+ output int
+ }{
+ {ints{1, 2, 3, 4}, 21},
+ {ints{2, 7, 1, 19, 18, 3}, 63},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(data.input.snum(), data.output)) // blank if ok, otherwise show the difference
+ }
+}
diff --git a/challenge-252/pokgopun/go/ch-2.go b/challenge-252/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..d8d9654546
--- /dev/null
+++ b/challenge-252/pokgopun/go/ch-2.go
@@ -0,0 +1,144 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-252/
+/*#
+
+Task 2: Unique Sum Zero
+
+Submitted by: [46]Mohammad S Anwar
+ __________________________________________________________________
+
+ You are given an integer, $n.
+
+ Write a script to find an array containing $n unique integers such that
+ they add up to zero.
+
+Example 1
+
+Input: $n = 5
+Output: (-7, -1, 1, 3, 4)
+
+Two other possible solutions could be as below:
+(-5, -1, 1, 2, 3) and (-3, -1, 2, -2, 4).
+
+Example 2
+
+Input: $n = 3
+Output: (-1, 0, 1)
+
+Example 3
+
+Input: $n = 1
+Output: (0)
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 21st January
+ 2024.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+ "slices"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+func isUsz(n int, s []int) bool {
+ slices.Sort(s)
+ l := 0
+ for i := 1; i < n; i++ {
+ l -= i
+ }
+ p := make([]int, n-l)
+ for i := 0; i < n-l; i++ {
+ p[i] = l + i
+ }
+ for v := range Combinations(p, uint(n)) {
+ sum := 0
+ for _, i := range v {
+ sum += i
+ }
+ if sum == 0 && cmp.Diff(v, s) == "" {
+ return true
+ }
+ }
+ return false
+}
+
+type ints []int
+
+func main() {
+ for _, data := range []struct {
+ n int
+ arr ints
+ res bool
+ }{
+ {5, ints{-7, -1, 1, 3, 4}, true},
+ {5, ints{-5, -1, 1, 2, 3}, true},
+ {5, ints{-3, -1, 2, -2, 4}, true},
+ {3, ints{-1, 0, 1}, true},
+ {1, ints{0}, true},
+ {5, ints{-6, -1, 1, 3, 4}, false},
+ {5, ints{-5, 1, 1, 2, 3}, false},
+ {5, ints{-3, -1, 1, 2, 4}, false},
+ {3, ints{-2, 0, 1}, false},
+ {1, ints{1}, false},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(isUsz(data.n, data.arr), data.res)) // blank if ok, otherwise show the difference
+ }
+}
+
+// transcribed from https://docs.python.org/3/library/itertools.html#itertools.combinations
+func Combinations[E any](s []E, r uint) chan []E {
+ res := make(chan []E)
+ n := uint(len(s))
+ if r > n {
+ go func() {
+ res <- []E{}
+ close(res)
+ }()
+ return res
+ }
+ go func() {
+ res <- s[:r]
+ idx := make([]uint, r)
+ for i := range idx {
+ idx[i] = uint(i)
+ }
+ var i, j uint
+ for {
+ i = r
+ for {
+ i--
+ if idx[i] != i+n-r {
+ break
+ }
+ if i > 0 {
+ continue
+ }
+ close(res)
+ return
+ }
+ idx[i]++
+ j = i + 1
+ for j < r {
+ idx[j] = idx[j-1] + 1
+ j++
+ }
+ result := make([]E, r)
+ i = 0
+ for _, v := range idx {
+ result[i] = s[v]
+ i++
+ }
+ res <- result
+ }
+ close(res)
+ }()
+ return res
+}
diff --git a/challenge-252/pokgopun/python/ch-1.py b/challenge-252/pokgopun/python/ch-1.py
new file mode 100644
index 0000000000..ae6a9021e9
--- /dev/null
+++ b/challenge-252/pokgopun/python/ch-1.py
@@ -0,0 +1,67 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-252/
+"""
+
+Task 1: Special Numbers
+
+Submitted by: [45]Mohammad S Anwar
+ __________________________________________________________________
+
+ You are given an array of integers, @ints.
+
+ Write a script to find the sum of the squares of all special elements
+ of the given array.
+An element $int[i] of @ints is called special if i divides n, i.e. n % i == 0.
+Where n is the length of the given array. Also the array is 1-indexed for the ta
+sk.
+
+Example 1
+
+Input: @ints = (1, 2, 3, 4)
+Output: 21
+
+There are exactly 3 special elements in the given array:
+$ints[1] since 1 divides 4,
+$ints[2] since 2 divides 4, and
+$ints[4] since 4 divides 4.
+
+Hence, the sum of the squares of all special elements of given array:
+1 * 1 + 2 * 2 + 4 * 4 = 21.
+
+Example 2
+
+Input: @ints = (2, 7, 1, 19, 18, 3)
+Output: 63
+
+There are exactly 4 special elements in the given array:
+$ints[1] since 1 divides 6,
+$ints[2] since 2 divides 6,
+$ints[3] since 3 divides 6, and
+$ints[6] since 6 divides 6.
+
+Hence, the sum of the squares of all special elements of given array:
+2 * 2 + 7 * 7 + 1 * 1 + 3 * 3 = 63
+
+Task 2: Unique Sum Zero
+"""
+### solution by pokgopun@gmail.com
+
+def snum(tup: tuple):
+ l = len(tup)
+ return sum(
+ tup[i]**2 for i in range(0,l)
+ if l % (i+1) == 0
+ )
+
+import unittest
+
+class TestSnum(unittest.TestCase):
+ def test(self):
+ for inpt,otpt in {
+ (1, 2, 3, 4): 21,
+ (2, 7, 1, 19, 18, 3): 63,
+ }.items():
+ self.assertEqual(snum(inpt),otpt)
+
+unittest.main()
+
+
diff --git a/challenge-252/pokgopun/python/ch-2.py b/challenge-252/pokgopun/python/ch-2.py
new file mode 100644
index 0000000000..5eb2dc31da
--- /dev/null
+++ b/challenge-252/pokgopun/python/ch-2.py
@@ -0,0 +1,70 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-252/
+"""
+
+Task 2: Unique Sum Zero
+
+Submitted by: [46]Mohammad S Anwar
+ __________________________________________________________________
+
+ You are given an integer, $n.
+
+ Write a script to find an array containing $n unique integers such that
+ they add up to zero.
+
+Example 1
+
+Input: $n = 5
+Output: (-7, -1, 1, 3, 4)
+
+Two other possible solutions could be as below:
+(-5, -1, 1, 2, 3) and (-3, -1, 2, -2, 4).
+
+Example 2
+
+Input: $n = 3
+Output: (-1, 0, 1)
+
+Example 3
+
+Input: $n = 1
+Output: (0)
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 21st January
+ 2024.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+"""
+### solution by pokgopun@gmail.com
+
+from itertools import combinations
+
+def isUsz(n: int, tup: tuple):
+ # assuming array member is less than n, the min value for the member will be -(1 + .. + n - 1)
+ p = range(-sum(i for i in range(1,n)), n)
+ return set(tup) in ( set(cmb) for cmb in combinations(p,n) if sum(cmb)==0 )
+
+import unittest
+
+class TestIsUsz(unittest.TestCase):
+ def test1(self):
+ for n,tup in {
+ 5: (-7, -1, 1, 3, 4),
+ 5: (-5, -1, 1, 2, 3),
+ 5: (-3, -1, 2, -2, 4),
+ 3: (-1, 0, 1),
+ 1: (0,),
+ }.items():
+ self.assertEqual(isUsz(n,tup),True)
+ def test2(self):
+ for n,tup in {
+ 5: (-1, 0, 1, 2, 3),
+ 5: (-2, -1, 0, 1, 3),
+ 5: (-3, -1, 0, 1, 2),
+ 3: (-2, 0, 1),
+ 1: (1,),
+ }.items():
+ self.assertEqual(isUsz(n,tup),False)
+
+unittest.main()