aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-02-07 13:13:26 +0000
committerGitHub <noreply@github.com>2025-02-07 13:13:26 +0000
commit50ea7eeede81b6247d1d51e7582edcefdfa169ed (patch)
tree6bc65882b10b3a185758b4655f554b1b98aeba38
parent06422ed09504a519eb58e639fe08543d06d86bf4 (diff)
parent0b2434c12ddacf2c45c89d9d489a90346e51fcfc (diff)
downloadperlweeklychallenge-club-50ea7eeede81b6247d1d51e7582edcefdfa169ed.tar.gz
perlweeklychallenge-club-50ea7eeede81b6247d1d51e7582edcefdfa169ed.tar.bz2
perlweeklychallenge-club-50ea7eeede81b6247d1d51e7582edcefdfa169ed.zip
Merge pull request #11537 from pokgopun/pwc307
Pwc307
-rw-r--r--challenge-307/pokgopun/go/ch-1.go83
-rw-r--r--challenge-307/pokgopun/go/ch-2.go104
-rw-r--r--challenge-307/pokgopun/python/ch-1.py65
-rw-r--r--challenge-307/pokgopun/python/ch-2.py75
4 files changed, 327 insertions, 0 deletions
diff --git a/challenge-307/pokgopun/go/ch-1.go b/challenge-307/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..d1594c8b69
--- /dev/null
+++ b/challenge-307/pokgopun/go/ch-1.go
@@ -0,0 +1,83 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-307/
+/*#
+
+Task 1: Check Order
+
+Submitted by: [42]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given an array of integers, @ints.
+
+ Write a script to re-arrange the given array in an increasing order and
+ return the indices where it differs from the original array.
+
+Example 1
+
+Input: @ints = (5, 2, 4, 3, 1)
+Output: (0, 2, 3, 4)
+
+Before: (5, 2, 4, 3, 1)
+After : (1, 2, 3, 4, 5)
+
+Difference at indices: (0, 2, 3, 4)
+
+Example 2
+
+Input: @ints = (1, 2, 1, 1, 3)
+Output: (1, 3)
+
+Before: (1, 2, 1, 1, 3)
+After : (1, 1, 1, 2, 3)
+
+Difference at indices: (1, 3)
+
+Example 3
+
+Input: @ints = (3, 1, 3, 2, 3)
+Output: (0, 1, 3)
+
+Before: (3, 1, 3, 2, 3)
+After : (1, 2, 3, 3, 3)
+
+Difference at indices: (0, 1, 3)
+
+Task 2: Find Anagrams
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+ "slices"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+type ints []int
+
+func (is ints) checkOrder() ints {
+ sints := make(ints, len(is))
+ copy(sints, is)
+ slices.Sort(sints)
+ var s ints
+ for i, v := range is {
+ if v != sints[i] {
+ s = append(s, i)
+ }
+ }
+ return s
+}
+
+func main() {
+ for _, data := range []struct {
+ input, output ints
+ }{
+ {ints{5, 2, 4, 3, 1}, ints{0, 2, 3, 4}},
+ {ints{1, 2, 1, 1, 3}, ints{1, 3}},
+ {ints{3, 1, 3, 2, 3}, ints{0, 1, 3}},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(data.input.checkOrder(), data.output)) // blank if ok, otherwise show the difference
+ }
+}
diff --git a/challenge-307/pokgopun/go/ch-2.go b/challenge-307/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..f10841ccbc
--- /dev/null
+++ b/challenge-307/pokgopun/go/ch-2.go
@@ -0,0 +1,104 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-307/
+/*#
+
+Task 2: Find Anagrams
+
+Submitted by: [43]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a list of words, @words.
+
+ Write a script to find any two consecutive words and if they are
+ anagrams, drop the first word and keep the second. You continue this
+ until there is no more anagrams in the given list and return the count
+ of final list.
+
+Example 1
+
+Input: @words = ("acca", "dog", "god", "perl", "repl")
+Output: 3
+
+Step 1: "dog" and "god" are anagrams, so dropping "dog" and keeping "god" => ("a
+cca", "god", "perl", "repl")
+Step 2: "perl" and "repl" are anagrams, so dropping "perl" and keeping "repl" =>
+ ("acca", "god", "repl")
+
+Example 2
+
+Input: @words = ("abba", "baba", "aabb", "ab", "ab")
+Output: 2
+
+Step 1: "abba" and "baba" are anagrams, so dropping "abba" and keeping "baba" =>
+ ("baba", "aabb", "ab", "ab")
+Step 2: "baba" and "aabb" are anagrams, so dropping "baba" and keeping "aabb" =>
+ ("aabb", "ab", "ab")
+Step 3: "ab" and "ab" are anagrams, so dropping "ab" and keeping "ab" => ("aabb"
+, "ab")
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 9^th February
+ 2025.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+type word string
+
+func (w word) isAnagram(wrd word) bool {
+ if len(w) != len(wrd) {
+ return false
+ }
+ m := make(map[rune]int)
+ for _, v := range wrd {
+ m[v]++
+ }
+ for _, v := range w {
+ m[v]--
+ if m[v] == 0 {
+ delete(m, v)
+ }
+ }
+ return len(m) == 0
+}
+
+type words []word
+
+func findAnagrams(ws words) int {
+ i := 0
+ for {
+ if i+1 >= len(ws) {
+ break
+ }
+ if ws[i].isAnagram(ws[i+1]) {
+ copy((ws)[i:], ws[i+1:])
+ ws = ws[:len(ws)-1]
+ } else {
+ i++
+ }
+ }
+ //fmt.Println(ws)
+ return len(ws)
+}
+
+func main() {
+ for _, data := range []struct {
+ input words
+ output int
+ }{
+ {words{"acca", "dog", "god", "perl", "repl"}, 3},
+ {words{"abba", "baba", "aabb", "ab", "ab"}, 2},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(findAnagrams(data.input), data.output)) // blank if ok, otherwise show the difference
+ }
+}
diff --git a/challenge-307/pokgopun/python/ch-1.py b/challenge-307/pokgopun/python/ch-1.py
new file mode 100644
index 0000000000..be785600a2
--- /dev/null
+++ b/challenge-307/pokgopun/python/ch-1.py
@@ -0,0 +1,65 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-307/
+"""
+
+Task 1: Check Order
+
+Submitted by: [42]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given an array of integers, @ints.
+
+ Write a script to re-arrange the given array in an increasing order and
+ return the indices where it differs from the original array.
+
+Example 1
+
+Input: @ints = (5, 2, 4, 3, 1)
+Output: (0, 2, 3, 4)
+
+Before: (5, 2, 4, 3, 1)
+After : (1, 2, 3, 4, 5)
+
+Difference at indices: (0, 2, 3, 4)
+
+Example 2
+
+Input: @ints = (1, 2, 1, 1, 3)
+Output: (1, 3)
+
+Before: (1, 2, 1, 1, 3)
+After : (1, 1, 1, 2, 3)
+
+Difference at indices: (1, 3)
+
+Example 3
+
+Input: @ints = (3, 1, 3, 2, 3)
+Output: (0, 1, 3)
+
+Before: (3, 1, 3, 2, 3)
+After : (1, 2, 3, 3, 3)
+
+Difference at indices: (0, 1, 3)
+
+Task 2: Find Anagrams
+"""
+### solution by pokgopun@gmail.com
+
+from typing import Tuple
+
+def checkOrder(ints: Tuple[int]) -> Tuple[int]:
+ sints = sorted(ints)
+ return tuple( i for i in range(len(ints)) if ints[i] != sints[i] )
+
+import unittest
+
+class TestCheckOrder(unittest.TestCase):
+ def test(self):
+ for inpt, otpt in {
+ (5, 2, 4, 3, 1): (0, 2, 3, 4),
+ (1, 2, 1, 1, 3): (1, 3),
+ (3, 1, 3, 2, 3): (0, 1, 3),
+ }.items():
+ self.assertEqual(checkOrder(inpt), otpt)
+
+unittest.main()
diff --git a/challenge-307/pokgopun/python/ch-2.py b/challenge-307/pokgopun/python/ch-2.py
new file mode 100644
index 0000000000..150b9f8210
--- /dev/null
+++ b/challenge-307/pokgopun/python/ch-2.py
@@ -0,0 +1,75 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-307/
+"""
+
+Task 2: Find Anagrams
+
+Submitted by: [43]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a list of words, @words.
+
+ Write a script to find any two consecutive words and if they are
+ anagrams, drop the first word and keep the second. You continue this
+ until there is no more anagrams in the given list and return the count
+ of final list.
+
+Example 1
+
+Input: @words = ("acca", "dog", "god", "perl", "repl")
+Output: 3
+
+Step 1: "dog" and "god" are anagrams, so dropping "dog" and keeping "god" => ("a
+cca", "god", "perl", "repl")
+Step 2: "perl" and "repl" are anagrams, so dropping "perl" and keeping "repl" =>
+ ("acca", "god", "repl")
+
+Example 2
+
+Input: @words = ("abba", "baba", "aabb", "ab", "ab")
+Output: 2
+
+Step 1: "abba" and "baba" are anagrams, so dropping "abba" and keeping "baba" =>
+ ("baba", "aabb", "ab", "ab")
+Step 2: "baba" and "aabb" are anagrams, so dropping "baba" and keeping "aabb" =>
+ ("aabb", "ab", "ab")
+Step 3: "ab" and "ab" are anagrams, so dropping "ab" and keeping "ab" => ("aabb"
+, "ab")
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 9^th February
+ 2025.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+"""
+### solution by pokgopun@gmail.com
+
+from typing import Tuple
+
+def findAnagrams(words: Tuple[str]) -> Tuple[str]:
+ words = list(words)
+ i = 0
+ while True:
+ if i + 1 >= len(words):
+ break
+ cur,nxt = words[i], words[i+1]
+ if len(cur) == len(nxt) and set(cur) == set(nxt):
+ words.pop(i)
+ else:
+ i += 1
+ #print(words)
+ return len(words)
+
+import unittest
+
+class TestFindAnagrams(unittest.TestCase):
+ def test(self):
+ for inpt, otpt in {
+ ("acca", "dog", "god", "perl", "repl"): 3,
+ ("abba", "baba", "aabb", "ab", "ab"): 2,
+ }.items():
+ self.assertEqual(findAnagrams(inpt), otpt)
+
+unittest.main()
+
+