diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-07-08 14:18:13 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-08 14:18:13 +0100 |
| commit | 296f786d64c8e1d2902fcea6c4590ffb93ee6f08 (patch) | |
| tree | 06844c0cf31651f8b745d0204785ac59135e995d | |
| parent | 996112d2e3daa2be2425e08aec3ce172f71b841b (diff) | |
| parent | cd47e4b155a5c72586de6d9e4e39fc5301becb9a (diff) | |
| download | perlweeklychallenge-club-296f786d64c8e1d2902fcea6c4590ffb93ee6f08.tar.gz perlweeklychallenge-club-296f786d64c8e1d2902fcea6c4590ffb93ee6f08.tar.bz2 perlweeklychallenge-club-296f786d64c8e1d2902fcea6c4590ffb93ee6f08.zip | |
Merge pull request #10389 from pokgopun/pwc277
pwc277
| -rw-r--r-- | challenge-277/pokgopun/go/ch-1.go | 97 | ||||
| -rw-r--r-- | challenge-277/pokgopun/go/ch-2.go | 89 | ||||
| -rw-r--r-- | challenge-277/pokgopun/python/ch-1.py | 62 | ||||
| -rw-r--r-- | challenge-277/pokgopun/python/ch-2.py | 62 |
4 files changed, 310 insertions, 0 deletions
diff --git a/challenge-277/pokgopun/go/ch-1.go b/challenge-277/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..a8b499055d --- /dev/null +++ b/challenge-277/pokgopun/go/ch-1.go @@ -0,0 +1,97 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-277/ +/*# + +Task 1: Count Common + +Submitted by: [53]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given two array of strings, @words1 and @words2. + + Write a script to return the count of words that appears in both arrays + exactly once. + +Example 1 + +Input: @words1 = ("Perl", "is", "my", "friend") + @words2 = ("Perl", "and", "Raku", "are", "friend") +Output: 2 + +The words "Perl" and "friend" appear once in each array. + +Example 2 + +Input: @words1 = ("Perl", "and", "Python", "are", "very", "similar") + @words2 = ("Python", "is", "top", "in", "guest", "languages") +Output: 1 + +Example 3 + +Input: @words1 = ("Perl", "is", "imperative", "Lisp", "is", "functional") + @words2 = ("Crystal", "is", "similar", "to", "Ruby") +Output: 0 + +Task 2: Strong Pair +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +type words []string + +func (wd words) removeMultiple() words { + m := make(map[string]int) + for _, v := range wd { + m[v]++ + } + var r words + for k, v := range m { + if v > 1 { + continue + } + r = append(r, k) + } + return r +} + +func (wd words) remainMultiple() words { + m := make(map[string]int) + for _, v := range wd { + m[v]++ + } + var r words + for k, v := range m { + if v > 1 { + r = append(r, k) + } + } + return r +} + +func countCommon(words1, words2 words) int { + return len( + append( + words1.removeMultiple(), words2.removeMultiple()..., + ).remainMultiple(), + ) +} + +func main() { + for _, data := range []struct { + words1, words2 words + count int + }{ + {words{"Perl", "is", "my", "friend"}, words{"Perl", "and", "Raku", "are", "friend"}, 2}, + {words{"Perl", "and", "Python", "are", "very", "similar"}, words{"Python", "is", "top", "in", "guest", "languages"}, 1}, + {words{"Perl", "is", "imperative", "Lisp", "is", "functional"}, words{"Crystal", "is", "similar", "to", "Ruby"}, 0}, + } { + io.WriteString(os.Stdout, cmp.Diff(countCommon(data.words1, data.words2), data.count)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-277/pokgopun/go/ch-2.go b/challenge-277/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..277965f895 --- /dev/null +++ b/challenge-277/pokgopun/go/ch-2.go @@ -0,0 +1,89 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-277/ +/*# + +Task 2: Strong Pair + +Submitted by: [54]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of integers, @ints. + + Write a script to return the count of all strong pairs in the given + array. + + A pair of integers x and y is called strong pair if it satisfies: 0 + < |x - y| < min(x, y). + +Example 1 + +Input: @ints = (1, 2, 3, 4, 5) +Ouput: 4 + +Strong Pairs: (2, 3), (3, 4), (3, 5), (4, 5) + +Example 2 + +Input: @ints = (5, 7, 1, 7) +Ouput: 1 + +Strong Pairs: (5, 7) + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 14th July 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +type ints []int + +func (is ints) removeDuplicate() ints { + m := make(map[int]int) + for _, v := range is { + m[v]++ + } + var r ints + for k := range m { + r = append(r, k) + } + return r +} + +func strongPair(is ints) int { + is = is.removeDuplicate() + c := 0 + l := len(is) + for i := 0; i < l-1; i++ { + for j := i + 1; j < l; j++ { + if is[i] == is[j] { + continue + } + if max(is[i], is[j]) < 2*min(is[i], is[j]) { + c++ + } + } + } + return c +} + +func main() { + for _, data := range []struct { + ints ints + count int + }{ + {ints{1, 2, 3, 4, 5}, 4}, + {ints{5, 7, 1, 7}, 1}, + } { + io.WriteString(os.Stdout, cmp.Diff(strongPair(data.ints), data.count)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-277/pokgopun/python/ch-1.py b/challenge-277/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..bd99b9d013 --- /dev/null +++ b/challenge-277/pokgopun/python/ch-1.py @@ -0,0 +1,62 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-277/ +""" + +Task 1: Count Common + +Submitted by: [53]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given two array of strings, @words1 and @words2. + + Write a script to return the count of words that appears in both arrays + exactly once. + +Example 1 + +Input: @words1 = ("Perl", "is", "my", "friend") + @words2 = ("Perl", "and", "Raku", "are", "friend") +Output: 2 + +The words "Perl" and "friend" appear once in each array. + +Example 2 + +Input: @words1 = ("Perl", "and", "Python", "are", "very", "similar") + @words2 = ("Python", "is", "top", "in", "guest", "languages") +Output: 1 + +Example 3 + +Input: @words1 = ("Perl", "is", "imperative", "Lisp", "is", "functional") + @words2 = ("Crystal", "is", "similar", "to", "Ruby") +Output: 0 + +Task 2: Strong Pair +""" +### solution by pokgopun@gmail.com + +def countCommon(words1, words2): + return len( + set( + e for e in set(words1) if words1.count(e)==1 + ).intersection( + set( + e for e in set(words2) if words2.count(e)==1 + ) + ) + ) + +import unittest + +class TestCountCommon(unittest.TestCase): + def test(self): + for (words1, words2), cc in { + (("Perl", "is", "my", "friend"),("Perl", "and", "Raku", "are", "friend")): 2, + (("Perl", "and", "Python", "are", "very", "similar"),("Python", "is", "top", "in", "guest", "languages")): 1, + (("Perl", "is", "imperative", "Lisp", "is", "functional"),("Crystal", "is", "similar", "to", "Ruby")): 0, + }.items(): + self.assertEqual(countCommon(words1,words2),cc) + +unittest.main() + + diff --git a/challenge-277/pokgopun/python/ch-2.py b/challenge-277/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..2da0dfe4c3 --- /dev/null +++ b/challenge-277/pokgopun/python/ch-2.py @@ -0,0 +1,62 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-277/ +""" + +Task 2: Strong Pair + +Submitted by: [54]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of integers, @ints. + + Write a script to return the count of all strong pairs in the given + array. + + A pair of integers x and y is called strong pair if it satisfies: 0 + < |x - y| < min(x, y). + +Example 1 + +Input: @ints = (1, 2, 3, 4, 5) +Ouput: 4 + +Strong Pairs: (2, 3), (3, 4), (3, 5), (4, 5) + +Example 2 + +Input: @ints = (5, 7, 1, 7) +Ouput: 1 + +Strong Pairs: (5, 7) + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 14th July 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +def strongPair(ints): + ints = tuple(set(ints)) + c = 0 + l = len(ints) + for i in range(l-1): + for j in range(i+1,l): + x, y = ints[i], ints[j] + if x == y: + continue + if max(x,y) < 2*min(x,y): + c += 1 + return c + +import unittest + +class TestStrongPair(unittest.TestCase): + def test(self): + for ints, count in { + (1, 2, 3, 4, 5): 4, + (5, 7, 1, 7): 1, + }.items(): + self.assertEqual(strongPair(ints),count) + +unittest.main() |
