aboutsummaryrefslogtreecommitdiff
path: root/challenge-280
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-07-29 11:29:55 +0100
committerGitHub <noreply@github.com>2024-07-29 11:29:55 +0100
commitf814e05c8e6580d2a1c0c0fd2082296478492f39 (patch)
tree42ad4cb41126cca51274c2cf9c7841f3ccc3ba99 /challenge-280
parentb9587166580480d2f5dddce64bfcb8d33ef4e127 (diff)
parent9c694541b722a3d170491cf8c0dabb45e639634f (diff)
downloadperlweeklychallenge-club-f814e05c8e6580d2a1c0c0fd2082296478492f39.tar.gz
perlweeklychallenge-club-f814e05c8e6580d2a1c0c0fd2082296478492f39.tar.bz2
perlweeklychallenge-club-f814e05c8e6580d2a1c0c0fd2082296478492f39.zip
Merge pull request #10505 from pokgopun/pwc280
pwc280 solution
Diffstat (limited to 'challenge-280')
-rw-r--r--challenge-280/pokgopun/go/ch-1.go77
-rw-r--r--challenge-280/pokgopun/go/ch-2.go86
-rw-r--r--challenge-280/pokgopun/python/ch-1.py53
-rw-r--r--challenge-280/pokgopun/python/ch-2.py67
4 files changed, 283 insertions, 0 deletions
diff --git a/challenge-280/pokgopun/go/ch-1.go b/challenge-280/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..ce48d76009
--- /dev/null
+++ b/challenge-280/pokgopun/go/ch-1.go
@@ -0,0 +1,77 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-280/
+/*#
+
+Task 1: Twice Appearance
+
+Submitted by: [53]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a string, $str, containing lowercase English letters
+ only.
+
+ Write a script to print the first letter that appears twice.
+
+Example 1
+
+Input: $str = "acbddbca"
+Output: "d"
+
+Example 2
+
+Input: $str = "abccd"
+Output: "c"
+
+Example 3
+
+Input: $str = "abcdabbb"
+Output: "a"
+
+Task 2: Count Asterisks
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+func twcApr(str string) rune {
+ m := make(map[rune]int)
+ var (
+ c rune
+ mn int
+ )
+ for i, v := range str {
+ //fmt.Println(string(v), v)
+ if m[v] == 0 {
+ m[v] = -i - 1
+ } else if m[v] < 0 {
+ m[v] = i + 1
+ }
+ if m[v] > 0 {
+ if mn == 0 || mn > m[v] {
+ mn = m[v]
+ c = v
+ }
+ }
+ }
+ //fmt.Println(m)
+ return c
+}
+
+func main() {
+ for _, data := range []struct {
+ input string
+ output rune
+ }{
+ {"acbddbca", 'd'},
+ {"abccd", 'c'},
+ {"abcdabbb", 'a'},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(twcApr(data.input), data.output)) // blank if ok, otherwise show the difference
+ }
+}
diff --git a/challenge-280/pokgopun/go/ch-2.go b/challenge-280/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..a07e74a936
--- /dev/null
+++ b/challenge-280/pokgopun/go/ch-2.go
@@ -0,0 +1,86 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-280/
+/*#
+
+Task 2: Count Asterisks
+
+Submitted by: [54]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a string, $str, where every two consecutive vertical bars
+ are grouped into a pair.
+
+ Write a script to return the number of asterisks, *, excluding any
+ between each pair of vertical bars.
+
+Example 1
+
+Input: $str = "p|*e*rl|w**e|*ekly|"
+Ouput: 2
+
+The characters we are looking here are "p" and "w**e".
+
+Example 2
+
+Input: $str = "perl"
+Ouput: 0
+
+Example 3
+
+Input: $str = "th|ewe|e**|k|l***ych|alleng|e"
+Ouput: 5
+
+The characters we are looking here are "th", "e**", "l***ych" and "e".
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 4th August
+ 2024.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+func cntAtr(str string) int {
+ a, b := byte('*'), byte('|')
+ c := 0
+ l := len(str)
+ for l > 0 {
+ l--
+ switch str[l] {
+ case a:
+ c++
+ case b:
+ d := len(str[:l])
+ for d > 0 {
+ d--
+ if str[d] == b {
+ l = d
+ break
+ }
+ }
+ }
+ }
+ return c
+}
+
+func main() {
+ for _, data := range []struct {
+ input string
+ output int
+ }{
+ {"p|*e*rl|w**e|*ekly|", 2},
+ {"perl", 0},
+ {"th|ewe|e**|k|l***ych|alleng|e", 5},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(cntAtr(data.input), data.output)) // blank if ok, otherwise show the difference
+ }
+}
diff --git a/challenge-280/pokgopun/python/ch-1.py b/challenge-280/pokgopun/python/ch-1.py
new file mode 100644
index 0000000000..f3f4684f8e
--- /dev/null
+++ b/challenge-280/pokgopun/python/ch-1.py
@@ -0,0 +1,53 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-280/
+"""
+
+Task 1: Twice Appearance
+
+Submitted by: [53]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a string, $str, containing lowercase English letters
+ only.
+
+ Write a script to print the first letter that appears twice.
+
+Example 1
+
+Input: $str = "acbddbca"
+Output: "d"
+
+Example 2
+
+Input: $str = "abccd"
+Output: "c"
+
+Example 3
+
+Input: $str = "abcdabbb"
+Output: "a"
+
+Task 2: Count Asterisks
+"""
+### solution by pokgopun@gmail.com
+
+def ta(string: str):
+ return min(
+ e for e in (
+ (string[t[0]+1:].find(t[1]),t[1]) for t in (
+ (string.find(c),c) for c in set(string)
+ )
+ ) if e[0] >= 0
+ )[1]
+
+import unittest
+
+class TestTa(unittest.TestCase):
+ def test(self):
+ for inpt,otpt in {
+ # "acbddbca": "d",
+ "abccd": "c",
+ "abcdabbb": "a",
+ }.items():
+ self.assertEqual(ta(inpt),otpt)
+
+unittest.main()
diff --git a/challenge-280/pokgopun/python/ch-2.py b/challenge-280/pokgopun/python/ch-2.py
new file mode 100644
index 0000000000..ce4a624900
--- /dev/null
+++ b/challenge-280/pokgopun/python/ch-2.py
@@ -0,0 +1,67 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-280/
+"""
+
+Task 2: Count Asterisks
+
+Submitted by: [54]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a string, $str, where every two consecutive vertical bars
+ are grouped into a pair.
+
+ Write a script to return the number of asterisks, *, excluding any
+ between each pair of vertical bars.
+
+Example 1
+
+Input: $str = "p|*e*rl|w**e|*ekly|"
+Ouput: 2
+
+The characters we are looking here are "p" and "w**e".
+
+Example 2
+
+Input: $str = "perl"
+Ouput: 0
+
+Example 3
+
+Input: $str = "th|ewe|e**|k|l***ych|alleng|e"
+Ouput: 5
+
+The characters we are looking here are "th", "e**", "l***ych" and "e".
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 4th August
+ 2024.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+"""
+### solution by pokgopun@gmail.com
+
+def cntAtr(string:str):
+ c = 0
+ l = len(string)
+ while l > 0:
+ l -= 1
+ if string[l] == "*":
+ c += 1
+ elif string[l] == "|":
+ i = string[:l].rfind("|")
+ if i != -1:
+ l = i
+ return c
+
+import unittest
+
+class TestCntAtr(unittest.TestCase):
+ def test(self):
+ for inpt, otpt in {
+ "p|*e*rl|w**e|*ekly|": 2,
+ "perl": 0,
+ "th|ewe|e**|k|l***ych|alleng|e": 5,
+ }.items():
+ self.assertEqual(cntAtr(inpt),otpt)
+
+unittest.main()