aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-07-16 11:43:14 +0100
committerGitHub <noreply@github.com>2024-07-16 11:43:14 +0100
commit7afd76cd18a98ae3b3535cfd90d292a88ac8b9a2 (patch)
tree3add2739160fb596d1a24b4d0b455ebadc8f18b4
parent6d0af37dbeba795de3a812874f1d711995d16c2d (diff)
parenta92039a93c92921d5522ca3e43eee35b3a29ea75 (diff)
downloadperlweeklychallenge-club-7afd76cd18a98ae3b3535cfd90d292a88ac8b9a2.tar.gz
perlweeklychallenge-club-7afd76cd18a98ae3b3535cfd90d292a88ac8b9a2.tar.bz2
perlweeklychallenge-club-7afd76cd18a98ae3b3535cfd90d292a88ac8b9a2.zip
Merge pull request #10435 from pokgopun/pwc278
Pwc278
-rw-r--r--challenge-278/pokgopun/go/ch-1.go124
-rw-r--r--challenge-278/pokgopun/go/ch-2.go71
-rw-r--r--challenge-278/pokgopun/python/ch-1.py67
-rw-r--r--challenge-278/pokgopun/python/ch-2.py55
4 files changed, 317 insertions, 0 deletions
diff --git a/challenge-278/pokgopun/go/ch-1.go b/challenge-278/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..5f9e8c7646
--- /dev/null
+++ b/challenge-278/pokgopun/go/ch-1.go
@@ -0,0 +1,124 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-278/
+/*#
+
+Task 1: Sort String
+
+Submitted by: [51]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a shuffle string, $str.
+
+ Write a script to return the sorted string.
+
+ A string is shuffled by appending word position to each word.
+
+Example 1
+
+Input: $str = "and2 Raku3 cousins5 Perl1 are4"
+Output: "Perl and Raku are cousins"
+
+Example 2
+
+Input: $str = "guest6 Python1 most4 the3 popular5 is2 language7"
+Output: "Python is the most popular guest language"
+
+Example 3
+
+Input: $str = "Challenge3 The1 Weekly2"
+Output: "The Weekly Challenge"
+
+Task 2: Reverse Word
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "cmp"
+ "io"
+ "os"
+ "slices"
+ "strconv"
+ "strings"
+
+ gcmp "github.com/google/go-cmp/cmp"
+)
+
+// word position is consistent and single-digit
+func sortString0(str string) string {
+ s := strings.Split(str, " ")
+ r := make([]string, len(s))
+ for _, v := range s {
+ l := len(v)
+ r[v[l-1]-49] = v[:l-1]
+ }
+ return strings.Join(r, " ")
+}
+
+// word position is consistent and can be multiple-digit
+func sortString1(str string) string {
+ s := strings.Split(str, " ")
+ r := make([]string, len(s))
+ for _, v := range s {
+ l := len(v)
+ for l > 0 {
+ l--
+ if v[l] < 48 || v[l] > 57 {
+ break
+ }
+ }
+ d := v[l+1:]
+ rank, _ := strconv.Atoi(d)
+ r[rank-1] = v[:l+1]
+ }
+ return strings.Join(r, " ")
+}
+
+// word position does not need to be consistent and can be multiple-digit or empty
+func sortString2(str string) string {
+ s := strings.Split(str, " ")
+ type rankWord struct {
+ rank int
+ word string
+ }
+ l := len(s)
+ rw := make([]rankWord, l)
+ r := make([]string, l)
+ for i, v := range s {
+ l := len(v)
+ for l > 0 {
+ l--
+ if v[l] < 48 || v[l] > 57 {
+ break
+ }
+ }
+ d := v[l+1:]
+ rank, _ := strconv.Atoi(d)
+ rw[i] = rankWord{rank, v[:l+1]}
+ }
+ slices.SortFunc(rw, func(a, b rankWord) int {
+ return cmp.Compare(a.rank, b.rank)
+ })
+ for i, v := range rw {
+ r[i] = v.word
+ }
+ return strings.Join(r, " ")
+}
+
+func main() {
+ sortStringFuncs := [3]func(string) string{sortString2, sortString1, sortString0}
+ for _, data := range []struct {
+ input, output string
+ flevel int
+ }{
+ {"and2 Raku3 cousins5 Perl1 are4", "Perl and Raku are cousins", 3},
+ {"guest6 Python1 most4 the3 popular5 is2 language7", "Python is the most popular guest language", 3},
+ {"Challenge3 The1 Weekly2", "The Weekly Challenge", 3},
+ {"it9 guest6 Python1 most4 the3 popular5 timtowtdi12 is2 language7 but8 is10 not11", "Python is the most popular guest language but it is not timtowtdi", 2},
+ {"Challenge31 The0 Weekly2", "The Weekly Challenge", 1},
+ } {
+ for _, f := range sortStringFuncs[:data.flevel] {
+ io.WriteString(os.Stdout, gcmp.Diff(f(data.input), data.output)) // blank if ok, otherwise show the difference
+ }
+ }
+}
diff --git a/challenge-278/pokgopun/go/ch-2.go b/challenge-278/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..88e5b1bd7b
--- /dev/null
+++ b/challenge-278/pokgopun/go/ch-2.go
@@ -0,0 +1,71 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-278/
+/*#
+
+Task 2: Reverse Word
+
+Submitted by: [52]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a word, $word and a character, $char.
+
+ Write a script to replace the substring up to and including $char with
+ its characters sorted alphabetically. If the $char doesn’t exist then
+ DON'T do anything.
+
+Example 1
+
+Input: $str = "challenge", $char = "e"
+Ouput: "acehllnge"
+
+Example 2
+
+Input: $str = "programming", $char = "a"
+Ouput: "agoprrmming"
+
+Example 3
+
+Input: $str = "champion", $char = "b"
+Ouput: "champion"
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 21st July 2024.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+ "slices"
+ "strings"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+func reverseWord(word string, char rune) string {
+ i := strings.IndexRune(word, char)
+ if i < 0 {
+ return word
+ }
+ s := []rune(word)[:i+1]
+ slices.Sort(s)
+ return string(s) + word[i+1:]
+}
+
+func main() {
+ for _, data := range []struct {
+ word string
+ char rune
+ output string
+ }{
+ {"challenge", 'e', "acehllnge"},
+ {"programming", 'a', "agoprrmming"},
+ {"champion", 'b', "champion"},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(reverseWord(data.word, data.char), data.output)) // blank if ok, otherwise show the difference
+ }
+}
diff --git a/challenge-278/pokgopun/python/ch-1.py b/challenge-278/pokgopun/python/ch-1.py
new file mode 100644
index 0000000000..302f04325b
--- /dev/null
+++ b/challenge-278/pokgopun/python/ch-1.py
@@ -0,0 +1,67 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-278/
+"""
+
+Task 1: Sort String
+
+Submitted by: [51]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a shuffle string, $str.
+
+ Write a script to return the sorted string.
+
+ A string is shuffled by appending word position to each word.
+
+Example 1
+
+Input: $str = "and2 Raku3 cousins5 Perl1 are4"
+Output: "Perl and Raku are cousins"
+
+Example 2
+
+Input: $str = "guest6 Python1 most4 the3 popular5 is2 language7"
+Output: "Python is the most popular guest language"
+
+Example 3
+
+Input: $str = "Challenge3 The1 Weekly2"
+Output: "The Weekly Challenge"
+
+Task 2: Reverse Word
+"""
+### solution by pokgopun@gmail.com
+
+'''
+def sortString(string: str):
+ return " ".join(
+ e[:-1] for e in sorted(
+ string.split(), key=lambda x: x[-1]
+ )
+ )
+'''
+
+def rankWord(string: str):
+ i = 0
+ while string[i-1].isdigit():
+ i -= 1
+ return (int(string[i:]),string[:i])
+
+def sortString(string: str):
+ return " ".join(
+ e[1] for e in sorted(
+ rankWord(x) for x in string.split()
+ )
+ )
+
+import unittest
+
+class TestSortString(unittest.TestCase):
+ def test(self):
+ for inpt, otpt in {
+ "and2 Raku3 cousins5 Perl1 are4": "Perl and Raku are cousins",
+ "guest6 Python1 most4 the3 popular5 is2 language7": "Python is the most popular guest language",
+ "Challenge3 The1 Weekly2": "The Weekly Challenge",
+ }.items():
+ self.assertEqual(sortString(inpt),otpt)
+
+unittest.main()
diff --git a/challenge-278/pokgopun/python/ch-2.py b/challenge-278/pokgopun/python/ch-2.py
new file mode 100644
index 0000000000..a5e1e34059
--- /dev/null
+++ b/challenge-278/pokgopun/python/ch-2.py
@@ -0,0 +1,55 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-278/
+"""
+
+Task 2: Reverse Word
+
+Submitted by: [52]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a word, $word and a character, $char.
+
+ Write a script to replace the substring up to and including $char with
+ its characters sorted alphabetically. If the $char doesn’t exist then
+ DON'T do anything.
+
+Example 1
+
+Input: $str = "challenge", $char = "e"
+Ouput: "acehllnge"
+
+Example 2
+
+Input: $str = "programming", $char = "a"
+Ouput: "agoprrmming"
+
+Example 3
+
+Input: $str = "champion", $char = "b"
+Ouput: "champion"
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 21st July 2024.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+"""
+### solution by pokgopun@gmail.com
+
+def reverseWord(word, char):
+ i = word.find(char)
+ if i < 0:
+ return word
+ return "".join(sorted(word[:i+1]))+word[i+1:]
+
+import unittest
+
+class TestReverseWord(unittest.TestCase):
+ def test(self):
+ for (word,char), otpt in {
+ ("challenge", "e"): "acehllnge",
+ ("programming", "a"):"agoprrmming",
+ ("champion", "b"): "champion",
+ }.items():
+ self.assertEqual(reverseWord(word,char), otpt)
+
+unittest.main()