diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-02-05 19:01:36 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-02-05 19:01:36 +0000 |
| commit | aef0e4bc11aade61951ef67cae0e07c176b1b860 (patch) | |
| tree | 40453c71f1d22ad5852f4487453ff435e5f6e1e3 /challenge-255 | |
| parent | 261ffda2e5ef595efd76b5692a7fcc7a5fe39a8a (diff) | |
| parent | 9e3bdff8884004b83a1a49209c58e8c966ad413a (diff) | |
| download | perlweeklychallenge-club-aef0e4bc11aade61951ef67cae0e07c176b1b860.tar.gz perlweeklychallenge-club-aef0e4bc11aade61951ef67cae0e07c176b1b860.tar.bz2 perlweeklychallenge-club-aef0e4bc11aade61951ef67cae0e07c176b1b860.zip | |
Merge pull request #9521 from pokgopun/pwc255
pwc255 solution
Diffstat (limited to 'challenge-255')
| -rw-r--r-- | challenge-255/pokgopun/go/ch-1.go | 70 | ||||
| -rw-r--r-- | challenge-255/pokgopun/go/ch-2.go | 88 | ||||
| -rw-r--r-- | challenge-255/pokgopun/python/ch-1.py | 49 | ||||
| -rw-r--r-- | challenge-255/pokgopun/python/ch-2.py | 61 |
4 files changed, 268 insertions, 0 deletions
diff --git a/challenge-255/pokgopun/go/ch-1.go b/challenge-255/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..ddba3637e9 --- /dev/null +++ b/challenge-255/pokgopun/go/ch-1.go @@ -0,0 +1,70 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-255/ +/*# + +Task 1: Odd Character + +Submitted by: [46]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given two strings, $s and $t. The string $t is generated using + the shuffled characters of the string $s with an additional character. + + Write a script to find the additional character in the string $t.. + +Example 1 + +Input: $s = "Perl" $t = "Preel" +Output: "e" + +Example 2 + +Input: $s = "Weekly" $t = "Weeakly" +Output: "a" + +Example 3 + +Input: $s = "Box" $t = "Boxy" +Output: "y" + +Task 2: Most Frequent Word +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +type str string + +func (st str) oddChar(s str) rune { + m := make(map[rune]int) + for _, v := range st { + m[v]++ + } + for _, v := range s { + if m[v] > 0 { + m[v]-- + } else { + return v + } + } + return 0 +} + +func main() { + for _, data := range []struct { + s, t str + o rune + }{ + {"Perl", "Preel", 'e'}, + {"Weekly", "Weeakly", 'a'}, + {"Box", "Boxy", 'y'}, + } { + io.WriteString(os.Stdout, cmp.Diff(data.s.oddChar(data.t), data.o)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-255/pokgopun/go/ch-2.go b/challenge-255/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..127a62dadc --- /dev/null +++ b/challenge-255/pokgopun/go/ch-2.go @@ -0,0 +1,88 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-255/ +/*# + +Task 2: Most Frequent Word + +Submitted by: [47]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a paragraph $p and a banned word $w. + + Write a script to return the most frequent word that is not banned. + +Example 1 + +Input: $p = "Joe hit a ball, the hit ball flew far after it was hit." + $w = "hit" +Output: "ball" + +The banned word "hit" occurs 3 times. +The other word "ball" occurs 2 times. + +Example 2 + +Input: $p = "Perl and Raku belong to the same family. Perl is the most popular l +anguage in the weekly challenge." + $w = "the" +Output: "Perl" + +The banned word "the" occurs 3 times. +The other word "Perl" occurs 2 times. + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 11th February + 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + "regexp" + + "github.com/google/go-cmp/cmp" +) + +func mostFreqWord(p, w string) string { + re := regexp.MustCompile(`\w+`) + m := make(map[string]int) + var ( + i, mx int + loc []int + r, v string + ) + l := len(p) + for l-i > 1 { + loc = re.FindStringIndex(p[i:]) + if loc == nil { + break + } + v = p[loc[0]+i : loc[1]+i] + i += loc[1] + if v == w { + continue + } + m[v]++ + if m[v] > mx { + mx = m[v] + r = v + } + } + return r +} + +func main() { + for _, data := range []struct { + p, w, o string + }{ + {"Joe hit a ball, the hit ball flew far after it was hit.", "hit", "ball"}, + {"Perl and Raku belong to the same family. Perl is the most popular language in the weekly challenge.", "the", "Perl"}, + } { + io.WriteString(os.Stdout, cmp.Diff(mostFreqWord(data.p, data.w), data.o)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-255/pokgopun/python/ch-1.py b/challenge-255/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..c48ab19023 --- /dev/null +++ b/challenge-255/pokgopun/python/ch-1.py @@ -0,0 +1,49 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-255/ +""" + +Task 1: Odd Character + +Submitted by: [46]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given two strings, $s and $t. The string $t is generated using + the shuffled characters of the string $s with an additional character. + + Write a script to find the additional character in the string $t.. + +Example 1 + +Input: $s = "Perl" $t = "Preel" +Output: "e" + +Example 2 + +Input: $s = "Weekly" $t = "Weeakly" +Output: "a" + +Example 3 + +Input: $s = "Box" $t = "Boxy" +Output: "y" + +Task 2: Most Frequent Word +""" +### solution by pokgopun@gmail.com + +def oddChar(s,t: str): + for c in s: + t = t.replace(c,"",1) + return t + +import unittest + +class TestOddChar(unittest.TestCase): + def test(self): + for (s,t), otpt in { + ("Perl","Preel"): "e", + ("Weekly","Weeakly"): "a", + ("Box","Boxy"): "y", + }.items(): + self.assertEqual(oddChar(s,t), otpt) + +unittest.main() diff --git a/challenge-255/pokgopun/python/ch-2.py b/challenge-255/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..8a936ca3f2 --- /dev/null +++ b/challenge-255/pokgopun/python/ch-2.py @@ -0,0 +1,61 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-255/ +""" + +Task 2: Most Frequent Word + +Submitted by: [47]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a paragraph $p and a banned word $w. + + Write a script to return the most frequent word that is not banned. + +Example 1 + +Input: $p = "Joe hit a ball, the hit ball flew far after it was hit." + $w = "hit" +Output: "ball" + +The banned word "hit" occurs 3 times. +The other word "ball" occurs 2 times. + +Example 2 + +Input: $p = "Perl and Raku belong to the same family. Perl is the most popular l +anguage in the weekly challenge." + $w = "the" +Output: "Perl" + +The banned word "the" occurs 3 times. +The other word "Perl" occurs 2 times. + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 11th February + 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +import re + +def mostFreqWord(p,w: str): + dct = dict() + for s in ( e.group(0) for e in re.finditer(r'\w+',p) if e.group(0) != w ): + dct[s] = dct.setdefault(s,0) + 1 + mx = max(dct.values()) + for e in dct.items(): + if e[1] == mx: return e[0] + +import unittest + +class TestMostFreqWord(unittest.TestCase): + def test(self): + for (p,w), otpt in { + ("Joe hit a ball, the hit ball flew far after it was hit.","hit"): "ball", + ("Perl and Raku belong to the same family. Perl is the most popular language in the weekly challenge.","the"): "Perl", + }.items(): + self.assertEqual(mostFreqWord(p,w),otpt) + +unittest.main() |
