aboutsummaryrefslogtreecommitdiff
path: root/challenge-308/lubos-kolouch/python
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2025-02-11 15:11:02 +0100
committerLubos Kolouch <lubos@kolouch.net>2025-02-11 15:11:02 +0100
commitb744356b9515b100e6038dc131159feeb255278f (patch)
tree3993685fc154feec90d07b296d9332e5030694d2 /challenge-308/lubos-kolouch/python
parentd8179c22c12d35d4201bc8e3f759a4a8009e6b1b (diff)
downloadperlweeklychallenge-club-b744356b9515b100e6038dc131159feeb255278f.tar.gz
perlweeklychallenge-club-b744356b9515b100e6038dc131159feeb255278f.tar.bz2
perlweeklychallenge-club-b744356b9515b100e6038dc131159feeb255278f.zip
Challenge 308 LK Perl Python
Diffstat (limited to 'challenge-308/lubos-kolouch/python')
-rw-r--r--challenge-308/lubos-kolouch/python/ch-1.py54
-rw-r--r--challenge-308/lubos-kolouch/python/ch-2.py68
2 files changed, 122 insertions, 0 deletions
diff --git a/challenge-308/lubos-kolouch/python/ch-1.py b/challenge-308/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..227a3fa942
--- /dev/null
+++ b/challenge-308/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,54 @@
+#!/usr/bin/env python3
+"""
+Task 1: Count Common
+
+Given two arrays of strings, str1 and str2,
+return the count of common strings in both arrays.
+
+Examples:
+ Input: str1 = ["perl", "weekly", "challenge"]
+ str2 = ["raku", "weekly", "challenge"]
+ Output: 2
+
+ Input: str1 = ["perl", "raku", "python"]
+ str2 = ["python", "java"]
+ Output: 1
+
+ Input: str1 = ["guest", "contribution"]
+ str2 = ["fun", "weekly", "challenge"]
+ Output: 0
+
+"""
+
+import unittest
+
+# typedef: Define a type alias for list of strings.
+StrList = list[str]
+
+
+def count_common(str1: StrList, str2: StrList) -> int:
+ """
+ Count the number of common strings between two lists.
+ """
+ return len(set(str1).intersection(str2))
+
+
+class TestCountCommon(unittest.TestCase):
+
+ def test_example1(self):
+ self.assertEqual(
+ count_common(["perl", "weekly", "challenge"],
+ ["raku", "weekly", "challenge"]), 2)
+
+ def test_example2(self):
+ self.assertEqual(
+ count_common(["perl", "raku", "python"], ["python", "java"]), 1)
+
+ def test_example3(self):
+ self.assertEqual(
+ count_common(["guest", "contribution"],
+ ["fun", "weekly", "challenge"]), 0)
+
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/challenge-308/lubos-kolouch/python/ch-2.py b/challenge-308/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..11b038dc85
--- /dev/null
+++ b/challenge-308/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,68 @@
+#!/usr/bin/env python3
+"""
+Module: xor_decode
+Task 2: Decode XOR
+
+Given an encoded array where each element is the XOR of consecutive elements from the original array,
+this module provides a function to restore the original array from the encoded array and an initial value.
+
+The encoding is defined as:
+ encoded[i] = original[i] XOR original[i+1]
+
+Example:
+ Input: encoded = [1, 2, 3], initial = 1
+ Output: [1, 0, 2, 1]
+"""
+
+from typing import NewType
+import unittest
+
+# Type definitions for clarity
+EncodedArray = NewType("EncodedArray", list[int]) # EncodedArray: list of int
+OriginalArray = list[int] # OriginalArray: list of int
+
+
+def decode_xor(encoded: list[int], initial: int) -> OriginalArray:
+ """
+ Decode the original array from an encoded array and an initial value.
+
+ The encoded array is defined such that:
+ encoded[i] = original[i] XOR original[i+1]
+
+ Args:
+ encoded (List[int]): A list of integers representing the XOR encoded array.
+ initial (int): The first element of the original array.
+
+ Returns:
+ List[int]: The decoded original array.
+
+ Examples:
+ >>> decode_xor([1, 2, 3], 1)
+ [1, 0, 2, 1]
+ """
+ original = [initial]
+ for value in encoded:
+ # The inverse of XOR: original[i+1] = original[i] XOR encoded[i]
+ original.append(original[-1] ^ value)
+ return original
+
+
+# Unit tests
+class TestDecodeXOR(unittest.TestCase):
+ """Unit tests for the decode_xor function."""
+
+ def test_example1(self):
+ """Test with first example."""
+ self.assertEqual(decode_xor([1, 2, 3], 1), [1, 0, 2, 1])
+
+ def test_example2(self):
+ """Test with second example."""
+ self.assertEqual(decode_xor([6, 2, 7, 3], 4), [4, 2, 0, 7, 4])
+
+ def test_empty_encoded(self):
+ """Test with an empty encoded array."""
+ self.assertEqual(decode_xor([], 10), [10])
+
+
+if __name__ == '__main__':
+ unittest.main()