aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-07-22 09:30:21 +0100
committerGitHub <noreply@github.com>2024-07-22 09:30:21 +0100
commit35bd04420628aa394a8fc9ece79140786a4bac18 (patch)
tree894d688ea00214e3068965b716f41089f0d6ac2c
parentc09789e1d811898d44e5f637c59693ed3d8d65be (diff)
parentd16417257e7f2381b0970c2d27ac2086c0ace1e9 (diff)
downloadperlweeklychallenge-club-35bd04420628aa394a8fc9ece79140786a4bac18.tar.gz
perlweeklychallenge-club-35bd04420628aa394a8fc9ece79140786a4bac18.tar.bz2
perlweeklychallenge-club-35bd04420628aa394a8fc9ece79140786a4bac18.zip
Merge pull request #10466 from pokgopun/pwc279
pwc279 solution
-rw-r--r--challenge-279/pokgopun/go/ch-1.go64
-rw-r--r--challenge-279/pokgopun/go/ch-2.go106
-rw-r--r--challenge-279/pokgopun/python/ch-1.py53
-rw-r--r--challenge-279/pokgopun/python/ch-2.py73
4 files changed, 296 insertions, 0 deletions
diff --git a/challenge-279/pokgopun/go/ch-1.go b/challenge-279/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..1417aaec23
--- /dev/null
+++ b/challenge-279/pokgopun/go/ch-1.go
@@ -0,0 +1,64 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-279/
+/*#
+
+Task 1: Sort Letters
+
+Submitted by: [45]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given two arrays, @letters and @weights.
+
+ Write a script to sort the given array @letters based on the @weights.
+
+Example 1
+
+Input: @letters = ('R', 'E', 'P', 'L')
+ @weights = (3, 2, 1, 4)
+Output: PERL
+
+Example 2
+
+Input: @letters = ('A', 'U', 'R', 'K')
+ @weights = (2, 4, 1, 3)
+Output: RAKU
+
+Example 3
+
+Input: @letters = ('O', 'H', 'Y', 'N', 'P', 'T')
+ @weights = (5, 4, 2, 6, 1, 3)
+Output: PYTHON
+
+Task 2: Split String
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+func sortLetters(letters []rune, weights []int) string {
+ r := make([]rune, len(letters))
+ for i, v := range letters {
+ r[weights[i]-1] = v
+ }
+ return string(r)
+}
+
+func main() {
+ for _, data := range []struct {
+ letters []rune
+ weights []int
+ output string
+ }{
+ {[]rune{'R', 'E', 'P', 'L'}, []int{3, 2, 1, 4}, "PERL"},
+ {[]rune{'A', 'U', 'R', 'K'}, []int{2, 4, 1, 3}, "RAKU"},
+ {[]rune{'O', 'H', 'Y', 'N', 'P', 'T'}, []int{5, 4, 2, 6, 1, 3}, "PYTHON"},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(sortLetters(data.letters, data.weights), data.output)) // blank if ok otherwise show the difference
+ }
+}
diff --git a/challenge-279/pokgopun/go/ch-2.go b/challenge-279/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..852579e91a
--- /dev/null
+++ b/challenge-279/pokgopun/go/ch-2.go
@@ -0,0 +1,106 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-279/
+/*#
+
+Task 2: Split String
+
+Submitted by: [46]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a string, $str.
+
+ Write a script to split the given string into two containing exactly
+ same number of vowels and return true if you can otherwise false.
+
+Example 1
+
+Input: $str = "perl"
+Ouput: false
+
+Example 2
+
+Input: $str = "book"
+Ouput: true
+
+Two possible strings "bo" and "ok" containing exactly one vowel each.
+
+Example 3
+
+Input: $str = "good morning"
+Ouput: true
+
+Two possible strings "good " and "morning" containing two vowels each or "good m
+" and "orning" containing two vowels each.
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 28th July 2024.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+type text string
+
+func (tx text) vwlIdx() []int {
+ var r []int
+ for i, v := range tx {
+ switch v {
+ case 'a', 'e', 'i', 'o', 'u':
+ r = append(r, i)
+ }
+ }
+ return r
+}
+
+func splitString0(str text) bool {
+ l := len(str.vwlIdx())
+ if l > 0 {
+ return l%2 == 0
+ }
+ return false
+}
+
+type splitted struct {
+ First, Second text
+ Ok bool
+}
+
+func splitString1(str text) splitted {
+ idx := str.vwlIdx()
+ l := len(idx)
+ h := l / 2
+ b := l%2 == 0
+ if b {
+ h -= 1
+ }
+ var i int
+ if l > 1 {
+ i = idx[h+1]
+ } else {
+ i = idx[h] + 1
+ }
+ return splitted{str[:i], str[i:], b}
+}
+
+func main() {
+ for _, data := range []struct {
+ str text
+ output splitted
+ }{
+ {"perl", splitted{"pe", "rl", false}},
+ {"book", splitted{"bo", "ok", true}},
+ {"good morning", splitted{"good m", "orning", true}},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(splitString0(data.str), data.output.Ok)) // blank if ok, otherwise show the difference
+ io.WriteString(os.Stdout, cmp.Diff(splitString1(data.str), data.output)) // blank if ok, otherwise show the difference
+ }
+}
diff --git a/challenge-279/pokgopun/python/ch-1.py b/challenge-279/pokgopun/python/ch-1.py
new file mode 100644
index 0000000000..d68c18c09b
--- /dev/null
+++ b/challenge-279/pokgopun/python/ch-1.py
@@ -0,0 +1,53 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-279/
+"""
+
+Task 1: Sort Letters
+
+Submitted by: [45]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given two arrays, @letters and @weights.
+
+ Write a script to sort the given array @letters based on the @weights.
+
+Example 1
+
+Input: @letters = ('R', 'E', 'P', 'L')
+ @weights = (3, 2, 1, 4)
+Output: PERL
+
+Example 2
+
+Input: @letters = ('A', 'U', 'R', 'K')
+ @weights = (2, 4, 1, 3)
+Output: RAKU
+
+Example 3
+
+Input: @letters = ('O', 'H', 'Y', 'N', 'P', 'T')
+ @weights = (5, 4, 2, 6, 1, 3)
+Output: PYTHON
+
+Task 2: Split String
+"""
+### solution by pokgopun@gmail.com
+
+def sortLetters(letters,weights):
+ return "".join(
+ e[1] for e in sorted(
+ (weights[i],letters[i]) for i in range(len(letters))
+ )
+ )
+
+import unittest
+
+class TestSortLetters(unittest.TestCase):
+ def test(self):
+ for (letters,weights),otpt in {
+ (('R', 'E', 'P', 'L'),(3, 2, 1, 4)): "PERL",
+ (('A', 'U', 'R', 'K'),(2, 4, 1, 3)): "RAKU",
+ (('O', 'H', 'Y', 'N', 'P', 'T'),(5, 4, 2, 6, 1, 3)): "PYTHON",
+ }.items():
+ self.assertEqual(sortLetters(letters,weights),otpt)
+
+unittest.main()
diff --git a/challenge-279/pokgopun/python/ch-2.py b/challenge-279/pokgopun/python/ch-2.py
new file mode 100644
index 0000000000..129bc186bb
--- /dev/null
+++ b/challenge-279/pokgopun/python/ch-2.py
@@ -0,0 +1,73 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-279/
+"""
+
+Task 2: Split String
+
+Submitted by: [46]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a string, $str.
+
+ Write a script to split the given string into two containing exactly
+ same number of vowels and return true if you can otherwise false.
+
+Example 1
+
+Input: $str = "perl"
+Ouput: false
+
+Example 2
+
+Input: $str = "book"
+Ouput: true
+
+Two possible strings "bo" and "ok" containing exactly one vowel each.
+
+Example 3
+
+Input: $str = "good morning"
+Ouput: true
+
+Two possible strings "good " and "morning" containing two vowels each or "good m
+" and "orning" containing two vowels each.
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 28th July 2024.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+"""
+### solution by pokgopun@gmail.com
+
+def splitString0(string: str):
+ return sum( 1 for i in range(len(string)) if string[i] in "aeiou" ) % 2 == 0
+
+def splitString1(string: str):
+ idx = tuple( i for i in range(len(string)) if string[i] in "aeiou" )
+ l = len(idx)
+ h = int(l/2)
+ b = l % 2 == 0
+ if b:
+ h -= 1
+ i = idx[h+1] if l > 1 else idx[h]+1
+ return (string[:i], string[i:], b)
+
+import unittest
+
+class TestSplitString(unittest.TestCase):
+ def test0(self):
+ for inpt,otpt in {
+ "perl": False,
+ "book": True,
+ "good morning": True,
+ }.items():
+ self.assertEqual(splitString0(inpt),otpt)
+ def test1(self):
+ for inpt,otpt in {
+ "perl": ("pe","rl",False),
+ "book": ("bo","ok",True),
+ "good morning": ("good m","orning",True),
+ }.items():
+ self.assertEqual(splitString1(inpt),otpt)
+
+unittest.main()