diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-07-22 00:24:09 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-22 00:24:09 +0100 |
| commit | 73724104536fa40079f4d32e9237ca2148f9c92b (patch) | |
| tree | ebac0d780ede5ca19c62aa1bdccf74f332da5599 | |
| parent | 3b9d323057607e41cb23a9c35149cf91bbdc902a (diff) | |
| parent | 6fa4b220680e4136652dbbda5472e87bff7c6147 (diff) | |
| download | perlweeklychallenge-club-73724104536fa40079f4d32e9237ca2148f9c92b.tar.gz perlweeklychallenge-club-73724104536fa40079f4d32e9237ca2148f9c92b.tar.bz2 perlweeklychallenge-club-73724104536fa40079f4d32e9237ca2148f9c92b.zip | |
Merge pull request #12386 from pokgopun/pwc331
Pwc331
| -rw-r--r-- | challenge-331/pokgopun/go/ch-1.go | 58 | ||||
| -rw-r--r-- | challenge-331/pokgopun/go/ch-2.go | 105 | ||||
| -rw-r--r-- | challenge-331/pokgopun/python/ch-1.py | 49 | ||||
| -rw-r--r-- | challenge-331/pokgopun/python/ch-2.py | 81 |
4 files changed, 293 insertions, 0 deletions
diff --git a/challenge-331/pokgopun/go/ch-1.go b/challenge-331/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..eed78fe900 --- /dev/null +++ b/challenge-331/pokgopun/go/ch-1.go @@ -0,0 +1,58 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-331/ +/*# + +Task 1: Last Word + +Submitted by: [43]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a string. + + Write a script to find the length of last word in the given string. + +Example 1 + +Input: $str = "The Weekly Challenge" +Output: 9 + +Example 2 + +Input: $str = " Hello World " +Output: 5 + +Example 3 + +Input: $str = "Let's begin the fun" +Output: 3 + +Task 2: Buddy Strings +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + "regexp" + + "github.com/google/go-cmp/cmp" +) + +func lw(str string) int { + idx := regexp.MustCompile(`(\S+)\s*$`).FindStringSubmatchIndex(str) + return idx[3] - idx[2] +} + +func main() { + for _, data := range []struct { + input string + output int + }{ + {"The Weekly Challenge", 9}, + {" Hello World ", 5}, + {"Let's begin the fun", 3}, + } { + io.WriteString(os.Stdout, cmp.Diff(lw(data.input), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-331/pokgopun/go/ch-2.go b/challenge-331/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..c1a735e3bd --- /dev/null +++ b/challenge-331/pokgopun/go/ch-2.go @@ -0,0 +1,105 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-331/ +/*# + +Task 2: Buddy Strings + +Submitted by: [44]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given two strings, source and target. + + Write a script to find out if the given strings are Buddy Strings. +If swapping of a letter in one string make them same as the other then they are +`Buddy Strings`. + +Example 1 + +Input: $source = "fuck" + $target = "fcuk" +Output: true + +The swapping of 'u' with 'c' makes it buddy strings. + +Example 2 + +Input: $source = "love" + $target = "love" +Output: false + +Example 3 + +Input: $source = "fodo" + $target = "food" +Output: true + +Example 4 + +Input: $source = "feed" + $target = "feed" +Output: true + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 27th July 2025. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + "slices" + + "github.com/google/go-cmp/cmp" +) + +type input struct { + source, target string +} + +func (in input) process() bool { + l := len(in.source) + if l != len(in.target) { + return false + } + s := []byte(in.source) + slices.Sort(s) + s = slices.Compact(s) + if in.source == in.target { + if l != len(s) { + return true + } else { + return false + } + } + var swp []byte + for i := range l { + a, b := in.source[i], in.target[i] + if a != b { + swp = append(swp, a, b) + } + if len(swp) == 4 { + if swp[0] == swp[3] && swp[1] == swp[2] { + return in.source[i+1:] == in.target[i+1:] + } + } + } + return false +} + +func main() { + for _, data := range []struct { + input input + output bool + }{ + {input{"fuck", "fcuk"}, true}, + {input{"love", "love"}, false}, + {input{"fodo", "food"}, true}, + {input{"feed", "feed"}, true}, + } { + io.WriteString(os.Stdout, cmp.Diff(data.input.process(), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-331/pokgopun/python/ch-1.py b/challenge-331/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..05f3f308df --- /dev/null +++ b/challenge-331/pokgopun/python/ch-1.py @@ -0,0 +1,49 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-331/ +""" + +Task 1: Last Word + +Submitted by: [43]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a string. + + Write a script to find the length of last word in the given string. + +Example 1 + +Input: $str = "The Weekly Challenge" +Output: 9 + +Example 2 + +Input: $str = " Hello World " +Output: 5 + +Example 3 + +Input: $str = "Let's begin the fun" +Output: 3 + +Task 2: Buddy Strings +""" +### solution by pokgopun@gmail.com + +import re + +def lw(string: str) -> str: + m = re.search(r'(\S+)\s*$', string) + return m.end(1) - m.start(1) + +import unittest + +class TestLw(unittest.TestCase): + def test(self): + for inpt, otpt in { + "The Weekly Challenge": 9, + " Hello World ": 5, + "Let's begin the fun": 3, + }.items(): + self.assertEqual(lw(inpt),otpt) + +unittest.main() diff --git a/challenge-331/pokgopun/python/ch-2.py b/challenge-331/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..22c61d9240 --- /dev/null +++ b/challenge-331/pokgopun/python/ch-2.py @@ -0,0 +1,81 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-331/ +""" + +Task 2: Buddy Strings + +Submitted by: [44]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given two strings, source and target. + + Write a script to find out if the given strings are Buddy Strings. +If swapping of a letter in one string make them same as the other then they are +`Buddy Strings`. + +Example 1 + +Input: $source = "fuck" + $target = "fcuk" +Output: true + +The swapping of 'u' with 'c' makes it buddy strings. + +Example 2 + +Input: $source = "love" + $target = "love" +Output: false + +Example 3 + +Input: $source = "fodo" + $target = "food" +Output: true + +Example 4 + +Input: $source = "feed" + $target = "feed" +Output: true + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 27th July 2025. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +def bs(s: str, t: str) -> bool: + l = len(s) + if l != len(t): + return False + if s == t: + if l != len(set(s)): + return True + else: + return False + lst: list[str] = [] + for i in range(l): + a, b = s[i], t[i] + if a != b: + lst.append(a) + lst.append(b) + if len(lst) == 4: + if lst[0] == lst[3] and lst[1] == lst[2]: + return s[i+1:] == t[i+1:] + return False + +import unittest + +class TestBs(unittest.TestCase): + def test(self): + for (source, target), otpt in { + ("fuck", "fcuk"): True, + ("love", "love"): False, + ("fodo", "food"): True, + ("feed", "feed"): True, + }.items(): + self.assertEqual(bs(source,target),otpt) + +unittest.main() |
