diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-04-29 15:38:13 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-04-29 15:38:13 +0100 |
| commit | 944cd752aa16f4b6c81494ed32a78985c7e5731c (patch) | |
| tree | 760196939b7856df027b8c8530c669343f0d348e /challenge-319 | |
| parent | 776d8b30b44e6adeb6d5d5f39ddd400809e3207e (diff) | |
| parent | 3f37c77fa199178b30ff3d655284eaa97473bd23 (diff) | |
| download | perlweeklychallenge-club-944cd752aa16f4b6c81494ed32a78985c7e5731c.tar.gz perlweeklychallenge-club-944cd752aa16f4b6c81494ed32a78985c7e5731c.tar.bz2 perlweeklychallenge-club-944cd752aa16f4b6c81494ed32a78985c7e5731c.zip | |
Merge pull request #11946 from pokgopun/pwc319
Pwc319
Diffstat (limited to 'challenge-319')
| -rw-r--r-- | challenge-319/pokgopun/go/ch-1.go | 89 | ||||
| -rw-r--r-- | challenge-319/pokgopun/go/ch-2.go | 85 | ||||
| -rw-r--r-- | challenge-319/pokgopun/python/ch-1.py | 50 | ||||
| -rw-r--r-- | challenge-319/pokgopun/python/ch-2.py | 58 |
4 files changed, 282 insertions, 0 deletions
diff --git a/challenge-319/pokgopun/go/ch-1.go b/challenge-319/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..8695486b97 --- /dev/null +++ b/challenge-319/pokgopun/go/ch-1.go @@ -0,0 +1,89 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-319/ +/*# + +Task 1: Word Count + +Submitted by: [46]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a list of words containing alphabetic characters only. + + Write a script to return the count of words either starting with a + vowel or ending with a vowel. + +Example 1 + +Input: @list = ("unicode", "xml", "raku", "perl") +Output: 2 + +The words are "unicode" and "raku". + +Example 2 + +Input: @list = ("the", "weekly", "challenge") +Output: 2 + +Example 3 + +Input: @list = ("perl", "python", "postgres") +Output: 0 + +Task 2: Minimum Common +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +type words []word + +func (ws words) wc() int { + c := 0 + for _, v := range ws { + if v.charIsVowel(0) { + c++ + } else { + l := len(v) + if v.charIsVowel(l - 1) { + c++ + } + } + } + return c +} + +type word string + +func (w word) charIsVowel(i int) bool { + c := []rune(w)[i] + if c >= 'A' && c <= 'Z' { + c += 'a' - 'A' + } + for _, r := range "aeiuo" { + if r == c { + return true + } + } + return false +} + +func main() { + for _, data := range []struct { + input words + output int + }{ + {words{"unicode", "xml", "raku", "perl"}, 2}, + {words{"the", "weekly", "challenge"}, 2}, + {words{"perl", "python", "postgres"}, 0}, + {words{"Unicode", "xml", "rakU", "perl"}, 2}, + {words{"thE", "weekly", "challengE"}, 2}, + } { + io.WriteString(os.Stdout, cmp.Diff(data.input.wc(), data.output)) // blank if ok, otherwise show the differences + } +} diff --git a/challenge-319/pokgopun/go/ch-2.go b/challenge-319/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..8d550634bf --- /dev/null +++ b/challenge-319/pokgopun/go/ch-2.go @@ -0,0 +1,85 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-319/ +/*# + +Task 2: Minimum Common + +Submitted by: [47]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given two arrays of integers. + + Write a script to return the minimum integer common to both arrays. If + none found return -1. + +Example 1 + +Input: @array_1 = (1, 2, 3, 4) + @array_2 = (3, 4, 5, 6) +Output: 3 + +The common integer in both arrays: 3, 4 +The minimum is 3. + +Example 2 + +Input: @array_1 = (1, 2, 3) + @array_2 = (2, 4) +Output: 2 + +Example 3 + +Input: @array_1 = (1, 2, 3, 4) + @array_2 = (5, 6, 7, 8) +Output: -1 + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 4th May 2025. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +type ints []int + +type input struct { + arr1, arr2 ints +} + +func (in input) process() int { + m := make(map[int]int) + mn := -1 + for _, v := range in.arr1 { + m[v]++ + } + for _, v := range in.arr2 { + if _, ok := m[v]; ok { + if mn == -1 || mn > v { + mn = v + } + } + } + return mn +} + +func main() { + for _, data := range []struct { + input input + output int + }{ + {input{ints{1, 2, 3, 4}, ints{3, 4, 5, 6}}, 3}, + {input{ints{1, 2, 3}, ints{2, 4}}, 2}, + {input{ints{1, 2, 3, 4}, ints{5, 6, 7, 8}}, -1}, + } { + io.WriteString(os.Stdout, cmp.Diff(data.input.process(), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-319/pokgopun/python/ch-1.py b/challenge-319/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..a13bfdd72e --- /dev/null +++ b/challenge-319/pokgopun/python/ch-1.py @@ -0,0 +1,50 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-319/ +""" + +Task 1: Word Count + +Submitted by: [46]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a list of words containing alphabetic characters only. + + Write a script to return the count of words either starting with a + vowel or ending with a vowel. + +Example 1 + +Input: @list = ("unicode", "xml", "raku", "perl") +Output: 2 + +The words are "unicode" and "raku". + +Example 2 + +Input: @list = ("the", "weekly", "challenge") +Output: 2 + +Example 3 + +Input: @list = ("perl", "python", "postgres") +Output: 0 + +Task 2: Minimum Common +""" +### solution by pokgopun@gmail.com + +def wc(words: tuple[str]) -> int: + v = "eaiou" + return sum(1 for w in words if w[0].lower() in v or w[-1].lower() in v) + +import unittest + +class TestWc(unittest.TestCase): + def test(self): + for inpt, otpt in { + ("unicode", "xml", "raku", "perl"): 2, + ("the", "weekly", "challenge"): 2, + ("perl", "python", "postgres"): 0, + }.items(): + self.assertEqual(wc(inpt), otpt) + +unittest.main() diff --git a/challenge-319/pokgopun/python/ch-2.py b/challenge-319/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..73493ca9bb --- /dev/null +++ b/challenge-319/pokgopun/python/ch-2.py @@ -0,0 +1,58 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-319/ +""" + +Task 2: Minimum Common + +Submitted by: [47]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given two arrays of integers. + + Write a script to return the minimum integer common to both arrays. If + none found return -1. + +Example 1 + +Input: @array_1 = (1, 2, 3, 4) + @array_2 = (3, 4, 5, 6) +Output: 3 + +The common integer in both arrays: 3, 4 +The minimum is 3. + +Example 2 + +Input: @array_1 = (1, 2, 3) + @array_2 = (2, 4) +Output: 2 + +Example 3 + +Input: @array_1 = (1, 2, 3, 4) + @array_2 = (5, 6, 7, 8) +Output: -1 + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 4th May 2025. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +def mc(arr1: tuple[int], arr2: tuple[int]) -> int: + cmn = set(arr1) & set(arr2) + return min(cmn) if len(cmn) > 0 else -1 + +import unittest + +class TestMc(unittest.TestCase): + def test(self): + for (arr1,arr2), otpt in { + ((1, 2, 3, 4),(3, 4, 5, 6)): 3, + ((1, 2, 3),(2, 4)): 2, + ((1, 2, 3, 4),(5, 6, 7, 8)): -1, + }.items(): + self.assertEqual(mc(arr1,arr2),otpt) + +unittest.main() |
