aboutsummaryrefslogtreecommitdiff
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
parentd8179c22c12d35d4201bc8e3f759a4a8009e6b1b (diff)
downloadperlweeklychallenge-club-b744356b9515b100e6038dc131159feeb255278f.tar.gz
perlweeklychallenge-club-b744356b9515b100e6038dc131159feeb255278f.tar.bz2
perlweeklychallenge-club-b744356b9515b100e6038dc131159feeb255278f.zip
Challenge 308 LK Perl Python
-rw-r--r--challenge-308/lubos-kolouch/perl/ch-1.pl76
-rw-r--r--challenge-308/lubos-kolouch/perl/ch-2.pl93
-rw-r--r--challenge-308/lubos-kolouch/python/ch-1.py54
-rw-r--r--challenge-308/lubos-kolouch/python/ch-2.py68
4 files changed, 291 insertions, 0 deletions
diff --git a/challenge-308/lubos-kolouch/perl/ch-1.pl b/challenge-308/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..46fe045bc0
--- /dev/null
+++ b/challenge-308/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,76 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+use Test::More tests => 3;
+
+=pod
+
+=head1 Task 1: Count Common
+
+Given two array 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
+
+=cut
+
+# typedef: In this context, our inputs are array refs of strings (ArrayRef[Str]).
+
+sub count_common {
+ my ( $arr1_ref, $arr2_ref ) = @_;
+
+# Validate that each input is an array reference of strings (not enforced, but assumed)
+
+ # Use a hash to track the unique strings in the first array.
+ my %common;
+ foreach my $elem (@$arr1_ref) {
+ $common{$elem} = 1;
+ }
+
+ my $count = 0;
+
+ # For every element in the second array, check for existence in %common
+ foreach my $elem (@$arr2_ref) {
+ if ( exists $common{$elem} ) {
+ $count++;
+
+ # Remove to prevent duplicate counting if repeated in second array.
+ delete $common{$elem};
+ }
+ }
+ return $count;
+}
+
+# Unit tests
+is(
+ count_common(
+ [ "perl", "weekly", "challenge" ],
+ [ "raku", "weekly", "challenge" ]
+ ),
+ 2,
+ "Example 1"
+) or diag "Failed for Example 1";
+
+is( count_common( [ "perl", "raku", "python" ], [ "python", "java" ] ),
+ 1, "Example 2" )
+ or diag "Failed for Example 2";
+
+is(
+ count_common(
+ [ "guest", "contribution" ],
+ [ "fun", "weekly", "challenge" ]
+ ),
+ 0,
+ "Example 3"
+) or diag "Failed for Example 3";
diff --git a/challenge-308/lubos-kolouch/perl/ch-2.pl b/challenge-308/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..e6f208b6a3
--- /dev/null
+++ b/challenge-308/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,93 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use feature 'say';
+use Test::More tests => 3;
+
+=pod
+
+=head1 NAME
+
+xor_decode.pl - Task 2: Decode XOR
+
+=head1 DESCRIPTION
+
+This script decodes an original array from an encoded array based on the following encoding:
+
+ encoded[i] = original[i] XOR original[i+1]
+
+Given the encoded array and an initial value (the first element of original),
+the original array is restored.
+
+=head1 FUNCTIONS
+
+=head2 decode_xor
+
+ decode_xor($encoded_array_ref, $initial) => $original_array_ref
+
+Decodes the original array from an encoded array reference and an initial value.
+
+=over 4
+
+=item * C<$encoded_array_ref>: Reference to an array of integers (EncodedArray)
+
+=item * C<$initial>: An integer representing the first element of the original array
+
+=back
+
+Returns a reference to the decoded original array.
+
+=head1 TYPEDEFS
+
+=over
+
+=item * EncodedArray - ArrayRef[Integer]
+
+=item * OriginalArray - ArrayRef[Integer]
+
+=back
+
+=cut
+
+sub decode_xor {
+ my ( $encoded_ref, $initial ) = @_;
+
+ # Ensure $encoded_ref is an array reference
+ die "First argument must be an array reference"
+ unless ref $encoded_ref eq 'ARRAY';
+
+ my @original = ($initial);
+ for my $enc (@$encoded_ref) {
+
+ # Bitwise XOR operator in Perl is "^"
+ push @original, $original[-1] ^ $enc;
+ }
+ return \@original;
+}
+
+# Unit tests
+
+# Example 1:
+my $encoded_ref1 = [ 1, 2, 3 ];
+my $initial1 = 1;
+my $expected_ref1 = [ 1, 0, 2, 1 ];
+is_deeply( decode_xor( $encoded_ref1, $initial1 ),
+ $expected_ref1, 'Example 1 decode' );
+
+# Example 2:
+my $encoded_ref2 = [ 6, 2, 7, 3 ];
+my $initial2 = 4;
+my $expected_ref2 = [ 4, 2, 0, 7, 4 ];
+is_deeply( decode_xor( $encoded_ref2, $initial2 ),
+ $expected_ref2, 'Example 2 decode' );
+
+# Test with an empty encoded array:
+my $encoded_ref3 = [];
+my $initial3 = 10;
+my $expected_ref3 = [10];
+is_deeply( decode_xor( $encoded_ref3, $initial3 ),
+ $expected_ref3, 'Empty encoded decode' );
+
+done_testing();
+
+1;
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()