diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-07-05 11:18:41 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-07-05 11:18:41 +0100 |
| commit | 5b226dd7016faca47c13dc0c5cb699aaec10b3eb (patch) | |
| tree | ba811d8a0b34bd05624d4bd9f30f126746540614 | |
| parent | 08c96517f775b02400fd05c11728902132546587 (diff) | |
| parent | 018d443a5649c04bda7095e7512736110d71b10f (diff) | |
| download | perlweeklychallenge-club-5b226dd7016faca47c13dc0c5cb699aaec10b3eb.tar.gz perlweeklychallenge-club-5b226dd7016faca47c13dc0c5cb699aaec10b3eb.tar.bz2 perlweeklychallenge-club-5b226dd7016faca47c13dc0c5cb699aaec10b3eb.zip | |
Merge pull request #337 from noudald/noud-ch-015
Solutions for challenge 015 problem 1 and 2 by Noud
| -rw-r--r-- | challenge-015/noud/perl6/ch-1.p6 | 22 | ||||
| -rw-r--r-- | challenge-015/noud/perl6/ch-2.p6 | 42 |
2 files changed, 64 insertions, 0 deletions
diff --git a/challenge-015/noud/perl6/ch-1.p6 b/challenge-015/noud/perl6/ch-1.p6 new file mode 100644 index 0000000000..1b85ce7e56 --- /dev/null +++ b/challenge-015/noud/perl6/ch-1.p6 @@ -0,0 +1,22 @@ +# Write a script to generate first 10 strong and weak prime numbers. +# +# For example, the nth prime number is represented by p(n). +# +# p(1) = 2 +# p(2) = 3 +# p(3) = 5 +# p(4) = 7 +# p(5) = 11 +# +# Strong Prime number p(n) when p(n) > [ p(n-1) + p(n+1) ] / 2 +# Weak Prime number p(n) when p(n) < [ p(n-1) + p(n+1) ] / 2 + +my @p = (^Inf).grep: *.is-prime; +my @weak_prime_idx = (^Inf + 1).grep({ 2 * @p[$_] < @p[$_ - 1] + @p[$_ + 1] }); +my @strong_prime_idx = (^Inf + 1).grep({ 2 * @p[$_] > @p[$_ - 1] + @p[$_ + 1] }); + +say "First 10 weak primes:"; +@p[@weak_prime_idx[^10]].say; + +say "First 10 strong primes:"; +@p[@strong_prime_idx[^10]].say; diff --git a/challenge-015/noud/perl6/ch-2.p6 b/challenge-015/noud/perl6/ch-2.p6 new file mode 100644 index 0000000000..6b60e8f10d --- /dev/null +++ b/challenge-015/noud/perl6/ch-2.p6 @@ -0,0 +1,42 @@ +# Write a script to implement Vigenère cipher. The script should be able encode +# and decode. Checkout wiki page for more information. + +# I restrict the Vigenere cipher only to lower case characters a, b, ..., z. +# Extending the cipher to upper case characters is possible, but makes this +# solution more tedious, because you need to check for extra cases and you +# have to define how to apply upper case keys to lower case characters and +# visa versa. For example what is 'a' + 'Z'? + +sub cipher_apply(@inp_ords, @key_ords) { + return (@inp_ords Z ({ @key_ords[$++ % *] } ... Inf)).map({ + if 'a'.ord <= $_[0] and $_[0] <= 'z'.ord { + (($_[0] + $_[1] - 2 * 'a'.ord) % 26) + 'a'.ord + } else { + $_[0] + } + }); +} + +sub cipher_encrypt(Str $inp, Str $key) { + return cipher_apply($inp.ords, $key.ords).chrs; +}; + +sub cipher_decrypt(Str $inp, Str $key) { + return cipher_apply($inp.ords, $key.ords.map({ 2 * 'a'.ord - $_ })).chrs +}; + + +multi MAIN('unit-test') { + use Test; + + my $input = 'perl weekly challenge'; + my $key = 'fifteen'; + + my $encrypted = cipher_encrypt($input, $key); + my $decrypted = cipher_decrypt($encrypted, $key); + + $encrypted.say; + $decrypted.say; + + is $input, $decrypted; +} |
