aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-10-10 15:01:42 +0100
committerGitHub <noreply@github.com>2023-10-10 15:01:42 +0100
commit35fba78e6f340e4d081ecf46c2ab876220a801df (patch)
tree2c5bc992026ebf03e91f54b3062026a7939636ae
parentb989494de26cd24376ad4193720e9080cbbf2a15 (diff)
parent962c2aa4bebf9d52f59e1384e804fc620afa7cd6 (diff)
downloadperlweeklychallenge-club-35fba78e6f340e4d081ecf46c2ab876220a801df.tar.gz
perlweeklychallenge-club-35fba78e6f340e4d081ecf46c2ab876220a801df.tar.bz2
perlweeklychallenge-club-35fba78e6f340e4d081ecf46c2ab876220a801df.zip
Merge pull request #8847 from pokgopun/pwc238
pwc238 solution
-rw-r--r--challenge-238/pokgopun/go/ch-1.go59
-rw-r--r--challenge-238/pokgopun/go/ch-2.go82
-rw-r--r--challenge-238/pokgopun/python/ch-1.py45
-rw-r--r--challenge-238/pokgopun/python/ch-2.py70
4 files changed, 256 insertions, 0 deletions
diff --git a/challenge-238/pokgopun/go/ch-1.go b/challenge-238/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..e429633daf
--- /dev/null
+++ b/challenge-238/pokgopun/go/ch-1.go
@@ -0,0 +1,59 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-238/
+/*#
+
+Task 1: Running Sum
+
+Submitted by: [43]Mohammad S Anwar
+ __________________________________________________________________
+
+ You are given an array of integers.
+
+ Write a script to return the running sum of the given array. The
+ running sum can be calculated as sum[i] = num[0] + num[1] + …. +
+ num[i].
+
+Example 1
+
+Input: @int = (1, 2, 3, 4, 5)
+Output: (1, 3, 6, 10, 15)
+
+Example 2
+
+Input: @int = (1, 1, 1, 1, 1)
+Output: (1, 2, 3, 4, 5)
+
+Example 3
+
+Input: @int = (0, -1, 1, 2)
+Output: (0, -1, 0, 2)
+
+Task 2: Persistence Sort
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "fmt"
+ "slices"
+)
+
+func main() {
+ for _, data := range []struct {
+ input, output []int
+ }{
+ {[]int{1, 2, 3, 4, 5}, []int{1, 3, 6, 10, 15}},
+ {[]int{1, 1, 1, 1, 1}, []int{1, 2, 3, 4, 5}},
+ {[]int{0, -1, 1, 2}, []int{0, -1, 0, 2}},
+ } {
+ fmt.Println(slices.Equal(runningSum(data.input), data.output))
+ }
+}
+
+func runningSum(s []int) []int {
+ l := len(s)
+ for i := 1; i < l; i++ {
+ s[i] += s[i-1]
+ }
+ return s
+}
diff --git a/challenge-238/pokgopun/go/ch-2.go b/challenge-238/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..b0cff2d66e
--- /dev/null
+++ b/challenge-238/pokgopun/go/ch-2.go
@@ -0,0 +1,82 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-238/
+/*#
+
+Task 2: Persistence Sort
+
+Submitted by: [44]Mohammad S Anwar
+ __________________________________________________________________
+
+ You are given an array of positive integers.
+
+ Write a script to sort the given array in increasing order with respect
+ to the count of steps required to obtain a single-digit number by
+ multiplying its digits recursively for each array element. If any two
+ numbers have the same count of steps, then print the smaller number
+ first.
+
+Example 1
+
+Input: @int = (15, 99, 1, 34)
+Output: (1, 15, 34, 99)
+
+15 => 1 x 5 => 5 (1 step)
+99 => 9 x 9 => 81 => 8 x 1 => 8 (2 steps)
+1 => 0 step
+34 => 3 x 4 => 12 => 1 x 2 => 2 (2 steps)
+
+Example 2
+
+Input: @int = (50, 25, 33, 22)
+Output: (22, 33, 50, 25)
+
+50 => 5 x 0 => 0 (1 step)
+25 => 2 x 5 => 10 => 1 x 0 => 0 (2 steps)
+33 => 3 x 3 => 9 (1 step)
+22 => 2 x 2 => 4 (1 step)
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 15th October
+ 2023.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "fmt"
+ "slices"
+ "sort"
+ "strconv"
+)
+
+func main() {
+ for _, data := range []struct {
+ input, output []int
+ }{
+ {[]int{15, 99, 1, 34}, []int{1, 15, 34, 99}},
+ {[]int{50, 25, 33, 22}, []int{22, 33, 50, 25}},
+ } {
+ sort.SliceStable(data.input, func(i, j int) bool {
+ return true
+ })
+ sort.SliceStable(data.input, func(i, j int) bool {
+ return mk1dgt(data.input[i]) < mk1dgt(data.input[j])
+ })
+ fmt.Println(slices.Equal(data.input, data.output))
+ }
+}
+
+func mk1dgt(n int) (i int) {
+ for n > 9 {
+ i++
+ r := 1
+ for _, v := range strconv.Itoa(n) {
+ r *= int(v) - 48
+ }
+ n = r
+ }
+ return i
+}
diff --git a/challenge-238/pokgopun/python/ch-1.py b/challenge-238/pokgopun/python/ch-1.py
new file mode 100644
index 0000000000..9ac57e1aa2
--- /dev/null
+++ b/challenge-238/pokgopun/python/ch-1.py
@@ -0,0 +1,45 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-238/
+"""
+
+Task 1: Running Sum
+
+Submitted by: [43]Mohammad S Anwar
+ __________________________________________________________________
+
+ You are given an array of integers.
+
+ Write a script to return the running sum of the given array. The
+ running sum can be calculated as sum[i] = num[0] + num[1] + …. +
+ num[i].
+
+Example 1
+
+Input: @int = (1, 2, 3, 4, 5)
+Output: (1, 3, 6, 10, 15)
+
+Example 2
+
+Input: @int = (1, 1, 1, 1, 1)
+Output: (1, 2, 3, 4, 5)
+
+Example 3
+
+Input: @int = (0, -1, 1, 2)
+Output: (0, -1, 0, 2)
+
+Task 2: Persistence Sort
+"""
+### solution by pokgopun@gmail.com
+
+def runningSum(tup):
+ lst = list(tup)
+ for i in range(1,len(lst)):
+ lst[i] += lst[i-1]
+ return tuple(lst)
+
+for inpt, otpt in {
+ (1, 2, 3, 4, 5):(1, 3, 6, 10, 15),
+ (1, 1, 1, 1, 1):(1, 2, 3, 4, 5),
+ (0, -1, 1, 2):(0, -1, 0, 2),
+ }.items():
+ print(runningSum(inpt)==otpt)
diff --git a/challenge-238/pokgopun/python/ch-2.py b/challenge-238/pokgopun/python/ch-2.py
new file mode 100644
index 0000000000..086194f65a
--- /dev/null
+++ b/challenge-238/pokgopun/python/ch-2.py
@@ -0,0 +1,70 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-238/
+"""
+
+Task 2: Persistence Sort
+
+Submitted by: [44]Mohammad S Anwar
+ __________________________________________________________________
+
+ You are given an array of positive integers.
+
+ Write a script to sort the given array in increasing order with respect
+ to the count of steps required to obtain a single-digit number by
+ multiplying its digits recursively for each array element. If any two
+ numbers have the same count of steps, then print the smaller number
+ first.
+
+Example 1
+
+Input: @int = (15, 99, 1, 34)
+Output: (1, 15, 34, 99)
+
+15 => 1 x 5 => 5 (1 step)
+99 => 9 x 9 => 81 => 8 x 1 => 8 (2 steps)
+1 => 0 step
+34 => 3 x 4 => 12 => 1 x 2 => 2 (2 steps)
+
+Example 2
+
+Input: @int = (50, 25, 33, 22)
+Output: (22, 33, 50, 25)
+
+50 => 5 x 0 => 0 (1 step)
+25 => 2 x 5 => 10 => 1 x 0 => 0 (2 steps)
+33 => 3 x 3 => 9 (1 step)
+22 => 2 x 2 => 4 (1 step)
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 15th October
+ 2023.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+"""
+### solution by pokgopun@gmail.com
+
+def mk1dgt(num):
+ i = 0
+ while num > 9:
+ i += 1
+ num = eval(
+ "*".join(
+ map(
+ lambda x: chr(x), bytes(str(num),"ascii")
+ )
+ )
+ )
+ return i
+
+def pSort(tup):
+ lst = sorted(list(tup))
+ lst.sort(key=mk1dgt)
+ return tuple(lst)
+
+for inpt, otpt in {
+ (15, 99, 1, 34):(1, 15, 34, 99),
+ (50, 25, 33, 22):(22, 33, 50, 25),
+ }.items():
+ print(pSort(inpt)==otpt)
+
+