diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2019-11-19 14:31:18 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2019-11-19 14:31:18 +0000 |
| commit | a672e0d747ab37b4b54521c192e68b445b1c73ef (patch) | |
| tree | 8a623e4f0bad2516a27e59d41cef6667f3c8366f /challenge-035 | |
| parent | e3e4db36399c4389908df819e45e242c4dbcc492 (diff) | |
| download | perlweeklychallenge-club-a672e0d747ab37b4b54521c192e68b445b1c73ef.tar.gz perlweeklychallenge-club-a672e0d747ab37b4b54521c192e68b445b1c73ef.tar.bz2 perlweeklychallenge-club-a672e0d747ab37b4b54521c192e68b445b1c73ef.zip | |
- Added solutions by Javier Luque.
Diffstat (limited to 'challenge-035')
| -rw-r--r-- | challenge-035/javier-luque/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-035/javier-luque/perl5/ch-1.pl | 70 | ||||
| -rw-r--r-- | challenge-035/javier-luque/perl5/ch-2.pl | 119 | ||||
| -rw-r--r-- | challenge-035/javier-luque/perl6/ch-1.p6 | 63 | ||||
| -rw-r--r-- | challenge-035/javier-luque/perl6/ch-2.p6 | 61 |
5 files changed, 314 insertions, 0 deletions
diff --git a/challenge-035/javier-luque/blog.txt b/challenge-035/javier-luque/blog.txt new file mode 100644 index 0000000000..11f4704553 --- /dev/null +++ b/challenge-035/javier-luque/blog.txt @@ -0,0 +1 @@ +https://perlchallenges.wordpress.com/2019/11/18/perl-weekly-challenge-035/ diff --git a/challenge-035/javier-luque/perl5/ch-1.pl b/challenge-035/javier-luque/perl5/ch-1.pl new file mode 100644 index 0000000000..0783e844de --- /dev/null +++ b/challenge-035/javier-luque/perl5/ch-1.pl @@ -0,0 +1,70 @@ +#!/usr/bin/perl +# Test: ./ch1.pl 'string to test' +use strict; +use warnings; +use feature qw /say/; + +say encode(join ' ', @ARGV); + +sub encode { + my $test_string = shift; + my $converted_string = ''; + + # Some definitions + my $dot = '1'; + my $dash = '1' x 3; + my $gap = '0'; + my $char_gap = '0' x 3; + my $word_gap = '0' x 7; + + # For my sanity I just copied and paste real morse code + # This could be cleaner + my %morse = qw( + A .- B -... C -.-. D -.. E . F ..-. + G --. H .... I .. J .--- K -.- L .-.. + M -- N -. O --- P .--. Q --.- R .-. + S ... T - U ..- V ...- W .-- + X -..- Y -.-- Z --.. + 0 ----- 1 .---- 2 ..--- 3 ...-- 4 ....- + 5 ..... 6 -.... 7 --... 8 ---.. 9 ----. + . .-.-.- / -...- : ---... ' .----. + - -....- ? ..--.. ! ..--. @ ...-.- + .-.-. + ); + + # Stop perl from spitting out a warning + # about a comma in qw + $morse{','} = '--..--'; + + # Convert the dot and dashes to the required 0 and 1's + for my $key (sort keys %morse) { + my $code = $morse{$key}; + $code =~ s/\.$/$dot/; # trailing $dot + $code =~ s/\-$/$dash/; # trailing $dash + $code =~ s/\./$dot$gap/g; # all other dots + $code =~ s/\-/$dash$gap/g; # all other dashes + $morse{$key} = $code; + } + + # Split words + my @words = split('\s+', $test_string); + for my $i (0 .. $#words) { + + # Split characters + my @chars = split('', $words[$i]); + for my $j (0 .. $#chars) { + # Translate the character + $converted_string .= $morse{uc($chars[$j])}; + + # Add the character gap + $converted_string .= $char_gap + unless ($j == $#chars); + } + + # Add the word gap + $converted_string .= $word_gap + unless ($i == $#words); + } + + return $converted_string; +} + diff --git a/challenge-035/javier-luque/perl5/ch-2.pl b/challenge-035/javier-luque/perl5/ch-2.pl new file mode 100644 index 0000000000..2e218f956c --- /dev/null +++ b/challenge-035/javier-luque/perl5/ch-2.pl @@ -0,0 +1,119 @@ +#!/usr/bin/perl +# Test: ./ch1.pl 'string to test' | ./ch2.pl +use strict; +use warnings; +use feature qw /say/; + +say decode(<STDIN>); + +sub decode { + my $encoded_string = shift; + my $converted_string = ''; + chomp $encoded_string; + + # Do some error correction + if (scalar(@ARGV)) { + $encoded_string = + simulate_error($encoded_string, $ARGV[0]); + $encoded_string = + error_correction($encoded_string); + } + + # Some definitions + my $char_gap = '0' x 3; + my $word_gap = '0' x 7; + + # I generated this lookup table using a modified perl5 script + my %morse = ( + '1010111011101' => '!', '1011101110111011101' => "'", + '1011101011101' => '+', '1110111010101110111' => ',', + '111010101010111' => '-', '10111010111010111' => '.', + '1110101010111' => '/', '1110111011101110111' => '0', + '10111011101110111' => '1', '101011101110111' => '2', + '1010101110111' => '3', '10101010111' => '4', + '101010101' => '5', '11101010101' => '6', + '1110111010101' => '7', '111011101110101' => '8', + '11101110111011101' => '9', '11101110111010101' => ':', + '101011101110101' => '?', '101010111010111' => '@', + '10111' => 'A', '111010101' => 'B', + '11101011101' => 'C', '1110101' => 'D', + '1' => 'E', '101011101' => 'F', + '111011101' => 'G', '1010101' => 'H', + '101' => 'I', '1011101110111' => 'J', + '111010111' => 'K', '101110101' => 'L', + '1110111' => 'M', '11101' => 'N', + '11101110111' => 'O', '10111011101' => 'P', + '1110111010111' => 'Q', '1011101' => 'R', + '10101' => 'S', '111' => 'T', + '1010111' => 'U', '101010111' => 'V', + '101110111' => 'W', '11101010111' => 'X', + '1110101110111' => 'Y', '11101110101' => 'Z', + ); + + # Split words + my @words = split($word_gap, $encoded_string); + for my $i (0 .. $#words) { + + # Split characters + my @chars = split($char_gap, $words[$i]); + for my $j (0 .. $#chars) { + # Translate the character + $converted_string .= $morse{$chars[$j]} + if (defined($morse{$chars[$j]})); + } + + # Add the word gap + $converted_string .= ' ' + unless ($i == $#words); + } + + return $converted_string; +} + +sub simulate_error { + my $mutated_string = shift; + my $mutations = shift; + + for my $i (0..$mutations) { + my @zero_locations = (); + + # locate all the 0's + my @chars = split ('', $mutated_string); + for my $i (0 .. $#chars) { + push @zero_locations, $i + if ($chars[$i] == 0); + } + + # remove a random 0; + my $random_position = int(rand($#zero_locations)); + substr $mutated_string, + $zero_locations[$random_position], 1, ''; + } + + return $mutated_string; +} + + +sub error_correction { + my $corrected_string = shift; + + # missed char sep between two shorts + $corrected_string =~ s/^110/^1010/g; + + # missed char sep between two longs + $corrected_string =~ s/111111/1110111/g; + + # missed char sep between two shorts + $corrected_string =~ s/0110/01010/g; + + # missed word separator + $corrected_string =~ s/10{4,6}1/100000001/g; + + # missed char separator + $corrected_string =~ s/1001/10001/g; + + # missed on short and long + $corrected_string =~ s/1111/10111/g; + + return $corrected_string; +} diff --git a/challenge-035/javier-luque/perl6/ch-1.p6 b/challenge-035/javier-luque/perl6/ch-1.p6 new file mode 100644 index 0000000000..69bfd5e655 --- /dev/null +++ b/challenge-035/javier-luque/perl6/ch-1.p6 @@ -0,0 +1,63 @@ +# Test: perl6 ch2.p6 'string to test' +use v6.d; + +sub MAIN (Str $message) { + say encode($message); +} + +sub encode (Str $message) { + my $encoded_message = ''; + + # Some definitions + my $char_gap = '0' x 3; + my $word_gap = '0' x 7; + + # I generated this lookup table using a modified perl5 script + my %morse = ( + '!' => '1010111011101', "'" => '1011101110111011101', + '+' => '1011101011101', ',' => '1110111010101110111', + '-' => '111010101010111', '.' => '10111010111010111', + '/' => '1110101010111', '0' => '1110111011101110111', + '1' => '10111011101110111', '2' => '101011101110111', + '3' => '1010101110111', '4' => '10101010111', + '5' => '101010101', '6' => '11101010101', + '7' => '1110111010101', '8' => '111011101110101', + '9' => '11101110111011101', ':' => '11101110111010101', + '?' => '101011101110101', '@' => '101010111010111', + 'A' => '10111', 'B' => '111010101', + 'C' => '11101011101', 'D' => '1110101', + 'E' => '1', 'F' => '101011101', + 'G' => '111011101', 'H' => '1010101', + 'I' => '101', 'J' => '1011101110111', + 'K' => '111010111', 'L' => '101110101', + 'M' => '1110111', 'N' => '11101', + 'O' => '11101110111', 'P' => '10111011101', + 'Q' => '1110111010111', 'R' => '1011101', + 'S' => '10101', 'T' => '111', + 'U' => '1010111', 'V' => '101010111', + 'W' => '101110111', 'X' => '11101010111', + 'Y' => '1110101110111', 'Z' => '11101110101', + ); + + # Split words + my @words = $message.split(rx{\s+}); + for (0 .. @words.end) -> $i { + # Split characters + my @chars = @words[$i].comb; + + for (0 .. @chars.end) -> $j { + # Translate the character + $encoded_message ~= %morse{@chars[$j].uc}; + + # Add the character gap + $encoded_message ~= $char_gap + unless ($j == @chars.end); + } + + # Add the word gap + $encoded_message ~= $word_gap + unless ($i == (@words.end)); + } + + return $encoded_message; +} diff --git a/challenge-035/javier-luque/perl6/ch-2.p6 b/challenge-035/javier-luque/perl6/ch-2.p6 new file mode 100644 index 0000000000..b4b4e426a5 --- /dev/null +++ b/challenge-035/javier-luque/perl6/ch-2.p6 @@ -0,0 +1,61 @@ +# Test: perl6 ch1.p6 'string to test' | perl6 ch2.p6 +use v6.d; + +sub MAIN () { + for $*IN.lines() -> $line { + say decode($line); + } +} + +sub decode (Str $message) { + my $decoded_message = ''; + + # Some definitions + my $char_gap = '0' x 3; + my $word_gap = '0' x 7; + + # I generated this lookup table using a modified perl5 script + my %morse = ( + '1010111011101' => '!', '1011101110111011101' => "'", + '1011101011101' => '+', '1110111010101110111' => ',', + '111010101010111' => '-', '10111010111010111' => '.', + '1110101010111' => '/', '1110111011101110111' => '0', + '10111011101110111' => '1', '101011101110111' => '2', + '1010101110111' => '3', '10101010111' => '4', + '101010101' => '5', '11101010101' => '6', + '1110111010101' => '7', '111011101110101' => '8', + '11101110111011101' => '9', '11101110111010101' => ':', + '101011101110101' => '?', '101010111010111' => '@', + '10111' => 'A', '111010101' => 'B', + '11101011101' => 'C', '1110101' => 'D', + '1' => 'E', '101011101' => 'F', + '111011101' => 'G', '1010101' => 'H', + '101' => 'I', '1011101110111' => 'J', + '111010111' => 'K', '101110101' => 'L', + '1110111' => 'M', '11101' => 'N', + '11101110111' => 'O', '10111011101' => 'P', + '1110111010111' => 'Q', '1011101' => 'R', + '10101' => 'S', '111' => 'T', + '1010111' => 'U', '101010111' => 'V', + '101110111' => 'W', '11101010111' => 'X', + '1110101110111' => 'Y', '11101110101' => 'Z', + ); + + # Split words + my @words = $message.split($word_gap); + for (0 .. @words - 1) -> $i { + # Split characters + my @chars = @words[$i].split($char_gap); + + for (0 .. @chars - 1) -> $j { + # Translate the character + $decoded_message ~= %morse{@chars[$j]}; + } + + # Add the word gap + $decoded_message ~= ' ' + unless ($i == (@words - 1)); + } + + return $decoded_message; +} |
