diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2019-07-04 18:53:31 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2019-07-04 18:53:31 +0100 |
| commit | 08c96517f775b02400fd05c11728902132546587 (patch) | |
| tree | ef192713a4c922b6ef50189240dd815f97c4e218 /challenge-015 | |
| parent | fd5fc8c34b095516d28f358313a1f6206ac7a92f (diff) | |
| download | perlweeklychallenge-club-08c96517f775b02400fd05c11728902132546587.tar.gz perlweeklychallenge-club-08c96517f775b02400fd05c11728902132546587.tar.bz2 perlweeklychallenge-club-08c96517f775b02400fd05c11728902132546587.zip | |
- Added solutions by Lubos Kolouch.
Diffstat (limited to 'challenge-015')
| -rw-r--r-- | challenge-015/lubos-kolouch/perl5/ch-1.pl | 91 | ||||
| -rw-r--r-- | challenge-015/lubos-kolouch/perl5/ch-2.pl | 142 |
2 files changed, 233 insertions, 0 deletions
diff --git a/challenge-015/lubos-kolouch/perl5/ch-1.pl b/challenge-015/lubos-kolouch/perl5/ch-1.pl new file mode 100644 index 0000000000..4dd16bcb1c --- /dev/null +++ b/challenge-015/lubos-kolouch/perl5/ch-1.pl @@ -0,0 +1,91 @@ +#!/usr/bin/perl +#=============================================================================== +# +# FILE: 2019_15_1.pl +# +# USAGE: ./2019_15_1.pl +# +# DESCRIPTION: Perl Weekly challenge w15 #1 +# Write a script to generate first 10 strong and weak prime numbers. +# +# OPTIONS: --- +# REQUIREMENTS: --- +# BUGS: --- +# NOTES: --- +# AUTHOR: Lubos Kolouch +# ORGANIZATION: +# VERSION: 1.0 +# CREATED: 07/02/2019 09:47:38 PM +# REVISION: --- +#=============================================================================== + +use warnings; +use strict; +use feature qw{ say }; + +my @strong_primes; +my @weak_primes; + +# let's remember the previous and the next primes +# it's not necessary to remember all of them + +my $last_prime =2; +my $current_prime; +my $next_prime; + +# simple prime check +sub is_prime { + my $candidate = shift; + + for my $n ( 2 .. int( sqrt($candidate) ) ) { + + # if can be divided, no luck... + return 0 if ( $candidate % $n == 0 ); + } + + #cool, a prime! + return 1; +} + +my $n = 2; +while (1) +{ + $n++; + + next unless is_prime($n); + + # first run is like: + # last - 2 current - ? next - ? + + unless ($current_prime) { + $current_prime = $n; + next; + } + + # next runs are like : + # last - 2 current - 3 next - ? + # we need last - 2 current - 3 next - 5 + # let's assign next prime and test the current one + $next_prime = $n; + + push @strong_primes, $current_prime if $current_prime > ($last_prime + $next_prime)/2 and scalar @strong_primes < 10; + push @weak_primes, $current_prime if $current_prime < ($last_prime + $next_prime)/2 and scalar @weak_primes < 10; + + # ok now let's shift to + # last - 3 current - 5 + $last_prime = $current_prime; + $current_prime = $n; + + last if (scalar @strong_primes > 9) and (scalar @weak_primes > 9); +} + +say 'Strong primes: '. join ',', @strong_primes; +say 'Weak primes: '. join ',', @weak_primes; + + +use Test::More; + +is( is_prime(5), 1, 'test if prime 1' ); +is( is_prime(4), 0, 'test if prime 4'); + +done_testing(); diff --git a/challenge-015/lubos-kolouch/perl5/ch-2.pl b/challenge-015/lubos-kolouch/perl5/ch-2.pl new file mode 100644 index 0000000000..8937618042 --- /dev/null +++ b/challenge-015/lubos-kolouch/perl5/ch-2.pl @@ -0,0 +1,142 @@ +#!/usr/bin/perl +#=============================================================================== +# +# FILE: 2019_15_2.pl +# +# USAGE: ./2019_15_2.pl +# +# DESCRIPTION: Perl Weekly challenge w15 #2 +# Write a script to implement Vigenère cipher. The script should be able +# decode and encode. Checkout wiki page for more information. +# +# OPTIONS: --- +# REQUIREMENTS: --- +# BUGS: --- +# NOTES: --- +# AUTHOR: Lubos Kolouch +# ORGANIZATION: +# VERSION: 1.0 +# CREATED: 07/02/2019 09:47:38 PM +# REVISION: --- +#=============================================================================== + +use warnings; +use strict; +use feature qw{ say }; + +sub encode_letter { + my ($plain, $key) = @_; + + # calculate the shift for plain text + my $shift_plain = ord($plain) - ord('A'); + + # calculate the shift for key + my $shift_key = ord($key) - ord('A'); + + # calculate the resulting ASCII value + my $result = ord('A') + ($shift_plain + $shift_key) % (ord('Z')-ord('A')+1); + + return chr($result); +} + +sub decode_letter { + my ($key, $cypher) = @_; + + # calculate the shift for key + my $shift_key = ord($key) - ord('A'); + + # calculate the shift for cypher text + my $shift_cypher = ord($cypher) - ord('A'); + + # calculate the resulting ASCII value + my $result = ord('A') + ($shift_cypher - $shift_key) % (ord('Z')-ord('A')+1); + + return chr($result); +} + + +sub encode_text { + my $text = shift; + my $key = shift; + + # convert the text to arrays for convenience, could be also done as string + # with length + + my @text_a = split //, $text; + my @key_a = split //, $key; + + my $result; + + for (0.. scalar @text_a -1) { + my $letter = $text_a[$_]; + + # encode each letter + $result .= encode_letter($letter, $key_a[$_ % scalar @key_a]); + } + + return $result; + +} + +sub decode_text { + my $text = shift; + my $key = shift; + + # convert the text to arrays for convenience, could be also done as string + # with length + + my @text_a = split //, $text; + my @key_a = split //, $key; + + my $result; + + for (0.. scalar @text_a -1) { + my $letter = $text_a[$_]; + + # decode each letter + $result .= decode_letter($key_a[$_ % scalar @key_a], $letter); + } + + return $result; + +} + +# ------ MAIN STARTS HERE -------- +my ($text, $key, $direction) = @ARGV; + +die 'No text passed. Usage: script TEXT KEY DIRECTION("d" or "e")' unless $text; +die 'No key passed. Usage: script TEXT KEY DIRECTION("d" or "e")' unless $key; +die 'No direction passed. Usage: script TEXT KEY DIRECTION("d" or "e")' unless $direction; + +$text = uc($text); +$key=uc($key); +$direction = uc($direction); + +die 'direction must be d or e' unless $direction eq 'D' or $direction eq 'E'; + +say encode_text($text, $key) if ($direction eq 'E'); +say decode_text($text, $key) if ($direction eq 'D'); + +# ----------- TESTS ---------- +use Test::More; + +is( encode_letter('A','A'),'A', 'test encode A-A' ); +is( encode_letter('A','S'),'S', 'test encode A-S' ); +is( encode_letter('C','C'),'E', 'test encode C-C' ); +is( encode_letter('F','M'),'R', 'test encode F-M' ); +is( encode_letter('O','Y'),'M', 'test encode O-Y' ); +is( encode_letter('Y','L'),'J', 'test encode Y-L' ); +is( encode_letter('Z','Z'),'Y', 'test encode Z-Z' ); + +is( decode_letter('A','A'),'A', 'test decode A-A' ); +is( decode_letter('A','S'),'S', 'test decode A-S' ); +is( decode_letter('C','C'),'A', 'test decode C-C' ); +is( decode_letter('F','M'),'H', 'test decode F-M' ); +is( decode_letter('O','Y'),'K', 'test decode O-Y' ); +is( decode_letter('Y','L'),'N', 'test decode Y-L' ); +is( decode_letter('Z','Z'),'A', 'test decode Z-Z' ); +is( encode_text('ATTACKATDAWN', 'LEMON'), 'LXFOPVEFRNHR', 'Test encode ATTACKATDAWN LEMON'); +is( decode_text('LXFOPVEFRNHR', 'LEMON'), 'ATTACKATDAWN', 'Test decode ATTACKATDAWN LEMON'); +isnt( decode_text('LXFOPVEFRNHR', 'LEMON'), 'DTTACKATDAWN', 'Test decode ATTACKATDAWN LEMON'); +isnt( encode_text('ATTACKATDAWN', 'LEMON'), 'OXFOPVEFRNHR', 'Test encode ATTACKATDAWN LEMON'); +done_testing(); |
