Skip to content

Commit 59dc819

Browse files
authored
Merge pull request #11745 from pokgopun/pwc313
Pwc313
2 parents f782173 + 86b920f commit 59dc819

File tree

4 files changed

+346
-0
lines changed

4 files changed

+346
-0
lines changed

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

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
//# https://theweeklychallenge.org/blog/perl-weekly-challenge-313/
2+
/*#
3+
4+
Task 1: Broken Keys
5+
6+
Submitted by: [43]Mohammad Sajid Anwar
7+
__________________________________________________________________
8+
9+
You have a broken keyboard which sometimes type a character more than
10+
once.
11+
12+
You are given a string and actual typed string.
13+
14+
Write a script to find out if the actual typed string is meant for the
15+
given string.
16+
17+
Example 1
18+
19+
Input: $name = "perl", $typed = "perrrl"
20+
Output: true
21+
22+
Here "r" is pressed 3 times instead of 1 time.
23+
24+
Example 2
25+
26+
Input: $name = "raku", $typed = "rrakuuuu"
27+
Output: true
28+
29+
Example 3
30+
31+
Input: $name = "python", $typed = "perl"
32+
Output: false
33+
34+
Example 4
35+
36+
Input: $name = "coffeescript", $typed = "cofffeescccript"
37+
Output: true
38+
39+
Task 2: Reverse Letters
40+
#*/
41+
//# solution by [email protected]
42+
43+
package main
44+
45+
import (
46+
"io"
47+
"os"
48+
49+
"github.com/google/go-cmp/cmp"
50+
)
51+
52+
type charCount struct {
53+
chr rune
54+
cnt int
55+
}
56+
57+
type charCounts []charCount
58+
59+
func newCharCounts(str string) charCounts {
60+
chrs := []rune(str)
61+
if len(chrs) == 0 {
62+
return charCounts{}
63+
}
64+
ccs := charCounts{charCount{chrs[0], 1}}
65+
i := 0
66+
for _, v := range chrs[1:] {
67+
if v == ccs[i].chr {
68+
ccs[i].cnt++
69+
} else {
70+
ccs = append(ccs, charCount{v, 1})
71+
i++
72+
}
73+
}
74+
return ccs
75+
}
76+
77+
type input struct {
78+
name, typed string
79+
}
80+
81+
func (in input) process() bool {
82+
name_cc := newCharCounts(in.name)
83+
l := len(name_cc)
84+
typed_cc := newCharCounts(in.typed)
85+
if l != len(typed_cc) {
86+
return false
87+
}
88+
for i := range l {
89+
ncc := name_cc[i]
90+
tcc := typed_cc[i]
91+
if ncc.chr != tcc.chr || ncc.cnt > tcc.cnt {
92+
return false
93+
}
94+
}
95+
return true
96+
}
97+
98+
func main() {
99+
for _, data := range []struct {
100+
input input
101+
output bool
102+
}{
103+
{input{"perl", "perrrl"}, true},
104+
{input{"raku", "rrakuuuu"}, true},
105+
{input{"python", "perl"}, false},
106+
{input{"coffeescript", "cofffeescccript"}, true},
107+
{input{"noob", "nob"}, false},
108+
{input{"snow", "noow"}, false},
109+
{input{"ten", "teens"}, false},
110+
{input{"aha", "haa"}, false},
111+
{input{"กากมาก", "กาาากมาาาาาาก"}, true},
112+
{input{"กาาากมาาาก", "กากมาก"}, false},
113+
{input{"รักกัน", "รรักกันน"}, true},
114+
{input{"บรรทัด", "บรทัด"}, false},
115+
{input{"โรงเรียน", "โโโรรงงเรรียยนน"}, true},
116+
{input{"เกเรียน", "เกกเรรียยนน"}, true},
117+
{input{"แมว", "แแแมมมวววว"}, true},
118+
{input{"มมมแมว", "มแมมมวววว"}, false},
119+
} {
120+
//fmt.Println(data.input)
121+
io.WriteString(os.Stdout, cmp.Diff(data.input.process(), data.output)) // blank if ok, otherwise show the difference
122+
}
123+
}

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

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//# https://theweeklychallenge.org/blog/perl-weekly-challenge-313/
2+
/*#
3+
4+
Task 2: Reverse Letters
5+
6+
Submitted by: [44]Mohammad Sajid Anwar
7+
__________________________________________________________________
8+
9+
You are given a string.
10+
11+
Write a script to reverse only the alphabetic characters in the string.
12+
13+
Example 1
14+
15+
Input: $str = "p-er?l"
16+
Output: "l-re?p"
17+
18+
Example 2
19+
20+
Input: $str = "wee-k!L-y"
21+
Output: "yLk-e!e-w"
22+
23+
Example 3
24+
25+
Input: $str = "_c-!h_all-en!g_e"
26+
Output: "_e-!g_nel-la!h_c"
27+
__________________________________________________________________
28+
29+
Last date to submit the solution 23:59 (UK Time) Sunday 23rd March
30+
2025.
31+
__________________________________________________________________
32+
33+
SO WHAT DO YOU THINK ?
34+
#*/
35+
//# solution by [email protected]
36+
37+
package main
38+
39+
import (
40+
"io"
41+
"os"
42+
43+
"github.com/google/go-cmp/cmp"
44+
)
45+
46+
func rl(str string) string {
47+
chrs := []rune(str)
48+
var ltrPos []int
49+
for i, v := range chrs {
50+
if (v >= 'a' && v <= 'z') || (v >= 'A' && v <= 'Z') || (v >= 'ก' && v <= 'ฮ') {
51+
ltrPos = append(ltrPos, i)
52+
}
53+
}
54+
l := len(ltrPos)
55+
for i := range l / 2 {
56+
a, b := ltrPos[i], ltrPos[l-1-i]
57+
chrs[a], chrs[b] = chrs[b], chrs[a]
58+
}
59+
return string(chrs)
60+
}
61+
62+
func main() {
63+
for _, data := range []struct {
64+
input, output string
65+
}{
66+
{"p-er?l", "l-re?p"},
67+
{"wee-k!L-y", "yLk-e!e-w"},
68+
{"_c-!h_all-en!g_e", "_e-!g_nel-la!h_c"},
69+
{"ก-รอ?บ", "บ-อร?ก"},
70+
} {
71+
io.WriteString(os.Stdout, cmp.Diff(rl(data.input), data.output)) // blank if ok, otherwise show the difference
72+
}
73+
}

challenge-313/pokgopun/python/ch-1.py

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
### https://theweeklychallenge.org/blog/perl-weekly-challenge-313/
2+
"""
3+
4+
Task 1: Broken Keys
5+
6+
Submitted by: [43]Mohammad Sajid Anwar
7+
__________________________________________________________________
8+
9+
You have a broken keyboard which sometimes type a character more than
10+
once.
11+
12+
You are given a string and actual typed string.
13+
14+
Write a script to find out if the actual typed string is meant for the
15+
given string.
16+
17+
Example 1
18+
19+
Input: $name = "perl", $typed = "perrrl"
20+
Output: true
21+
22+
Here "r" is pressed 3 times instead of 1 time.
23+
24+
Example 2
25+
26+
Input: $name = "raku", $typed = "rrakuuuu"
27+
Output: true
28+
29+
Example 3
30+
31+
Input: $name = "python", $typed = "perl"
32+
Output: false
33+
34+
Example 4
35+
36+
Input: $name = "coffeescript", $typed = "cofffeescccript"
37+
Output: true
38+
39+
Task 2: Reverse Letters
40+
"""
41+
### solution by [email protected]
42+
43+
from dataclasses import dataclass
44+
45+
@dataclass
46+
class CharCount:
47+
char: str
48+
count: int
49+
50+
def seqCnt(string: str) -> list[CharCount]:
51+
l = len(string)
52+
if l == 0:
53+
return []
54+
sc = [CharCount(string[0],1)]
55+
for c in string[1:]:
56+
if c == sc[-1].char:
57+
sc[-1].count += 1
58+
else:
59+
sc.append(CharCount(c,1))
60+
return sc
61+
62+
def bk(name: str, typed: str) -> bool:
63+
name_sc = seqCnt(name)
64+
typed_sc = seqCnt(typed)
65+
#print("->",name_sc)
66+
#print("=>",typed_sc)
67+
l = len(typed_sc)
68+
if l != len(name_sc):
69+
return False
70+
for i in range(l):
71+
n = name_sc[i]
72+
t = typed_sc[i]
73+
if n.char != t.char or n.count > t.count:
74+
return False
75+
return True
76+
77+
import unittest
78+
79+
class TestBk(unittest.TestCase):
80+
def test(self):
81+
for (name, typed), otpt in {
82+
("perl", "perrrl"): True,
83+
("raku", "rrakuuuu"): True,
84+
("python", "perl"): False,
85+
("coffeescript", "cofffeescccript"): True,
86+
("noob", "nob"): False,
87+
("snow", "noow"): False,
88+
("ten", "teens"): False,
89+
("aha", "haa"): False,
90+
}.items():
91+
self.assertEqual(bk(name,typed),otpt)
92+
93+
unittest.main()

challenge-313/pokgopun/python/ch-2.py

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
### https://theweeklychallenge.org/blog/perl-weekly-challenge-313/
2+
"""
3+
4+
Task 2: Reverse Letters
5+
6+
Submitted by: [44]Mohammad Sajid Anwar
7+
__________________________________________________________________
8+
9+
You are given a string.
10+
11+
Write a script to reverse only the alphabetic characters in the string.
12+
13+
Example 1
14+
15+
Input: $str = "p-er?l"
16+
Output: "l-re?p"
17+
18+
Example 2
19+
20+
Input: $str = "wee-k!L-y"
21+
Output: "yLk-e!e-w"
22+
23+
Example 3
24+
25+
Input: $str = "_c-!h_all-en!g_e"
26+
Output: "_e-!g_nel-la!h_c"
27+
__________________________________________________________________
28+
29+
Last date to submit the solution 23:59 (UK Time) Sunday 23rd March
30+
2025.
31+
__________________________________________________________________
32+
33+
SO WHAT DO YOU THINK ?
34+
"""
35+
### solution by [email protected]
36+
37+
def rl(string: str) -> str:
38+
ltrPos = tuple(i for i in range(len(string)) if string[i].isalpha()) ### positions of letters
39+
chars = list(string) ### convert string to list so its letters can be reversed
40+
l = len(ltrPos)
41+
for i in range(l//2): ### do the reverse by swap a letter at start and end of the list unitl reaching the mid
42+
a, b = ltrPos[i], ltrPos[l-1-i]
43+
chars[a], chars[b] = chars[b], chars[a]
44+
return "".join(chars) ### compose the string back
45+
46+
import unittest
47+
48+
class TestRl(unittest.TestCase):
49+
def test(self):
50+
for inpt, otpt in {
51+
"p-er?l": "l-re?p",
52+
"wee-k!L-y": "yLk-e!e-w",
53+
"_c-!h_all-en!g_e": "_e-!g_nel-la!h_c",
54+
}.items():
55+
self.assertEqual(rl(inpt),otpt)
56+
57+
unittest.main()

0 commit comments

Comments
 (0)