diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-11-18 09:54:08 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-11-18 09:54:08 +0000 |
| commit | f8206e786b5e5513a72f4254791fd6a1b841332d (patch) | |
| tree | e5dcf1eb69c11f0d3c525596e57803dc5df95bf9 | |
| parent | 2116b7ce0ae0edfc4375600c491a22e76c6dc485 (diff) | |
| parent | d7aa3e67c378f18a778610cb6c2d347a06dc58a4 (diff) | |
| download | perlweeklychallenge-club-f8206e786b5e5513a72f4254791fd6a1b841332d.tar.gz perlweeklychallenge-club-f8206e786b5e5513a72f4254791fd6a1b841332d.tar.bz2 perlweeklychallenge-club-f8206e786b5e5513a72f4254791fd6a1b841332d.zip | |
Merge pull request #13044 from pokgopun/pwc348
Pwc348
| -rw-r--r-- | challenge-348/pokgopun/go/ch-1.go | 98 | ||||
| -rw-r--r-- | challenge-348/pokgopun/go/ch-2.go | 109 | ||||
| -rw-r--r-- | challenge-348/pokgopun/lua/ch-1.lua | 90 | ||||
| -rw-r--r-- | challenge-348/pokgopun/lua/ch-2.lua | 114 | ||||
| -rw-r--r-- | challenge-348/pokgopun/python/ch-1.py | 79 | ||||
| -rw-r--r-- | challenge-348/pokgopun/python/ch-2.py | 101 |
6 files changed, 591 insertions, 0 deletions
diff --git a/challenge-348/pokgopun/go/ch-1.go b/challenge-348/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..ff985e941a --- /dev/null +++ b/challenge-348/pokgopun/go/ch-1.go @@ -0,0 +1,98 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-348/ +/*# + +Task 1: String Alike + +Submitted by: [49]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a string of even length. + + Write a script to find if the given string split into two halves of + equal lengths and they both have same number of vowels. + +Example 1 + +Input: $str = "textbook" +Output: false + +1st half: "text" (1 vowel) +2nd half: "book" (2 vowels) + +Example 2 + +Input: $str = "book" +Output: true + +1st half: "bo" (1 vowel) +2nd half: "ok" (1 vowel) + +Example 3 + +Input: $str = "AbCdEfGh" +Output: true + +1st half: "AbCd" (1 vowel) +2nd half: "EfGh" (1 vowel) + +Example 4 + +Input: $str = "rhythmmyth" +Output: false + +1st half: "rhyth" (0 vowel) +2nd half: "mmyth" (0 vowel) + +Example 5 + +Input: $str = "UmpireeAudio" +Output: false + +1st half: "Umpire" (3 vowels) +2nd half: "eAudio" (5 vowels) + +Task 2: Covert Time +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +func sa(str string) bool { + h := len(str) / 2 + var cnt [2]int + d := 'a' - 'A' + for i, rs := range []string{str[:h], str[h:]} { + for _, r := range rs { + if r < 'a' { + r += d + } + switch r { + case 'a', 'e', 'i', 'o', 'u': + cnt[i]++ + } + } + } + return cnt[0] > 0 && cnt[0] == cnt[1] +} + +func main() { + for _, data := range []struct { + input string + output bool + }{ + {"textbook", false}, + {"book", true}, + {"AbCdEfGh", true}, + {"rhythmmyth", false}, + {"UmpireeAudio", false}, + } { + io.WriteString(os.Stdout, cmp.Diff(sa(data.input), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-348/pokgopun/go/ch-2.go b/challenge-348/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..6c7808a6d1 --- /dev/null +++ b/challenge-348/pokgopun/go/ch-2.go @@ -0,0 +1,109 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-348/ +/*# + +Task 2: Covert Time + +Submitted by: [50]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given two strings, $source and $target, containing time in + 24-hour time form. + + Write a script to convert the source into target by performing one of + the following operations: +1. Add 1 minute +2. Add 5 minutes +3. Add 15 minutes +4. Add 60 minutes + + Find the total operations needed to get to the target. + +Example 1 + +Input: $source = "02:30" + $target = "02:45" +Output: 1 + +Just one operation i.e. "Add 15 minutes". + +Example 2 + +Input: $source = "11:55" + $target = "12:15" +Output: 2 + +Two operations i.e. "Add 15 minutes" followed by "Add 5 minutes". + +Example 3 + +Input: $source = "09:00" + $target = "13:00" +Output: 4 + +Four operations of "Add 60 minutes". + +Example 4 + +Input: $source = "23:45" + $target = "00:30" +Output: 3 + +Three operations of "Add 15 minutes". + +Example 5 + +Input: $source = "14:20" + $target = "15:25" +Output: 2 + +Two operations, one "Add 60 minutes" and one "Add 5 minutes" + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 23rd November + 2025. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + "time" + + "github.com/google/go-cmp/cmp" +) + +func ct(source, target string) int { + tmpl := "15:04" + t1, _ := time.Parse(tmpl, source) + t2, _ := time.Parse(tmpl, target) + if t2.Before(t1) { + t2 = t2.AddDate(0, 0, 1) + } + m := int(t2.Sub(t1).Minutes()) + c := 0 + for _, n := range []int{60, 15, 5} { + c += m / n + m = m % n + } + return c + m +} + +func main() { + for _, data := range []struct { + source, target string + output int + }{ + {"02:30", "02:45", 1}, + {"11:55", "12:15", 2}, + {"09:00", "13:00", 4}, + {"23:45", "00:30", 3}, + {"14:20", "15:25", 2}, + } { + io.WriteString(os.Stdout, cmp.Diff(ct(data.source, data.target), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-348/pokgopun/lua/ch-1.lua b/challenge-348/pokgopun/lua/ch-1.lua new file mode 100644 index 0000000000..da2e830838 --- /dev/null +++ b/challenge-348/pokgopun/lua/ch-1.lua @@ -0,0 +1,90 @@ +--# https://theweeklychallenge.org/blog/perl-weekly-challenge-348/ +--[[ + +Task 1: String Alike + +Submitted by: [49]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a string of even length. + + Write a script to find if the given string split into two halves of + equal lengths and they both have same number of vowels. + +Example 1 + +Input: $str = "textbook" +Output: false + +1st half: "text" (1 vowel) +2nd half: "book" (2 vowels) + +Example 2 + +Input: $str = "book" +Output: true + +1st half: "bo" (1 vowel) +2nd half: "ok" (1 vowel) + +Example 3 + +Input: $str = "AbCdEfGh" +Output: true + +1st half: "AbCd" (1 vowel) +2nd half: "EfGh" (1 vowel) + +Example 4 + +Input: $str = "rhythmmyth" +Output: false + +1st half: "rhyth" (0 vowel) +2nd half: "mmyth" (0 vowel) + +Example 5 + +Input: $str = "UmpireeAudio" +Output: false + +1st half: "Umpire" (3 vowels) +2nd half: "eAudio" (5 vowels) + +Task 2: Covert Time +--]] +--# solution by pokgopun@gmail.com + +--@params string +local function sa(str) --@return bool + str = string.lower(str) + local vwl = "aeiou" + local cnt1 = 0 + for i=1,#str/2 do + if string.match(vwl,string.sub(str,i,i)) then + cnt1 = cnt1 + 1 + end + end + local cnt2 = 0 + for i=1+#str/2,#str do + if string.match(vwl,string.sub(str,i,i)) then + cnt2 = cnt2 + 1 + end + end + return cnt1 > 0 and cnt1 == cnt2 +end + +local lu = require("luaunit") + +function TestSa() + for inpt, otpt in pairs({ + ["textbook"]= false, + ["book"]= true, + ["AbCdEfGh"]= true, + ["rhythmmyth"]= false, + ["UmpireeAudio"]= false}) do + lu.assertEquals(sa(inpt),otpt) + end +end + +lu.run() diff --git a/challenge-348/pokgopun/lua/ch-2.lua b/challenge-348/pokgopun/lua/ch-2.lua new file mode 100644 index 0000000000..036bd3fb6e --- /dev/null +++ b/challenge-348/pokgopun/lua/ch-2.lua @@ -0,0 +1,114 @@ +--# https://theweeklychallenge.org/blog/perl-weekly-challenge-348/ +--[[ + +Task 2: Covert Time + +Submitted by: [50]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given two strings, $source and $target, containing time in + 24-hour time form. + + Write a script to convert the source into target by performing one of + the following operations: +1. Add 1 minute +2. Add 5 minutes +3. Add 15 minutes +4. Add 60 minutes + + Find the total operations needed to get to the target. + +Example 1 + +Input: $source = "02:30" + $target = "02:45" +Output: 1 + +Just one operation i.e. "Add 15 minutes". + +Example 2 + +Input: $source = "11:55" + $target = "12:15" +Output: 2 + +Two operations i.e. "Add 15 minutes" followed by "Add 5 minutes". + +Example 3 + +Input: $source = "09:00" + $target = "13:00" +Output: 4 + +Four operations of "Add 60 minutes". + +Example 4 + +Input: $source = "23:45" + $target = "00:30" +Output: 3 + +Three operations of "Add 15 minutes". + +Example 5 + +Input: $source = "14:20" + $target = "15:25" +Output: 2 + +Two operations, one "Add 60 minutes" and one "Add 5 minutes" + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 23rd November + 2025. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +--]] +--# solution by pokgopun@gmail.com + +local function helper(timestr) + local m = 0 + local h2m = true + for t in string.gmatch(timestr,"%d+") do + t = tonumber(t) + if h2m then + t = t * 60 + h2m = false + end + m = m + t + end + return m +end + +--@params string, string +local function ct(source, target) --@return integer + local m1 = helper(source) + local m2 = helper(target) + if m2 < m1 then + m2 = m2 + 24*60 + end + m = m2 - m1 + c = 0 + for _, n in ipairs({60, 15, 5}) do + c = c + m // n + m = m % n + end + --print(source, target, m1, m2, m + c) + return c + m +end + +local lu = require("luaunit") + +function TestCt() + for _, data in ipairs({ + {source = "02:30",target = "02:45", Output= 1}, + {source = "11:55",target = "12:15",Output= 2}, + {source = "09:00",target = "13:00",Output= 4}, + {source = "23:45",target = "00:30",Output= 3}, + {source = "14:20",target = "15:25",Output= 2}}) do + lu.assertEquals(ct(data.source,data.target), data.Output) + end +end + +lu.run() diff --git a/challenge-348/pokgopun/python/ch-1.py b/challenge-348/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..85dbe46cb2 --- /dev/null +++ b/challenge-348/pokgopun/python/ch-1.py @@ -0,0 +1,79 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-348/ +""" + +Task 1: String Alike + +Submitted by: [49]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a string of even length. + + Write a script to find if the given string split into two halves of + equal lengths and they both have same number of vowels. + +Example 1 + +Input: $str = "textbook" +Output: false + +1st half: "text" (1 vowel) +2nd half: "book" (2 vowels) + +Example 2 + +Input: $str = "book" +Output: true + +1st half: "bo" (1 vowel) +2nd half: "ok" (1 vowel) + +Example 3 + +Input: $str = "AbCdEfGh" +Output: true + +1st half: "AbCd" (1 vowel) +2nd half: "EfGh" (1 vowel) + +Example 4 + +Input: $str = "rhythmmyth" +Output: false + +1st half: "rhyth" (0 vowel) +2nd half: "mmyth" (0 vowel) + +Example 5 + +Input: $str = "UmpireeAudio" +Output: false + +1st half: "Umpire" (3 vowels) +2nd half: "eAudio" (5 vowels) + +Task 2: Covert Time +""" +### solution by pokgopun@gmail.com + +def sa(string: str) -> bool: + string = string.lower() + h = len(string) // 2 + vwl = "aeiou" + cnt1 = sum(1 if e in vwl else 0 for e in string[:h]) + cnt2 = sum(1 if e in vwl else 0 for e in string[h:]) + return cnt1 > 0 and cnt1 == cnt2 + +import unittest + +class TestSa(unittest.TestCase): + def test(self): + for inpt, otpt in { + "textbook": False, + "book": True, + "AbCdEfGh": True, + "rhythmmyth": False, + "UmpireeAudio": False, + }.items(): + self.assertEqual(sa(inpt), otpt) + +unittest.main() diff --git a/challenge-348/pokgopun/python/ch-2.py b/challenge-348/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..5124a6c2b0 --- /dev/null +++ b/challenge-348/pokgopun/python/ch-2.py @@ -0,0 +1,101 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-348/ +""" + +Task 2: Covert Time + +Submitted by: [50]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given two strings, $source and $target, containing time in + 24-hour time form. + + Write a script to convert the source into target by performing one of + the following operations: +1. Add 1 minute +2. Add 5 minutes +3. Add 15 minutes +4. Add 60 minutes + + Find the total operations needed to get to the target. + +Example 1 + +Input: $source = "02:30" + $target = "02:45" +Output: 1 + +Just one operation i.e. "Add 15 minutes". + +Example 2 + +Input: $source = "11:55" + $target = "12:15" +Output: 2 + +Two operations i.e. "Add 15 minutes" followed by "Add 5 minutes". + +Example 3 + +Input: $source = "09:00" + $target = "13:00" +Output: 4 + +Four operations of "Add 60 minutes". + +Example 4 + +Input: $source = "23:45" + $target = "00:30" +Output: 3 + +Three operations of "Add 15 minutes". + +Example 5 + +Input: $source = "14:20" + $target = "15:25" +Output: 2 + +Two operations, one "Add 60 minutes" and one "Add 5 minutes" + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 23rd November + 2025. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +from datetime import datetime + +def ct(source: str, target: str) -> int: + #print(source,target) + timefmt = "%H:%M" + s = datetime.strptime(source, timefmt) + t = datetime.strptime(target, timefmt) + d = (t - s) + m = d.total_seconds()/60 + if m < 0: + m += 24*60 + #print(m) + c = 0 + for dmn in (60,15,5): + c += m // dmn + m %= dmn + return c + m + +import unittest + +class TestCt(unittest.TestCase): + def test(self): + for (source, target), otpt in { + ("02:30", "02:45"): 1, + ("11:55", "12:15"): 2, + ("09:00", "13:00"): 4, + ("23:45", "00:30"): 3, + ("14:20", "15:25"): 2, + }.items(): + self.assertEqual(ct(source,target),otpt) + +unittest.main() |
