Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pwc313 #11745

Merged
merged 3 commits into from
Mar 21, 2025
Merged

Pwc313 #11745

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions challenge-313/pokgopun/go/ch-1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
//# https://theweeklychallenge.org/blog/perl-weekly-challenge-313/
/*#

Task 1: Broken Keys

Submitted by: [43]Mohammad Sajid Anwar
__________________________________________________________________

You have a broken keyboard which sometimes type a character more than
once.

You are given a string and actual typed string.

Write a script to find out if the actual typed string is meant for the
given string.

Example 1

Input: $name = "perl", $typed = "perrrl"
Output: true

Here "r" is pressed 3 times instead of 1 time.

Example 2

Input: $name = "raku", $typed = "rrakuuuu"
Output: true

Example 3

Input: $name = "python", $typed = "perl"
Output: false

Example 4

Input: $name = "coffeescript", $typed = "cofffeescccript"
Output: true

Task 2: Reverse Letters
#*/
//# solution by [email protected]

package main

import (
"io"
"os"

"github.com/google/go-cmp/cmp"
)

type charCount struct {
chr rune
cnt int
}

type charCounts []charCount

func newCharCounts(str string) charCounts {
chrs := []rune(str)
if len(chrs) == 0 {
return charCounts{}
}
ccs := charCounts{charCount{chrs[0], 1}}
i := 0
for _, v := range chrs[1:] {
if v == ccs[i].chr {
ccs[i].cnt++
} else {
ccs = append(ccs, charCount{v, 1})
i++
}
}
return ccs
}

type input struct {
name, typed string
}

func (in input) process() bool {
name_cc := newCharCounts(in.name)
l := len(name_cc)
typed_cc := newCharCounts(in.typed)
if l != len(typed_cc) {
return false
}
for i := range l {
ncc := name_cc[i]
tcc := typed_cc[i]
if ncc.chr != tcc.chr || ncc.cnt > tcc.cnt {
return false
}
}
return true
}

func main() {
for _, data := range []struct {
input input
output bool
}{
{input{"perl", "perrrl"}, true},
{input{"raku", "rrakuuuu"}, true},
{input{"python", "perl"}, false},
{input{"coffeescript", "cofffeescccript"}, true},
{input{"noob", "nob"}, false},
{input{"snow", "noow"}, false},
{input{"ten", "teens"}, false},
{input{"aha", "haa"}, false},
{input{"กากมาก", "กาาากมาาาาาาก"}, true},
{input{"กาาากมาาาก", "กากมาก"}, false},
{input{"รักกัน", "รรักกันน"}, true},
{input{"บรรทัด", "บรทัด"}, false},
{input{"โรงเรียน", "โโโรรงงเรรียยนน"}, true},
{input{"เกเรียน", "เกกเรรียยนน"}, true},
{input{"แมว", "แแแมมมวววว"}, true},
{input{"มมมแมว", "มแมมมวววว"}, false},
} {
//fmt.Println(data.input)
io.WriteString(os.Stdout, cmp.Diff(data.input.process(), data.output)) // blank if ok, otherwise show the difference
}
}
73 changes: 73 additions & 0 deletions challenge-313/pokgopun/go/ch-2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//# https://theweeklychallenge.org/blog/perl-weekly-challenge-313/
/*#

Task 2: Reverse Letters

Submitted by: [44]Mohammad Sajid Anwar
__________________________________________________________________

You are given a string.

Write a script to reverse only the alphabetic characters in the string.

Example 1

Input: $str = "p-er?l"
Output: "l-re?p"

Example 2

Input: $str = "wee-k!L-y"
Output: "yLk-e!e-w"

Example 3

Input: $str = "_c-!h_all-en!g_e"
Output: "_e-!g_nel-la!h_c"
__________________________________________________________________

Last date to submit the solution 23:59 (UK Time) Sunday 23rd March
2025.
__________________________________________________________________

SO WHAT DO YOU THINK ?
#*/
//# solution by [email protected]

package main

import (
"io"
"os"

"github.com/google/go-cmp/cmp"
)

func rl(str string) string {
chrs := []rune(str)
var ltrPos []int
for i, v := range chrs {
if (v >= 'a' && v <= 'z') || (v >= 'A' && v <= 'Z') || (v >= 'ก' && v <= 'ฮ') {
ltrPos = append(ltrPos, i)
}
}
l := len(ltrPos)
for i := range l / 2 {
a, b := ltrPos[i], ltrPos[l-1-i]
chrs[a], chrs[b] = chrs[b], chrs[a]
}
return string(chrs)
}

func main() {
for _, data := range []struct {
input, output string
}{
{"p-er?l", "l-re?p"},
{"wee-k!L-y", "yLk-e!e-w"},
{"_c-!h_all-en!g_e", "_e-!g_nel-la!h_c"},
{"ก-รอ?บ", "บ-อร?ก"},
} {
io.WriteString(os.Stdout, cmp.Diff(rl(data.input), data.output)) // blank if ok, otherwise show the difference
}
}
93 changes: 93 additions & 0 deletions challenge-313/pokgopun/python/ch-1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
### https://theweeklychallenge.org/blog/perl-weekly-challenge-313/
"""

Task 1: Broken Keys

Submitted by: [43]Mohammad Sajid Anwar
__________________________________________________________________

You have a broken keyboard which sometimes type a character more than
once.

You are given a string and actual typed string.

Write a script to find out if the actual typed string is meant for the
given string.

Example 1

Input: $name = "perl", $typed = "perrrl"
Output: true

Here "r" is pressed 3 times instead of 1 time.

Example 2

Input: $name = "raku", $typed = "rrakuuuu"
Output: true

Example 3

Input: $name = "python", $typed = "perl"
Output: false

Example 4

Input: $name = "coffeescript", $typed = "cofffeescccript"
Output: true

Task 2: Reverse Letters
"""
### solution by [email protected]

from dataclasses import dataclass

@dataclass
class CharCount:
char: str
count: int

def seqCnt(string: str) -> list[CharCount]:
l = len(string)
if l == 0:
return []
sc = [CharCount(string[0],1)]
for c in string[1:]:
if c == sc[-1].char:
sc[-1].count += 1
else:
sc.append(CharCount(c,1))
return sc

def bk(name: str, typed: str) -> bool:
name_sc = seqCnt(name)
typed_sc = seqCnt(typed)
#print("->",name_sc)
#print("=>",typed_sc)
l = len(typed_sc)
if l != len(name_sc):
return False
for i in range(l):
n = name_sc[i]
t = typed_sc[i]
if n.char != t.char or n.count > t.count:
return False
return True

import unittest

class TestBk(unittest.TestCase):
def test(self):
for (name, typed), otpt in {
("perl", "perrrl"): True,
("raku", "rrakuuuu"): True,
("python", "perl"): False,
("coffeescript", "cofffeescccript"): True,
("noob", "nob"): False,
("snow", "noow"): False,
("ten", "teens"): False,
("aha", "haa"): False,
}.items():
self.assertEqual(bk(name,typed),otpt)

unittest.main()
57 changes: 57 additions & 0 deletions challenge-313/pokgopun/python/ch-2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
### https://theweeklychallenge.org/blog/perl-weekly-challenge-313/
"""

Task 2: Reverse Letters

Submitted by: [44]Mohammad Sajid Anwar
__________________________________________________________________

You are given a string.

Write a script to reverse only the alphabetic characters in the string.

Example 1

Input: $str = "p-er?l"
Output: "l-re?p"

Example 2

Input: $str = "wee-k!L-y"
Output: "yLk-e!e-w"

Example 3

Input: $str = "_c-!h_all-en!g_e"
Output: "_e-!g_nel-la!h_c"
__________________________________________________________________

Last date to submit the solution 23:59 (UK Time) Sunday 23rd March
2025.
__________________________________________________________________

SO WHAT DO YOU THINK ?
"""
### solution by [email protected]

def rl(string: str) -> str:
ltrPos = tuple(i for i in range(len(string)) if string[i].isalpha()) ### positions of letters
chars = list(string) ### convert string to list so its letters can be reversed
l = len(ltrPos)
for i in range(l//2): ### do the reverse by swap a letter at start and end of the list unitl reaching the mid
a, b = ltrPos[i], ltrPos[l-1-i]
chars[a], chars[b] = chars[b], chars[a]
return "".join(chars) ### compose the string back

import unittest

class TestRl(unittest.TestCase):
def test(self):
for inpt, otpt in {
"p-er?l": "l-re?p",
"wee-k!L-y": "yLk-e!e-w",
"_c-!h_all-en!g_e": "_e-!g_nel-la!h_c",
}.items():
self.assertEqual(rl(inpt),otpt)

unittest.main()