aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-03-25 17:12:34 +0000
committerGitHub <noreply@github.com>2023-03-25 17:12:34 +0000
commitea00774e70d8cb3fa15e1af16094c257b20ab005 (patch)
tree73d89ba53f37b40d61638c65bfd441b85e79f1ef
parent1d97a43712ffabc3a2fb1bd1706c41e65437f198 (diff)
parentbddcef2b9268841acef59fa885c69d5b9cf4c7c0 (diff)
downloadperlweeklychallenge-club-ea00774e70d8cb3fa15e1af16094c257b20ab005.tar.gz
perlweeklychallenge-club-ea00774e70d8cb3fa15e1af16094c257b20ab005.tar.bz2
perlweeklychallenge-club-ea00774e70d8cb3fa15e1af16094c257b20ab005.zip
Merge pull request #7777 from LubosKolouch/master
Challenge 209 LK Perl Python
-rw-r--r--challenge-025/lubos-kolouch/perl/ch-1.pl41
-rw-r--r--challenge-025/lubos-kolouch/perl/ch-2.pl81
-rw-r--r--challenge-025/lubos-kolouch/python/ch-1.py109
-rw-r--r--challenge-025/lubos-kolouch/python/ch-2.py92
-rw-r--r--challenge-209/lubos-kolouch/perl/ch-1.pl40
-rw-r--r--challenge-209/lubos-kolouch/perl/ch-2.pl61
-rw-r--r--challenge-209/lubos-kolouch/python/ch-1.py59
-rw-r--r--challenge-209/lubos-kolouch/python/ch-2.py57
8 files changed, 540 insertions, 0 deletions
diff --git a/challenge-025/lubos-kolouch/perl/ch-1.pl b/challenge-025/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..1193b211bd
--- /dev/null
+++ b/challenge-025/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,41 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+my @names =
+ qw(audino bagon baltoy banette bidoof braviary bronzor carracosta charmeleon cresselia croagunk darmanitan deino emboar emolga exeggcute gabite girafarig gulpin haxorus heatmor heatran ivysaur jellicent jumpluff kangaskhan kricketune landorus ledyba loudred lumineon lunatone machamp magnezone mamoswine nosepass petilil pidgeotto pikachu pinsir poliwrath poochyena porygon2 porygonz registeel relicanth remoraid rufflet sableye scolipede scrafty seaking sealeo silcoon simisear snivy snorlax spoink starly tirtouga trapinch treecko tyrogue vigoroth vulpix wailord wartortle whismur wingull yamask);
+
+my %used;
+my @sequence;
+
+foreach my $name (@names) {
+ next if $used{$name};
+ my $current = $name;
+ my @temp = ($current);
+ my %temp_used = %used;
+ $temp_used{$current} = 1;
+
+ while (1) {
+ my $found = 0;
+ foreach my $next (@names) {
+ next if $temp_used{$next};
+ if ( substr( $current, -1 ) eq substr( $next, 0, 1 ) ) {
+ $current = $next;
+ push( @temp, $current );
+ $temp_used{$current} = 1;
+ $found = 1;
+ last;
+ }
+ }
+ last unless $found;
+ }
+
+ if ( scalar(@temp) > scalar(@sequence) ) {
+ @sequence = @temp;
+ }
+
+ %used = %temp_used;
+}
+
+print join( " -> ", @sequence ) . "\n";
diff --git a/challenge-025/lubos-kolouch/perl/ch-2.pl b/challenge-025/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..089e44615a
--- /dev/null
+++ b/challenge-025/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,81 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+# Define the Chaocipher alphabets
+my $left_alphabet = "HXUCZVAMDSLKPEFJRIGTWOBNYQ";
+my $right_alphabet = "PTLNBQDEOYSFAVZKGJRIHWXUMC";
+
+sub chaocipher_encrypt {
+ my $message = shift;
+ my $ciphertext = "";
+ my $left_index = 0;
+ my $right_index = 0;
+
+ # Loop through each character in the message
+ foreach my $char ( split //, uc($message) ) {
+ if ( $char !~ /[A-Z]/ ) {
+
+ # Ignore non-alphabetic characters
+ $ciphertext .= $char;
+ next;
+ }
+
+ # Find the index of the character in the left alphabet
+ my $left_char_index = index( $left_alphabet, $char );
+
+ # Swap the left and right indices
+ ( $left_index, $right_index ) = ( $right_index, $left_index );
+
+ # Find the corresponding character in the right alphabet
+ my $right_char =
+ substr( $right_alphabet, ( $left_char_index + $right_index ) % 26,
+ 1 );
+
+ # Append the encrypted character to the ciphertext
+ $ciphertext .= $right_char;
+ }
+
+ return $ciphertext;
+}
+
+sub chaocipher_decrypt {
+ my $ciphertext = shift;
+ my $plaintext = "";
+ my $left_index = 0;
+ my $right_index = 0;
+
+ # Loop through each character in the ciphertext
+ foreach my $char ( split //, uc($ciphertext) ) {
+ if ( $char !~ /[A-Z]/ ) {
+
+ # Ignore non-alphabetic characters
+ $plaintext .= $char;
+ next;
+ }
+
+ # Find the index of the character in the right alphabet
+ my $right_char_index = index( $right_alphabet, $char );
+
+ # Swap the left and right indices
+ ( $left_index, $right_index ) = ( $right_index, $left_index );
+
+ # Find the corresponding character in the left alphabet
+ my $left_char =
+ substr( $left_alphabet, ( $right_char_index - $right_index ) % 26,
+ 1 );
+
+ # Append the decrypted character to the plaintext
+ $plaintext .= $left_char;
+ }
+
+ return $plaintext;
+}
+
+# Example usage
+my $message = "Hello World!";
+my $ciphertext = chaocipher_encrypt($message);
+print "Ciphertext: $ciphertext\n";
+my $plaintext = chaocipher_decrypt($ciphertext);
+print "Plaintext: $plaintext\n";
diff --git a/challenge-025/lubos-kolouch/python/ch-1.py b/challenge-025/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..519cc6022a
--- /dev/null
+++ b/challenge-025/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,109 @@
+#!/usr/bin/env python3
+
+names = [
+ "audino",
+ "bagon",
+ "baltoy",
+ "banette",
+ "bidoof",
+ "braviary",
+ "bronzor",
+ "carracosta",
+ "charmeleon",
+ "cresselia",
+ "croagunk",
+ "darmanitan",
+ "deino",
+ "emboar",
+ "emolga",
+ "exeggcute",
+ "gabite",
+ "girafarig",
+ "gulpin",
+ "haxorus",
+ "heatmor",
+ "heatran",
+ "ivysaur",
+ "jellicent",
+ "jumpluff",
+ "kangaskhan",
+ "kricketune",
+ "landorus",
+ "ledyba",
+ "loudred",
+ "lumineon",
+ "lunatone",
+ "machamp",
+ "magnezone",
+ "mamoswine",
+ "nosepass",
+ "petilil",
+ "pidgeotto",
+ "pikachu",
+ "pinsir",
+ "poliwrath",
+ "poochyena",
+ "porygon2",
+ "porygonz",
+ "registeel",
+ "relicanth",
+ "remoraid",
+ "rufflet",
+ "sableye",
+ "scolipede",
+ "scrafty",
+ "seaking",
+ "sealeo",
+ "silcoon",
+ "simisear",
+ "snivy",
+ "snorlax",
+ "spoink",
+ "starly",
+ "tirtouga",
+ "trapinch",
+ "treecko",
+ "tyrogue",
+ "vigoroth",
+ "vulpix",
+ "wailord",
+ "wartortle",
+ "whismur",
+ "wingull",
+ "yamask",
+]
+
+used = {}
+sequence = []
+
+for name in names:
+ if name in used:
+ continue
+
+ current = name
+ temp = [current]
+ temp_used = used.copy()
+ temp_used[current] = True
+
+ while True:
+ found = False
+ for next_name in names:
+ if next_name in temp_used:
+ continue
+
+ if current[-1] == next_name[0]:
+ current = next_name
+ temp.append(current)
+ temp_used[current] = True
+ found = True
+ break
+
+ if not found:
+ break
+
+ if len(temp) > len(sequence):
+ sequence = temp
+
+ used = temp_used
+
+print(" -> ".join(sequence))
diff --git a/challenge-025/lubos-kolouch/python/ch-2.py b/challenge-025/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..2100aeb960
--- /dev/null
+++ b/challenge-025/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,92 @@
+def chaocipher_encrypt(message: str) -> str:
+ """
+ Encrypts a message using the Chaocipher algorithm.
+
+ Chaocipher is a symmetric encryption algorithm that uses two mixed alphabets to
+ perform a double substitution on each letter of the plaintext. The two alphabets
+ are predetermined and fixed.
+
+ Args:
+ message: The message to be encrypted.
+
+ Returns:
+ The encrypted message.
+ """
+ # Define the Chaocipher alphabets
+ left_alphabet = "HXUCZVAMDSLKPEFJRIGTWOBNYQ"
+ right_alphabet = "PTLNBQDEOYSFAVZKGJRIHWXUMC"
+
+ ciphertext = ""
+ left_index = 0
+ right_index = 0
+
+ # Loop through each character in the message
+ for char in message.upper():
+ if not char.isalpha():
+ # Ignore non-alphabetic characters
+ ciphertext += char
+ continue
+
+ # Find the index of the character in the left alphabet
+ left_char_index = left_alphabet.index(char)
+
+ # Swap the left and right indices
+ left_index, right_index = right_index, left_index
+
+ # Find the corresponding character in the right alphabet
+ right_char_index = (left_char_index + right_index) % 26
+ right_char = right_alphabet[right_char_index]
+
+ # Append the encrypted character to the ciphertext
+ ciphertext += right_char
+
+ return ciphertext
+
+
+def chaocipher_decrypt(ciphertext: str) -> str:
+ """
+ Decrypts a message that has been encrypted using the Chaocipher algorithm.
+
+ Args:
+ ciphertext: The message to be decrypted.
+
+ Returns:
+ The decrypted message.
+ """
+ # Define the Chaocipher alphabets
+ left_alphabet = "HXUCZVAMDSLKPEFJRIGTWOBNYQ"
+ right_alphabet = "PTLNBQDEOYSFAVZKGJRIHWXUMC"
+
+ plaintext = ""
+ left_index = 0
+ right_index = 0
+
+ # Loop through each character in the ciphertext
+ for char in ciphertext.upper():
+ if not char.isalpha():
+ # Ignore non-alphabetic characters
+ plaintext += char
+ continue
+
+ # Find the index of the character in the right alphabet
+ right_char_index = right_alphabet.index(char)
+
+ # Swap the left and right indices
+ left_index, right_index = right_index, left_index
+
+ # Find the corresponding character in the left alphabet
+ left_char_index = (right_char_index - right_index) % 26
+ left_char = left_alphabet[left_char_index]
+
+ # Append the decrypted character to the plaintext
+ plaintext += left_char
+
+ return plaintext
+
+
+# Example usage
+message = "Hello World!"
+ciphertext = chaocipher_encrypt(message)
+print(f"Ciphertext: {ciphertext}")
+plaintext = chaocipher_decrypt(ciphertext)
+print(f"Plaintext: {plaintext}")
diff --git a/challenge-209/lubos-kolouch/perl/ch-1.pl b/challenge-209/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..99d4724965
--- /dev/null
+++ b/challenge-209/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,40 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use Test::More;
+
+sub decode_bits {
+ my ($bits) = @_;
+
+ # Decode the binary bits and store the characters in @decoded array
+ my @decoded;
+ my $i = 0;
+ while ($i < scalar @$bits) {
+ if ($bits->[$i] == 0) {
+ push @decoded, 'a';
+ $i++;
+ } elsif ($bits->[$i] == 1 && $bits->[$i+1] == 0) {
+ push @decoded, 'b';
+ $i += 2;
+ } elsif ($bits->[$i] == 1 && $bits->[$i+1] == 1) {
+ push @decoded, 'c';
+ $i += 2;
+ }
+ }
+
+ return \@decoded;
+}
+
+# Test case 1
+my $bits1 = [1, 0, 0];
+my $decoded1 = decode_bits($bits1);
+is($decoded1->[-1], 'a', 'Test case 1 - last character is "a"');
+
+# Test case 2
+my $bits2 = [1, 1, 1, 0];
+my $decoded2 = decode_bits($bits2);
+ok($decoded2->[-1] eq 'a' ? 0 : 1, 'Test case 2 - last character is "a" or not');
+
+done_testing();
+
diff --git a/challenge-209/lubos-kolouch/perl/ch-2.pl b/challenge-209/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..418149e074
--- /dev/null
+++ b/challenge-209/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,61 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use Test::More tests => 1;
+
+sub merge_email_lists {
+ my @accounts = @_;
+
+ my @merged_lists;
+
+ # Process the input and create the merged lists
+ foreach my $account (@accounts) {
+ my ($list_name, @emails) = @$account;
+ my $list_found = 0;
+
+ # Check if the list name already exists in the merged lists
+ foreach my $merged_list (@merged_lists) {
+ my ($merged_name, @merged_emails) = @$merged_list;
+
+ # If the list names match and they share a common email address, merge the lists
+ if ($list_name eq $merged_name && (grep { my $email = $_; grep { $email eq $_ } @emails } @merged_emails)) {
+ push @$merged_list, @emails;
+ $list_found = 1;
+ last;
+ }
+ }
+
+ # If the list name is not found in the merged lists, add the current list
+ push @merged_lists, $account unless $list_found;
+ }
+
+ # Remove duplicate email addresses from each list and sort them
+ foreach my $merged_list (@merged_lists) {
+ my ($list_name, @emails) = @$merged_list;
+ my %seen;
+ @$merged_list = ($list_name, sort grep { !$seen{$_}++ } @emails);
+ }
+
+ return \@merged_lists;
+}
+
+# Test data
+my @test_accounts = (
+ ["A", 'a1@@a.com', 'a2@@a.com'],
+ ["B", 'b1@@b.com'],
+ ["A", 'a3@@a.com'],
+ ["B", 'b2@@b.com', 'b1@@b.com']
+);
+
+# Test result
+my $merged_email_lists = merge_email_lists(@test_accounts);
+my @expected_email_lists = (
+ ["A", 'a1@@a.com', 'a2@@a.com'],
+ ["B", 'b1@@b.com', 'b2@@b.com'],
+ ["A", 'a3@@a.com']
+);
+
+# Perform the test
+is_deeply($merged_email_lists, \@expected_email_lists, 'Test merging email lists with common email addresses');
+
diff --git a/challenge-209/lubos-kolouch/python/ch-1.py b/challenge-209/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..b5524f7136
--- /dev/null
+++ b/challenge-209/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,59 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+from typing import List
+
+
+def decode_bits(bits: List[int]) -> List[str]:
+ """
+ Decode a list of binary bits into characters according to the following rules:
+ [0] -> 'a'
+ [1, 0] -> 'b'
+ [1, 1] -> 'c'
+ Args:
+ bits: A list of binary bits ending with 0.
+ Returns:
+ A list of characters decoded from the binary bits.
+ """
+ decoded = []
+ i = 0
+ while i < len(bits):
+ if bits[i] == 0:
+ decoded.append('a')
+ i += 1
+ elif bits[i] == 1 and bits[i + 1] == 0:
+ decoded.append('b')
+ i += 2
+ elif bits[i] == 1 and bits[i + 1] == 1:
+ decoded.append('c')
+ i += 2
+ return decoded
+
+
+def is_last_character_a(bits: List[int]) -> int:
+ """
+ Check if the last character decoded from the binary bits is 'a'.
+ Args:
+ bits: A list of binary bits ending with 0.
+ Returns:
+ 1 if the last character decoded is 'a', 0 otherwise.
+ """
+ decoded = decode_bits(bits)
+ return 1 if decoded[-1] == 'a' else 0
+
+
+import unittest
+
+
+class TestDecodeBits(unittest.TestCase):
+
+ def test_decode_bits(self):
+ self.assertEqual(decode_bits([1, 0, 0]), ['b', 'a'])
+ self.assertEqual(decode_bits([1, 1, 1, 0]), ['c', 'b'])
+
+ def test_is_last_character_a(self):
+ self.assertEqual(is_last_character_a([1, 0, 0]), 1)
+ self.assertEqual(is_last_character_a([1, 1, 1, 0]), 0)
+
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/challenge-209/lubos-kolouch/python/ch-2.py b/challenge-209/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..f67cb508ab
--- /dev/null
+++ b/challenge-209/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,57 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+import unittest
+from typing import List, Tuple
+
+
+def merge_email_lists(
+ accounts: List[Tuple[str, List[str]]]) -> List[Tuple[str, List[str]]]:
+ merged_lists = []
+
+ for account in accounts:
+ list_name, emails = account
+ list_found = False
+
+ for merged_list in merged_lists:
+ merged_name, merged_emails = merged_list
+
+ # If the list names match and they share a common email address, merge the lists
+ if list_name == merged_name and any(email in emails
+ for email in merged_emails):
+ merged_list[1].extend(emails)
+ list_found = True
+ break
+
+ # If the list name is not found in the merged lists, add the current list
+ if not list_found:
+ merged_lists.append([list_name, emails])
+
+ # Remove duplicate email addresses from each list and sort them
+ for merged_list in merged_lists:
+ merged_list[1] = sorted(set(merged_list[1]))
+
+ return merged_lists
+
+
+class TestMergeEmailLists(unittest.TestCase):
+
+ def test_merge_email_lists(self):
+ test_accounts = [
+ ("A", ["a1@a.com", "a2@a.com"]),
+ ("B", ["b1@b.com"]),
+ ("A", ["a3@a.com"]),
+ ("B", ["b2@b.com", "b1@b.com"]),
+ ]
+
+ expected_email_lists = [
+ ["A", ["a1@a.com", "a2@a.com"]],
+ ["B", ["b1@b.com", "b2@b.com"]],
+ ["A", ["a3@a.com"]],
+ ]
+
+ self.assertEqual(merge_email_lists(test_accounts),
+ expected_email_lists)
+
+
+if __name__ == "__main__":
+ unittest.main()