Skip to content

Commit 007ebf1

Browse files
authored
Merge pull request #13002 from pokgopun/pwc347
Pwc347
2 parents d4f7584 + 39b0bd2 commit 007ebf1

File tree

6 files changed

+489
-0
lines changed

6 files changed

+489
-0
lines changed

challenge-347/pokgopun/go/ch-1.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
//# https://theweeklychallenge.org/blog/perl-weekly-challenge-347/
2+
/*#
3+
4+
Task 1: Format Date
5+
6+
Submitted by: [44]Mohammad Sajid Anwar
7+
__________________________________________________________________
8+
9+
You are given a date in the form: 10th Nov 2025.
10+
11+
Write a script to format the given date in the form: 2025-11-10 using
12+
the set below.
13+
@DAYS = ("1st", "2nd", "3rd", ....., "30th", "31st")
14+
@MONTHS = ("Jan", "Feb", "Mar", ....., "Nov", "Dec")
15+
@YEARS = (1900..2100)
16+
17+
Example 1
18+
19+
Input: $str = "1st Jan 2025"
20+
Output: "2025-01-01"
21+
22+
Example 2
23+
24+
Input: $str = "22nd Feb 2025"
25+
Output: "2025-02-22"
26+
27+
Example 3
28+
29+
Input: $str = "15th Apr 2025"
30+
Output: "2025-04-15"
31+
32+
Example 4
33+
34+
Input: $str = "23rd Oct 2025"
35+
Output: "2025-10-23"
36+
37+
Example 5
38+
39+
Input: $str = "31st Dec 2025"
40+
Output: "2025-12-31"
41+
42+
Task 2: Format Phone Number
43+
#*/
44+
//# solution by [email protected]
45+
46+
package main
47+
48+
import (
49+
"fmt"
50+
"io"
51+
"os"
52+
"slices"
53+
"strconv"
54+
"strings"
55+
56+
"github.com/google/go-cmp/cmp"
57+
)
58+
59+
func fd(str string) string {
60+
m := []string{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}
61+
d := strings.Split(str, " ")
62+
day, _ := strconv.Atoi(d[0][:len(d[0])-2])
63+
return fmt.Sprintf("%s-%02d-%02d", d[2], slices.Index(m, d[1])+1, day)
64+
}
65+
66+
func main() {
67+
for _, data := range []struct {
68+
input, output string
69+
}{
70+
{"1st Jan 2025", "2025-01-01"},
71+
{"22nd Feb 2025", "2025-02-22"},
72+
{"15th Apr 2025", "2025-04-15"},
73+
{"23rd Oct 2025", "2025-10-23"},
74+
{"31st Dec 2025", "2025-12-31"},
75+
} {
76+
io.WriteString(os.Stdout, cmp.Diff(fd(data.input), data.output)) // blank if ok, otherwise show the difference
77+
}
78+
}

challenge-347/pokgopun/go/ch-2.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
//# https://theweeklychallenge.org/blog/perl-weekly-challenge-347/
2+
/*#
3+
4+
Task 2: Format Phone Number
5+
6+
Submitted by: [45]Mohammad Sajid Anwar
7+
__________________________________________________________________
8+
9+
You are given a phone number as a string containing digits, space and
10+
dash only.
11+
12+
Write a script to format the given phone number using the below rules:
13+
1. Removing all spaces and dashes
14+
2. Grouping digits into blocks of length 3 from left to right
15+
3. Handling the final digits (4 or fewer) specially:
16+
- 2 digits: one block of length 2
17+
- 3 digits: one block of length 3
18+
- 4 digits: two blocks of length 2
19+
4. Joining all blocks with dashes
20+
21+
Example 1
22+
23+
Input: $phone = "1-23-45-6"
24+
Output: "123-456"
25+
26+
Example 2
27+
28+
Input: $phone = "1234"
29+
Output: "12-34"
30+
31+
Example 3
32+
33+
Input: $phone = "12 345-6789"
34+
Output: "123-456-789"
35+
36+
Example 4
37+
38+
Input: $phone = "123 4567"
39+
Output: "123-45-67"
40+
41+
Example 5
42+
43+
Input: $phone = "123 456-78"
44+
Output: "123-456-78"
45+
__________________________________________________________________
46+
47+
Last date to submit the solution 23:59 (UK Time) Sunday 16th November
48+
2025.
49+
__________________________________________________________________
50+
51+
SO WHAT DO YOU THINK ?
52+
#*/
53+
//# solution by [email protected]
54+
55+
package main
56+
57+
import (
58+
"io"
59+
"os"
60+
61+
"github.com/google/go-cmp/cmp"
62+
)
63+
64+
func fpn(str string) string {
65+
var rs []rune
66+
var i, j, k int
67+
for _, v := range str {
68+
if v < '0' || v > '9' {
69+
continue
70+
}
71+
if i > 0 && i%3 == 0 {
72+
rs = append(rs, '-')
73+
j++
74+
k = j
75+
}
76+
rs = append(rs, v)
77+
i++
78+
j++
79+
}
80+
//fmt.Println(i, j, k)
81+
if j-k == 1 {
82+
rs[k-1], rs[k-2] = rs[k-2], rs[k-1]
83+
}
84+
return string(rs)
85+
}
86+
87+
func main() {
88+
for _, data := range []struct {
89+
input, output string
90+
}{
91+
{"1-23-45-6", "123-456"},
92+
{"1234", "12-34"},
93+
{"12 345-6789", "123-456-789"},
94+
{"123 4567", "123-45-67"},
95+
{"123 456-78", "123-456-78"},
96+
} {
97+
io.WriteString(os.Stdout, cmp.Diff(fpn(data.input), data.output)) // blank if ok, otherwise show the difference
98+
}
99+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
--# https://theweeklychallenge.org/blog/perl-weekly-challenge-347/
2+
--[[
3+
4+
Task 1: Format Date
5+
6+
Submitted by: [44]Mohammad Sajid Anwar
7+
__________________________________________________________________
8+
9+
You are given a date in the form: 10th Nov 2025.
10+
11+
Write a script to format the given date in the form: 2025-11-10 using
12+
the set below.
13+
@DAYS = ("1st", "2nd", "3rd", ....., "30th", "31st")
14+
@MONTHS = ("Jan", "Feb", "Mar", ....., "Nov", "Dec")
15+
@YEARS = (1900..2100)
16+
17+
Example 1
18+
19+
Input: $str = "1st Jan 2025"
20+
Output: "2025-01-01"
21+
22+
Example 2
23+
24+
Input: $str = "22nd Feb 2025"
25+
Output: "2025-02-22"
26+
27+
Example 3
28+
29+
Input: $str = "15th Apr 2025"
30+
Output: "2025-04-15"
31+
32+
Example 4
33+
34+
Input: $str = "23rd Oct 2025"
35+
Output: "2025-10-23"
36+
37+
Example 5
38+
39+
Input: $str = "31st Dec 2025"
40+
Output: "2025-12-31"
41+
42+
Task 2: Format Phone Number
43+
--]]
44+
--# solution by [email protected]
45+
46+
47+
function fd(str)
48+
local m = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}
49+
local date = ""
50+
local idx = 0
51+
for d in string.gmatch(str, "%S+") do
52+
idx = idx + 1
53+
if idx == 1 then
54+
d = "-"..string.format("%02d", tonumber(string.sub(d, 1, #d-2)))
55+
elseif idx == 2 then
56+
for i, v in ipairs(m) do
57+
if d == v then
58+
d = "-"..string.format("%02d", i)
59+
end
60+
end
61+
end
62+
date = d .. date
63+
end
64+
return date
65+
end
66+
67+
local lu = require("luaunit")
68+
69+
function TestFd()
70+
for inpt, otpt in pairs({
71+
["1st Jan 2025"] = "2025-01-01",
72+
["22nd Feb 2025"] = "2025-02-22",
73+
["15th Apr 2025"] = "2025-04-15",
74+
["23rd Oct 2025"]= "2025-10-23",
75+
["31st Dec 2025"]= "2025-12-31"}) do
76+
lu.assertEquals(fd(inpt), otpt)
77+
end
78+
end
79+
80+
lu.run()
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
--# https://theweeklychallenge.org/blog/perl-weekly-challenge-347/
2+
--[[
3+
4+
Task 2: Format Phone Number
5+
6+
Submitted by: [45]Mohammad Sajid Anwar
7+
__________________________________________________________________
8+
9+
You are given a phone number as a string containing digits, space and
10+
dash only.
11+
12+
Write a script to format the given phone number using the below rules:
13+
1. Removing all spaces and dashes
14+
2. Grouping digits into blocks of length 3 from left to right
15+
3. Handling the final digits (4 or fewer) specially:
16+
- 2 digits: one block of length 2
17+
- 3 digits: one block of length 3
18+
- 4 digits: two blocks of length 2
19+
4. Joining all blocks with dashes
20+
21+
Example 1
22+
23+
Input: $phone = "1-23-45-6"
24+
Output: "123-456"
25+
26+
Example 2
27+
28+
Input: $phone = "1234"
29+
Output: "12-34"
30+
31+
Example 3
32+
33+
Input: $phone = "12 345-6789"
34+
Output: "123-456-789"
35+
36+
Example 4
37+
38+
Input: $phone = "123 4567"
39+
Output: "123-45-67"
40+
41+
Example 5
42+
43+
Input: $phone = "123 456-78"
44+
Output: "123-456-78"
45+
__________________________________________________________________
46+
47+
Last date to submit the solution 23:59 (UK Time) Sunday 16th November
48+
2025.
49+
__________________________________________________________________
50+
51+
SO WHAT DO YOU THINK ?
52+
--]]
53+
--# solution by [email protected]
54+
55+
function fpn(phone)
56+
local dgt = {}
57+
for v in string.gmatch(phone,"%d") do
58+
table.insert(dgt,v)
59+
end
60+
local l = #dgt
61+
local i = 4
62+
while i <= l do
63+
if i == l then
64+
i = i - 1
65+
end
66+
dgt[i] = "-"..dgt[i]
67+
i = i + 3
68+
end
69+
return table.concat(dgt,"")
70+
end
71+
72+
local lu = require("luaunit")
73+
74+
function TestFpn()
75+
for inpt, otpt in pairs({
76+
["1-23-45-6"]= "123-456",
77+
["1234"]= "12-34",
78+
["12 345-6789"]= "123-456-789",
79+
["123 4567"] = "123-45-67",
80+
["123 456-78"] = "123-456-78"}) do
81+
lu.assertEquals(fpn(inpt), otpt)
82+
end
83+
end
84+
85+
lu.run()

0 commit comments

Comments
 (0)