diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-07-07 19:18:55 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-07-07 19:18:55 +0100 |
| commit | 593eda7d23912c8ed89d601d1c33e09236b47360 (patch) | |
| tree | b7ffa7e4dc35be0839af195700cc9b7303e58cba | |
| parent | c3209c4b1f08f92df8607fd54c74dd1867cf25c2 (diff) | |
| parent | 7024b1d9881106fe239cd2948e176b9f8eed2b53 (diff) | |
| download | perlweeklychallenge-club-593eda7d23912c8ed89d601d1c33e09236b47360.tar.gz perlweeklychallenge-club-593eda7d23912c8ed89d601d1c33e09236b47360.tar.bz2 perlweeklychallenge-club-593eda7d23912c8ed89d601d1c33e09236b47360.zip | |
Merge pull request #344 from fjwhittle/master
fjwhittle challenge 015 solutions
| -rw-r--r-- | challenge-015/fjwhittle/perl6/ch-1.p6 | 10 | ||||
| -rw-r--r-- | challenge-015/fjwhittle/perl6/ch-2.p6 | 19 | ||||
| -rw-r--r-- | challenge-015/fjwhittle/perl6/ch-3.p6 | 35 |
3 files changed, 64 insertions, 0 deletions
diff --git a/challenge-015/fjwhittle/perl6/ch-1.p6 b/challenge-015/fjwhittle/perl6/ch-1.p6 new file mode 100644 index 0000000000..413abfa101 --- /dev/null +++ b/challenge-015/fjwhittle/perl6/ch-1.p6 @@ -0,0 +1,10 @@ +#!/usr/bin/env perl6 + +my $P := (^Inf).grep(*.is-prime); + +my $P-strong := (1..Inf).grep({ $P[$^n] > ([+] $P[$^n X+ -1,1]) / 2 }).map: { $P[$^n] }; +my $P-weak := (1..Inf).grep({ $P[$^n] < ([+] $P[$^n X+ -1,1]) / 2 }).map: { $P[$^n] }; + +put 'First 10 strong primes: ' ~ $P-strong[^10].».fmt('%2d').join(', '); + +put ' First 10 weak primes: ' ~ $P-weak[^10].».fmt('%2d').join(', '); diff --git a/challenge-015/fjwhittle/perl6/ch-2.p6 b/challenge-015/fjwhittle/perl6/ch-2.p6 new file mode 100644 index 0000000000..481e0622f5 --- /dev/null +++ b/challenge-015/fjwhittle/perl6/ch-2.p6 @@ -0,0 +1,19 @@ +#!/usr/bin/env perl6 + +my sub ordinate (Str $_) { + .uc.comb(/<[A..Z]>/)».ord X- 'A'.ord +} + +#| Encode <message> using Vigenère cipher with <key> +multi MAIN (Str $message, Str $key, Bool :$encode) { + my @key = ordinate $key; + + put ordinate($message).rotor(@key.elems, :partial).map({ (($_ Z+ @key) X% 26) X+ 'A'.ord})».Slip».chr.join +} + +#| Decode <message> using Vigenère cipher with <key> +multi MAIN (Str $message, Str $key, Bool :$decode!) { + my @key = ordinate $key; + + put ordinate($message).rotor(@key.elems, :partial).map({ (($_ Z- @key) X% 26) X+ 'A'.ord})».Slip».chr.join +} diff --git a/challenge-015/fjwhittle/perl6/ch-3.p6 b/challenge-015/fjwhittle/perl6/ch-3.p6 new file mode 100644 index 0000000000..1404036c17 --- /dev/null +++ b/challenge-015/fjwhittle/perl6/ch-3.p6 @@ -0,0 +1,35 @@ +#!/usr/bin/env perl6 + +use Cro::HTTP::Client; + +sub MAIN ( + Str $phrase, + Str :$api-key where *.chars = %*ENV<LANGDETECT_APIKEY> +) { + + my $http-client = Cro::HTTP::Client.new: + base-uri => 'https://ws.detectlanguage.com', + auth => { bearer => $api-key }; + + my %lang = $http-client.get('/0.2/languages').result.body.result.map: + { .<code> => (S:g/'_' +/ / given .<name>).wordcase }; + + my %detect = $http-client.post( + '/0.2/detect', + content-type => 'application/json', + body => {'q' => $phrase} + ).result.body.result; + + my @language = %lang{%detect<data><detections>».<language>}; + + say '‘%s’ is in %s'.sprintf: + $phrase, + join(' and ', @language.elems > 1 ?? @language[^*.elems].join(', ') !! (), @language.tail); + + CATCH { + when X::Cro::HTTP::Error { + $*ERR.say: .message; + $*ERR.say: 'During %s to ‘%s’'.sprintf: .method, .target given .request; + } + } +} |
