From 021c7ff25d352c5e5849a6f10438e6f10db70573 Mon Sep 17 00:00:00 2001 From: Michael Manring Date: Tue, 11 Feb 2025 17:18:56 +1100 Subject: pwc308 solution in python --- challenge-308/pokgopun/python/ch-1.py | 49 +++++++++++++++++++++++ challenge-308/pokgopun/python/ch-2.py | 74 +++++++++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 challenge-308/pokgopun/python/ch-1.py create mode 100644 challenge-308/pokgopun/python/ch-2.py diff --git a/challenge-308/pokgopun/python/ch-1.py b/challenge-308/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..b68d5e5966 --- /dev/null +++ b/challenge-308/pokgopun/python/ch-1.py @@ -0,0 +1,49 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-308/ +""" + +Task 1: Count Common + +Submitted by: [46]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given two array of strings, @str1 and @str2. + + Write a script to return the count of common strings in both arrays. + +Example 1 + +Input: @str1 = ("perl", "weekly", "challenge") + @str2 = ("raku", "weekly", "challenge") +Output: 2 + +Example 2 + +Input: @str1 = ("perl", "raku", "python") + @str2 = ("python", "java") +Output: 1 + +Example 3 + +Input: @str1 = ("guest", "contribution") + @str2 = ("fun", "weekly", "challenge") +Output: 0 + +Task 2: Decode XOR +""" +### solution by pokgopun@gmail.com + +def countCommon(strs1: tuple[str], strs2: tuple[str]) -> int: + return len( set(strs1) & set(strs2) ) + +import unittest + +class TestCountCommon(unittest.TestCase): + def test(self): + for (strs1, strs2), otpt in { + (("perl", "weekly", "challenge"),("raku", "weekly", "challenge")): 2, + (("perl", "raku", "python"),("python", "java")): 1, + (("guest", "contribution"),("fun", "weekly", "challenge")): 0, + }.items(): + self.assertEqual(countCommon(strs1,strs2), otpt) + +unittest.main() diff --git a/challenge-308/pokgopun/python/ch-2.py b/challenge-308/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..5be727177d --- /dev/null +++ b/challenge-308/pokgopun/python/ch-2.py @@ -0,0 +1,74 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-308/ +""" + +Task 2: Decode XOR + +Submitted by: [47]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an encoded array and an initial integer. + + Write a script to find the original array that produced the given + encoded array. It was encoded such that encoded[i] = orig[i] XOR orig[i + + 1]. + +Example 1 + +Input: @encoded = (1, 2, 3), $initial = 1 +Output: (1, 0, 2, 1) + +Encoded array created like below, if the original array was (1, 0, 2, 1) +$encoded[0] = (1 xor 0) = 1 +$encoded[1] = (0 xor 2) = 2 +$encoded[2] = (2 xor 1) = 3 + +Example 2 + +Input: @encoded = (6, 2, 7, 3), $initial = 4 +Output: (4, 2, 0, 7, 4) + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 16^th February + 2025. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +def decodeXOR(enc: tuple[int], init: int) -> tuple[int]: + org = [init] + for i in range(len(enc)): + o = 0 + if enc[i] == org[i]: ### a ^ o = a --> o = 0 + pass + elif enc[i] == 0: ### a ^ o = 0 --> o = a + o = org[i] + elif org[i] == 0: ### 0 ^ o = a --> o = a + o = enc[i] + else: + #print(i) + dst, src = enc[i], org[i] + if dst < src: + dst, src = src, dst + b = 1 + while dst > 0: + o += b * abs(dst%2 - src%2) + b *= 2 + dst //= 2 + src //= 2 + org.append(o) + return tuple(org) + +import unittest + +class TestDecodeXOR(unittest.TestCase): + def test(self): + for (enc, init), otpt in { + ((1, 2, 3), 1): (1, 0, 2, 1), + ((6, 2, 7, 3), 4): (4, 2, 0, 7, 4), + }.items(): + #print(f'{enc}\n{otpt}') + self.assertEqual(decodeXOR(enc, init), otpt) + +unittest.main() -- cgit From 824dd6ce5ffa5e3545787712d213bbbf02639f47 Mon Sep 17 00:00:00 2001 From: Michael Manring Date: Tue, 11 Feb 2025 18:42:21 +1100 Subject: pwc308 solution in go --- challenge-308/pokgopun/go/ch-1.go | 88 +++++++++++++++++++++++++++++++++ challenge-308/pokgopun/go/ch-2.go | 100 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 188 insertions(+) create mode 100644 challenge-308/pokgopun/go/ch-1.go create mode 100644 challenge-308/pokgopun/go/ch-2.go diff --git a/challenge-308/pokgopun/go/ch-1.go b/challenge-308/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..58af71f6e4 --- /dev/null +++ b/challenge-308/pokgopun/go/ch-1.go @@ -0,0 +1,88 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-308/ +/*# + +Task 1: Count Common + +Submitted by: [46]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given two array of strings, @str1 and @str2. + + Write a script to return the count of common strings in both arrays. + +Example 1 + +Input: @str1 = ("perl", "weekly", "challenge") + @str2 = ("raku", "weekly", "challenge") +Output: 2 + +Example 2 + +Input: @str1 = ("perl", "raku", "python") + @str2 = ("python", "java") +Output: 1 + +Example 3 + +Input: @str1 = ("guest", "contribution") + @str2 = ("fun", "weekly", "challenge") +Output: 0 + +Task 2: Decode XOR +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +type strs []string + +type input struct { + str1, str2 strs +} + +func (in input) process() int { + m := make(map[string]bool) + for _, v := range in.str1 { + m[v] = true + } + c := 0 + for _, v := range in.str2 { + if m[v] { + c++ + m[v] = false + } + } + /* + for k, v := range m { + if v { + delete(m, k) + } + } + fmt.Println(m) + */ + return c +} + +func main() { + for _, data := range []struct { + input input + output int + }{ + {input{strs{"perl", "weekly", "challenge"}, strs{"raku", "weekly", "challenge"}}, 2}, + {input{strs{"perl", "raku", "python"}, strs{"python", "java"}}, 1}, + {input{strs{"guest", "contribution"}, strs{"fun", "weekly", "challenge"}}, 0}, + {input{strs{"perl", "raku", "perl"}, strs{"perl", "perl"}}, 1}, + {input{strs{"challenge", "challenge", "challenge"}, strs{"challenge", "weekly", "challenge"}}, 1}, + {input{strs{"challenge", "go", "challenge"}, strs{"go", "challenge", "go"}}, 2}, + } { + //fmt.Println(data.input) + io.WriteString(os.Stdout, cmp.Diff(data.input.process(), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-308/pokgopun/go/ch-2.go b/challenge-308/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..12f0bba5ea --- /dev/null +++ b/challenge-308/pokgopun/go/ch-2.go @@ -0,0 +1,100 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-308/ +/*# + +Task 2: Decode XOR + +Submitted by: [47]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an encoded array and an initial integer. + + Write a script to find the original array that produced the given + encoded array. It was encoded such that encoded[i] = orig[i] XOR orig[i + + 1]. + +Example 1 + +Input: @encoded = (1, 2, 3), $initial = 1 +Output: (1, 0, 2, 1) + +Encoded array created like below, if the original array was (1, 0, 2, 1) +$encoded[0] = (1 xor 0) = 1 +$encoded[1] = (0 xor 2) = 2 +$encoded[2] = (2 xor 1) = 3 + +Example 2 + +Input: @encoded = (6, 2, 7, 3), $initial = 4 +Output: (4, 2, 0, 7, 4) + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 16^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 ints []int + +type input struct { + enc ints + init int +} + +func (in input) process() ints { + enc := in.enc + l := len(enc) + org := make(ints, l+1) + org[0] = in.init + for i := range l { + orgn := 0 + switch { + case enc[i] == org[i]: + case enc[i] == 0: + orgn = org[i] + case org[i] == 0: + orgn = enc[i] + default: + b := 1 + dst, src := enc[i], org[i] + if dst < src { + dst, src = src, dst + } + for dst > 0 { + o := dst%2 - src%2 + if o < 0 { + o = 1 + } + orgn += b * o + b *= 2 + dst /= 2 + src /= 2 + } + } + org[i+1] = orgn + } + return org +} + +func main() { + for _, data := range []struct { + input input + output ints + }{ + {input{ints{1, 2, 3}, 1}, ints{1, 0, 2, 1}}, + {input{ints{6, 2, 7, 3}, 4}, ints{4, 2, 0, 7, 4}}, + } { + io.WriteString(os.Stdout, cmp.Diff(data.input.process(), data.output)) // blank if ok, otherwise show the difference + } +} -- cgit From c1c1323849a9659113f16d26f56d5548722de1e8 Mon Sep 17 00:00:00 2001 From: Michael Manring Date: Tue, 11 Feb 2025 20:21:22 +1100 Subject: pwc308 minor update on task#2 --- challenge-308/pokgopun/go/ch-2.go | 40 ++++++++++++++++------------------ challenge-308/pokgopun/python/ch-2.py | 41 +++++++++++++++++------------------ 2 files changed, 39 insertions(+), 42 deletions(-) diff --git a/challenge-308/pokgopun/go/ch-2.go b/challenge-308/pokgopun/go/ch-2.go index 12f0bba5ea..716421da88 100644 --- a/challenge-308/pokgopun/go/ch-2.go +++ b/challenge-308/pokgopun/go/ch-2.go @@ -58,29 +58,27 @@ func (in input) process() ints { org := make(ints, l+1) org[0] = in.init for i := range l { + dst, src := enc[i], org[i] + if dst == src { + continue + } + if dst > src { + dst, src = src, dst + } orgn := 0 - switch { - case enc[i] == org[i]: - case enc[i] == 0: - orgn = org[i] - case org[i] == 0: - orgn = enc[i] - default: - b := 1 - dst, src := enc[i], org[i] - if dst < src { - dst, src = src, dst - } - for dst > 0 { - o := dst%2 - src%2 - if o < 0 { - o = 1 - } - orgn += b * o - b *= 2 - dst /= 2 - src /= 2 + b := 1 + for dst > 0 { + o := dst%2 - src%2 + if o < 0 { + o = 1 } + orgn += b * o + b *= 2 + dst /= 2 + src /= 2 + } + if src > 0 { + orgn += b * src } org[i+1] = orgn } diff --git a/challenge-308/pokgopun/python/ch-2.py b/challenge-308/pokgopun/python/ch-2.py index 5be727177d..25d2716b60 100644 --- a/challenge-308/pokgopun/python/ch-2.py +++ b/challenge-308/pokgopun/python/ch-2.py @@ -37,27 +37,26 @@ SO WHAT DO YOU THINK ? ### solution by pokgopun@gmail.com def decodeXOR(enc: tuple[int], init: int) -> tuple[int]: - org = [init] - for i in range(len(enc)): - o = 0 - if enc[i] == org[i]: ### a ^ o = a --> o = 0 - pass - elif enc[i] == 0: ### a ^ o = 0 --> o = a - o = org[i] - elif org[i] == 0: ### 0 ^ o = a --> o = a - o = enc[i] - else: - #print(i) - dst, src = enc[i], org[i] - if dst < src: - dst, src = src, dst - b = 1 - while dst > 0: - o += b * abs(dst%2 - src%2) - b *= 2 - dst //= 2 - src //= 2 - org.append(o) + l = len(enc) + org = [0 for i in range(l+1)] + org[0] = init + for i in range(l): + dst, src = enc[i], org[i] + if dst == src: ### a ^ o = a --> o = 0 + continue + #print(i) + if dst > src: + dst, src = src, dst + orgn = 0 + b = 1 + while dst > 0: + orgn += b * abs(dst%2 - src%2) + b *= 2 + dst //= 2 + src //= 2 + if src > 0: + orgn += b * src + org[i+1] = orgn return tuple(org) import unittest -- cgit