aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2023-03-22 20:49:17 +0100
committerLubos Kolouch <lubos@kolouch.net>2023-03-22 20:49:17 +0100
commitbddcef2b9268841acef59fa885c69d5b9cf4c7c0 (patch)
tree5ab424c7e41ee86549d3862e29f8b41d1b1268e2
parent3632c6b2f592ae348527b479c182a08b0777e1da (diff)
downloadperlweeklychallenge-club-bddcef2b9268841acef59fa885c69d5b9cf4c7c0.tar.gz
perlweeklychallenge-club-bddcef2b9268841acef59fa885c69d5b9cf4c7c0.tar.bz2
perlweeklychallenge-club-bddcef2b9268841acef59fa885c69d5b9cf4c7c0.zip
Challenge 025 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
4 files changed, 323 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}")