diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-07-07 22:09:01 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-07-07 22:09:01 +0100 |
| commit | bdd0fbf369c2de48e2fbe49344b6c6583710274b (patch) | |
| tree | 5a9089d9ec39ba7d29721acdf98ea5b5a7416f6b | |
| parent | b88cffc02a93785c33d6690aff50f8e5b46ae239 (diff) | |
| parent | aa3a1b3669a0827909721b18e0c4cc1aa8f70a37 (diff) | |
| download | perlweeklychallenge-club-bdd0fbf369c2de48e2fbe49344b6c6583710274b.tar.gz perlweeklychallenge-club-bdd0fbf369c2de48e2fbe49344b6c6583710274b.tar.bz2 perlweeklychallenge-club-bdd0fbf369c2de48e2fbe49344b6c6583710274b.zip | |
Merge pull request #350 from adamcrussell/challenge-015
solutions for challenge 015
| -rw-r--r-- | challenge-015/adam-russell/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-015/adam-russell/perl5/ch-1.pl | 68 | ||||
| -rw-r--r-- | challenge-015/adam-russell/perl5/ch-2.pl | 38 |
3 files changed, 107 insertions, 0 deletions
diff --git a/challenge-015/adam-russell/blog.txt b/challenge-015/adam-russell/blog.txt new file mode 100644 index 0000000000..ba4dcd32ab --- /dev/null +++ b/challenge-015/adam-russell/blog.txt @@ -0,0 +1 @@ +https://adamcrussell.livejournal.com/5175.html diff --git a/challenge-015/adam-russell/perl5/ch-1.pl b/challenge-015/adam-russell/perl5/ch-1.pl new file mode 100644 index 0000000000..3fcfef5868 --- /dev/null +++ b/challenge-015/adam-russell/perl5/ch-1.pl @@ -0,0 +1,68 @@ +use strict; +use warnings; +## +# Write a script to generate first 10 strong and weak prime numbers. +## +use boolean; +use LWP::UserAgent; +use constant PRIME_URL => "http://primes.utm.edu/lists/small/100000.txt"; + +sub get_primes{ + my @primes; + my $ua = new LWP::UserAgent( + ssl_opts => {verify_hostname => 0} + ); + my $response = $ua->get(PRIME_URL); + my @lines = split(/\n/,$response->decoded_content); + foreach my $line (@lines){ + my @p = split(/\s+/, $line); + unless(@p < 10){ + push @primes, @p[1..(@p - 1)]; + } + } + return @primes; +} + +sub find_strong_prime{ + my($n_prev, $n, $n_next) = @_; + if($n > (($n_prev + $n_next) / 2)){ + return true; + } + return false; +} + +sub find_weak_prime{ + my($n_prev, $n, $n_next) = @_; + if($n < (($n_prev + $n_next) / 2)){ + return true; + } + return false; +} +## +# Main +## +my(@weak, @strong); +my @primes = get_primes(); +my @test; +push @test, shift @primes; +push @test, shift @primes; +while((@weak < 10 || @strong < 10) && @primes > 3){ + push @test, shift @primes; + if(find_strong_prime(@test) && @strong < 10){ + push @strong, $test[1]; + } + if(find_weak_prime(@test) && @weak < 10){ + push @weak, $test[1]; + } + shift @test; +} +if(@weak == 10 && @strong == 10){ + print "\tStrong\t\tWeak\n"; + print "\t======\t\t====\n"; + foreach my $i (0..9){ + print "$i:\t$strong[$i]\t\t$weak[$i]\n"; + } +} +else{ + print "Ten strong/weak primes were not found in the first 100,000 primes.\n"; +} diff --git a/challenge-015/adam-russell/perl5/ch-2.pl b/challenge-015/adam-russell/perl5/ch-2.pl new file mode 100644 index 0000000000..b359e0e628 --- /dev/null +++ b/challenge-015/adam-russell/perl5/ch-2.pl @@ -0,0 +1,38 @@ +use strict; +use warnings; +## +# Write a script to implement Vigenère cipher. +# The script should be able to encode and decode. +## +use constant KEYWORD => "perl"; + +sub encode{ + my($plaintext) = @_; + my $encrypted = ""; + my @characters = split(//, $plaintext); + my @key = split(//, KEYWORD x ((length($plaintext) / length(KEYWORD)) + 1)); + foreach my $c (@characters){ + my $e = ((ord(lc($c)) - ord("a")) + (ord(shift @key) - ord("a"))) % 26; + $encrypted .= chr($e + ord("a")); + } + return $encrypted; +} + +sub decode{ + my($encrypted) = @_; + my $plaintext = ""; + my @characters = split(//, $encrypted); + my @key = split(//, KEYWORD x ((length($encrypted) / length(KEYWORD)) + 1)); + foreach my $c (@characters){ + my $e = ((ord(lc($c)) - ord("a")) - (ord(shift @key) - ord("a"))) % 26; + $plaintext .= chr($e + ord("a")); + } + return $plaintext; + +} + +## +# Main +## +print "ATTACKATDAWN is encoded as " . encode("ATTACKATDAWN") . "\n"; +print "PXKLRORESENY is decoded as " . decode("PXKLRORESENY") . "\n"; |
