diff --git a/challenge-347/pokgopun/go/ch-1.go b/challenge-347/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..f3dca081eb --- /dev/null +++ b/challenge-347/pokgopun/go/ch-1.go @@ -0,0 +1,78 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-347/ +/*# + +Task 1: Format Date + +Submitted by: [44]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a date in the form: 10th Nov 2025. + + Write a script to format the given date in the form: 2025-11-10 using + the set below. +@DAYS = ("1st", "2nd", "3rd", ....., "30th", "31st") +@MONTHS = ("Jan", "Feb", "Mar", ....., "Nov", "Dec") +@YEARS = (1900..2100) + +Example 1 + +Input: $str = "1st Jan 2025" +Output: "2025-01-01" + +Example 2 + +Input: $str = "22nd Feb 2025" +Output: "2025-02-22" + +Example 3 + +Input: $str = "15th Apr 2025" +Output: "2025-04-15" + +Example 4 + +Input: $str = "23rd Oct 2025" +Output: "2025-10-23" + +Example 5 + +Input: $str = "31st Dec 2025" +Output: "2025-12-31" + +Task 2: Format Phone Number +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "fmt" + "io" + "os" + "slices" + "strconv" + "strings" + + "github.com/google/go-cmp/cmp" +) + +func fd(str string) string { + m := []string{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"} + d := strings.Split(str, " ") + day, _ := strconv.Atoi(d[0][:len(d[0])-2]) + return fmt.Sprintf("%s-%02d-%02d", d[2], slices.Index(m, d[1])+1, day) +} + +func main() { + for _, data := range []struct { + input, output string + }{ + {"1st Jan 2025", "2025-01-01"}, + {"22nd Feb 2025", "2025-02-22"}, + {"15th Apr 2025", "2025-04-15"}, + {"23rd Oct 2025", "2025-10-23"}, + {"31st Dec 2025", "2025-12-31"}, + } { + io.WriteString(os.Stdout, cmp.Diff(fd(data.input), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-347/pokgopun/go/ch-2.go b/challenge-347/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..7fdb7c8ed5 --- /dev/null +++ b/challenge-347/pokgopun/go/ch-2.go @@ -0,0 +1,99 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-347/ +/*# + +Task 2: Format Phone Number + +Submitted by: [45]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a phone number as a string containing digits, space and + dash only. + + Write a script to format the given phone number using the below rules: +1. Removing all spaces and dashes +2. Grouping digits into blocks of length 3 from left to right +3. Handling the final digits (4 or fewer) specially: + - 2 digits: one block of length 2 + - 3 digits: one block of length 3 + - 4 digits: two blocks of length 2 +4. Joining all blocks with dashes + +Example 1 + +Input: $phone = "1-23-45-6" +Output: "123-456" + +Example 2 + +Input: $phone = "1234" +Output: "12-34" + +Example 3 + +Input: $phone = "12 345-6789" +Output: "123-456-789" + +Example 4 + +Input: $phone = "123 4567" +Output: "123-45-67" + +Example 5 + +Input: $phone = "123 456-78" +Output: "123-456-78" + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 16th November + 2025. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +func fpn(str string) string { + var rs []rune + var i, j, k int + for _, v := range str { + if v < '0' || v > '9' { + continue + } + if i > 0 && i%3 == 0 { + rs = append(rs, '-') + j++ + k = j + } + rs = append(rs, v) + i++ + j++ + } + //fmt.Println(i, j, k) + if j-k == 1 { + rs[k-1], rs[k-2] = rs[k-2], rs[k-1] + } + return string(rs) +} + +func main() { + for _, data := range []struct { + input, output string + }{ + {"1-23-45-6", "123-456"}, + {"1234", "12-34"}, + {"12 345-6789", "123-456-789"}, + {"123 4567", "123-45-67"}, + {"123 456-78", "123-456-78"}, + } { + io.WriteString(os.Stdout, cmp.Diff(fpn(data.input), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-347/pokgopun/lua/ch-1.lua b/challenge-347/pokgopun/lua/ch-1.lua new file mode 100644 index 0000000000..35a7066807 --- /dev/null +++ b/challenge-347/pokgopun/lua/ch-1.lua @@ -0,0 +1,80 @@ +--# https://theweeklychallenge.org/blog/perl-weekly-challenge-347/ +--[[ + +Task 1: Format Date + +Submitted by: [44]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a date in the form: 10th Nov 2025. + + Write a script to format the given date in the form: 2025-11-10 using + the set below. +@DAYS = ("1st", "2nd", "3rd", ....., "30th", "31st") +@MONTHS = ("Jan", "Feb", "Mar", ....., "Nov", "Dec") +@YEARS = (1900..2100) + +Example 1 + +Input: $str = "1st Jan 2025" +Output: "2025-01-01" + +Example 2 + +Input: $str = "22nd Feb 2025" +Output: "2025-02-22" + +Example 3 + +Input: $str = "15th Apr 2025" +Output: "2025-04-15" + +Example 4 + +Input: $str = "23rd Oct 2025" +Output: "2025-10-23" + +Example 5 + +Input: $str = "31st Dec 2025" +Output: "2025-12-31" + +Task 2: Format Phone Number +--]] +--# solution by pokgopun@gmail.com + + +function fd(str) + local m = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"} + local date = "" + local idx = 0 + for d in string.gmatch(str, "%S+") do + idx = idx + 1 + if idx == 1 then + d = "-"..string.format("%02d", tonumber(string.sub(d, 1, #d-2))) + elseif idx == 2 then + for i, v in ipairs(m) do + if d == v then + d = "-"..string.format("%02d", i) + end + end + end + date = d .. date + end + return date +end + +local lu = require("luaunit") + +function TestFd() + for inpt, otpt in pairs({ + ["1st Jan 2025"] = "2025-01-01", + ["22nd Feb 2025"] = "2025-02-22", + ["15th Apr 2025"] = "2025-04-15", + ["23rd Oct 2025"]= "2025-10-23", + ["31st Dec 2025"]= "2025-12-31"}) do + lu.assertEquals(fd(inpt), otpt) + end +end + +lu.run() diff --git a/challenge-347/pokgopun/lua/ch-2.lua b/challenge-347/pokgopun/lua/ch-2.lua new file mode 100644 index 0000000000..cc8c9f67ad --- /dev/null +++ b/challenge-347/pokgopun/lua/ch-2.lua @@ -0,0 +1,85 @@ +--# https://theweeklychallenge.org/blog/perl-weekly-challenge-347/ +--[[ + +Task 2: Format Phone Number + +Submitted by: [45]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a phone number as a string containing digits, space and + dash only. + + Write a script to format the given phone number using the below rules: +1. Removing all spaces and dashes +2. Grouping digits into blocks of length 3 from left to right +3. Handling the final digits (4 or fewer) specially: + - 2 digits: one block of length 2 + - 3 digits: one block of length 3 + - 4 digits: two blocks of length 2 +4. Joining all blocks with dashes + +Example 1 + +Input: $phone = "1-23-45-6" +Output: "123-456" + +Example 2 + +Input: $phone = "1234" +Output: "12-34" + +Example 3 + +Input: $phone = "12 345-6789" +Output: "123-456-789" + +Example 4 + +Input: $phone = "123 4567" +Output: "123-45-67" + +Example 5 + +Input: $phone = "123 456-78" +Output: "123-456-78" + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 16th November + 2025. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +--]] +--# solution by pokgopun@gmail.com + +function fpn(phone) + local dgt = {} + for v in string.gmatch(phone,"%d") do + table.insert(dgt,v) + end + local l = #dgt + local i = 4 + while i <= l do + if i == l then + i = i - 1 + end + dgt[i] = "-"..dgt[i] + i = i + 3 + end + return table.concat(dgt,"") +end + +local lu = require("luaunit") + +function TestFpn() + for inpt, otpt in pairs({ + ["1-23-45-6"]= "123-456", + ["1234"]= "12-34", + ["12 345-6789"]= "123-456-789", + ["123 4567"] = "123-45-67", + ["123 456-78"] = "123-456-78"}) do + lu.assertEquals(fpn(inpt), otpt) + end +end + +lu.run() diff --git a/challenge-347/pokgopun/python/ch-1.py b/challenge-347/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..c115301366 --- /dev/null +++ b/challenge-347/pokgopun/python/ch-1.py @@ -0,0 +1,66 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-347/ +""" + +Task 1: Format Date + +Submitted by: [44]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a date in the form: 10th Nov 2025. + + Write a script to format the given date in the form: 2025-11-10 using + the set below. +@DAYS = ("1st", "2nd", "3rd", ....., "30th", "31st") +@MONTHS = ("Jan", "Feb", "Mar", ....., "Nov", "Dec") +@YEARS = (1900..2100) + +Example 1 + +Input: $str = "1st Jan 2025" +Output: "2025-01-01" + +Example 2 + +Input: $str = "22nd Feb 2025" +Output: "2025-02-22" + +Example 3 + +Input: $str = "15th Apr 2025" +Output: "2025-04-15" + +Example 4 + +Input: $str = "23rd Oct 2025" +Output: "2025-10-23" + +Example 5 + +Input: $str = "31st Dec 2025" +Output: "2025-12-31" + +Task 2: Format Phone Number +""" +### solution by pokgopun@gmail.com + + +MONTHS = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec") + +def fd(string: str) -> str: + d = string.split(" ") + return "{}-{:02d}-{:02d}".format(d[2], MONTHS.index(d[1])+1, int(d[0][:-2])) + +import unittest + +class TestFd(unittest.TestCase): + def test(self): + for inpt, otpt in { + "1st Jan 2025": "2025-01-01", + "22nd Feb 2025": "2025-02-22", + "15th Apr 2025": "2025-04-15", + "23rd Oct 2025": "2025-10-23", + "31st Dec 2025": "2025-12-31", + }.items(): + self.assertEqual(fd(inpt), otpt) + +unittest.main() diff --git a/challenge-347/pokgopun/python/ch-2.py b/challenge-347/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..4854f427e4 --- /dev/null +++ b/challenge-347/pokgopun/python/ch-2.py @@ -0,0 +1,81 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-347/ +""" + +Task 2: Format Phone Number + +Submitted by: [45]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a phone number as a string containing digits, space and + dash only. + + Write a script to format the given phone number using the below rules: +1. Removing all spaces and dashes +2. Grouping digits into blocks of length 3 from left to right +3. Handling the final digits (4 or fewer) specially: + - 2 digits: one block of length 2 + - 3 digits: one block of length 3 + - 4 digits: two blocks of length 2 +4. Joining all blocks with dashes + +Example 1 + +Input: $phone = "1-23-45-6" +Output: "123-456" + +Example 2 + +Input: $phone = "1234" +Output: "12-34" + +Example 3 + +Input: $phone = "12 345-6789" +Output: "123-456-789" + +Example 4 + +Input: $phone = "123 4567" +Output: "123-45-67" + +Example 5 + +Input: $phone = "123 456-78" +Output: "123-456-78" + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 16th November + 2025. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +def fpn(string: str) -> str: + dgt = [e for e in string if e.isdigit()] + #print(dgt) + l = len(dgt) + i = 3 + while i < l: + if l-i == 1: + i -= 1 + dgt[i] = "-" + dgt[i] + i += 3 + #print(dgt) + return "".join(dgt) + +import unittest + +class TestFpn(unittest.TestCase): + def test(self): + for inpt, otpt in { + "1-23-45-6": "123-456", + "1234": "12-34", + "12 345-6789": "123-456-789", + "123 4567": "123-45-67", + "123 456-78": "123-456-78", + }.items(): + self.assertEqual(fpn(inpt),otpt) + +unittest.main()