diff options
| author | Doomtrain14 <yet.ebreo@gmail.com> | 2019-08-22 21:11:51 +0800 |
|---|---|---|
| committer | Doomtrain14 <yet.ebreo@gmail.com> | 2019-08-22 21:11:51 +0800 |
| commit | 5a8c3e8fa4cb60c54c118c4278970628c3fd8ba2 (patch) | |
| tree | 8174eb6741dfb0c46c4f37e5e82308a163dd8df8 | |
| parent | 827e7bd922a9817cd31eb32d4af80cb467293fa2 (diff) | |
| parent | 5fc80ca35a61b7e84529f422ba4101ed3aae9e70 (diff) | |
| download | perlweeklychallenge-club-5a8c3e8fa4cb60c54c118c4278970628c3fd8ba2.tar.gz perlweeklychallenge-club-5a8c3e8fa4cb60c54c118c4278970628c3fd8ba2.tar.bz2 perlweeklychallenge-club-5a8c3e8fa4cb60c54c118c4278970628c3fd8ba2.zip | |
Merge remote-tracking branch 'upstream/master'
41 files changed, 5165 insertions, 4716 deletions
diff --git a/challenge-021/jaldhar-h-vyas/blog.txt b/challenge-021/jaldhar-h-vyas/blog.txt new file mode 100644 index 0000000000..1d51db8b73 --- /dev/null +++ b/challenge-021/jaldhar-h-vyas/blog.txt @@ -0,0 +1 @@ +https://www.braincells.com/perl/2019/08/perl_weekly_challenge_week_21.html diff --git a/challenge-021/jaldhar-h-vyas/perl6/ch-2.p6 b/challenge-021/jaldhar-h-vyas/perl6/ch-2.p6 new file mode 100755 index 0000000000..92b661eb44 --- /dev/null +++ b/challenge-021/jaldhar-h-vyas/perl6/ch-2.p6 @@ -0,0 +1,121 @@ +#!/usr/bin/perl6 + +grammar URL { + rule TOP { + <Scheme> + ':' + [ + <Slashes>? + [ <Userinfo> '@' ]? + <Host> + [ ':' <Port> ]? + ]? + <Path>? + [ '?' <Query> ]? + [ '#' <Fragment> ]? + } + + token Scheme { <SchemeChar>+ } + token Slashes { \/\/ } + token Userinfo { <UserinfoChar>+ } + token Host { <IPv4> | <Unreserved>+ } + token Port { \d+ } + token Path { <PathChar>+ } + token Query { <QueryOrFragment>+ } + token Fragment { <QueryOrFragment>+ } + + token SchemeChar { <[ A..Z a..z 0..9 + \- . ]> } + token DecOctet { \d | <[1..9]>\d | 1\d\d | 2<[0..4]>\d | 25<[0..5]> } + token HexOctet { <[ 0..9 A..F a..f ]> ** 2 } + token IPv4 { <DecOctet> \. <DecOctet> \. <DecOctet> \. <DecOctet> } + token PChar { <UserinfoChar> | '@' } + token PathChar { <PChar> | '/' } + token PctEncoded { \% <HexOctet> } + token QueryOrFragment { <PChar> | \/ | \? } + token SubDelim { <[ \! $ & \' \( \) * \+ \, \; \= ]> } + token Unreserved { <[ A..Z a..z 0..9 \- . _ \~ ]> } + token UserinfoChar { <Unreserved> | <PctEncoded> | <SubDelim> | ':' } +} + +class URLAction { + method TOP($/) { + my $result = $<Scheme>.made ~ ':'; + if $/<Slashes> { + $result ~= $<Slashes>; + } + + if $/<Userinfo> { + $result ~= $<Userinfo>.made ~ '@'; + } + + if $/<Host> { + $result ~= $<Host>.made; + } + + if $/<Port.made> { + $result ~= ':' ~ $<Port>.made; + } + + $result ~= $<Path>.made; + + if $/<Query> { + $result ~= '?' ~ $<Query>.made; + } + + if $<Fragment> { + $result ~= '#' ~ $<Fragment>.made; + } + + make($result); + + } + + method Scheme($/) { + make($/.Str.lc); + } + + method Host($/) { + make($/.Str.lc); + } + + method Userinfo($/) { + make(self.processPctEncoded($/.Str)); + } + + method Port($/) { + if ($/.Str ~~ '80') { + make(Nil); + } + } + + method Path($/) { + make(self.processPctEncoded($/.Str)); + } + + method Query($/) { + make(self.processPctEncoded($/.Str)); + } + + method Fragment($/) { + make(self.processPctEncoded($/.Str)); + } + + method processPctEncoded($part) { + my $processed = $part; + for $part ~~ m:g/ \% $<HexOctet> = ( <[ 0..9 A..F a..f ]> ** 2 ) / + -> $pct { + my $hex = $pct<HexOctet>.Str.uc; + if :16($hex) ~~ / <[ A..Z a..z 0..9 \- . _ \~ ]> / { # Unreserved + $processed = $processed.subst($pct.Str, :16($hex).chr); + } else { + $processed = $processed.subst($pct.Str, "%$hex"); + } + } + return $processed; + } +} + + +multi sub MAIN($url) { + URL.parse($url, actions => URLAction.new).made.say; +}
\ No newline at end of file diff --git a/challenge-022/laurent-rosenfeld/blog.txt b/challenge-022/laurent-rosenfeld/blog.txt new file mode 100644 index 0000000000..b3e03920de --- /dev/null +++ b/challenge-022/laurent-rosenfeld/blog.txt @@ -0,0 +1 @@ +http://blogs.perl.org/users/laurent_r/2019/08/perl-weekly-challenge-22-sexy-prime-pairs-and-compression-algorithm.html diff --git a/challenge-022/laurent-rosenfeld/perl5/ch-1.pl b/challenge-022/laurent-rosenfeld/perl5/ch-1.pl new file mode 100644 index 0000000000..0a9ff01b94 --- /dev/null +++ b/challenge-022/laurent-rosenfeld/perl5/ch-1.pl @@ -0,0 +1,22 @@ +#!/usr/bin/perl +use strict; +use warnings; +use feature qw/say/; + +sub is_prime{ + my $num = shift; + for my $i (2 .. $num ** .5) { + return 0 if $num % $i == 0; + } + return 1; +} + +my ($candidate, $count) = (2, 0); +while (1) { + if (is_prime $candidate and is_prime $candidate + 6) { + say "$candidate ", $candidate + 6; + $count ++ + } + last if $count >= 10; + $candidate ++; +} diff --git a/challenge-022/laurent-rosenfeld/perl5/ch-2.pl b/challenge-022/laurent-rosenfeld/perl5/ch-2.pl new file mode 100644 index 0000000000..e1a44dc7e6 --- /dev/null +++ b/challenge-022/laurent-rosenfeld/perl5/ch-2.pl @@ -0,0 +1,50 @@ +#!/usr/bin/perl +use strict; +use warnings; +use feature qw /say/; +use constant start_dict_size => 256; +use utf8; + +sub encode { + my $in = shift; + my %dict = map { chr $_ => $_ } 0 .. start_dict_size - 1; + my $ω = ""; + my @result; + + for my $c (split //, $in) { + my $ωc = $ω . $c; + if (exists $dict{$ωc}) { + $ω = $ωc; + } else { + push @result, $dict{$ω}; + $dict{$ωc} = scalar keys %dict; + $ω = $c; + } + } + push @result, $dict{$ω} if length $ω; + return @result; +} +sub decode { + my @encoded = @_; + my $dict_size = start_dict_size; + my %dict = map { $_ => chr } 0 .. start_dict_size - 1;; + my $ω = $dict{shift @encoded}; + my @result = ($ω); + for my $i (@encoded) { + my $str; + if (exists $dict{$i}) { + $str = $dict{$i}; + } elsif ($i == $dict_size) { + $str = $ω . substr $ω, 0, 1; + } else { die "Error on $i" } + push @result, $str; + $dict{$dict_size++} = $ω . substr $str, 0, 1; + $ω = $str; + } + return join "", @result; +} + +my $input_str = 'TOBEORNOTTOBETOBEORNOTTOBETOBEORNOTTOBE'; +my @encoded = encode $input_str; +say "@encoded"; +say decode(@encoded); diff --git a/challenge-022/laurent-rosenfeld/perl6/ch-1.p6 b/challenge-022/laurent-rosenfeld/perl6/ch-1.p6 new file mode 100644 index 0000000000..a5271748af --- /dev/null +++ b/challenge-022/laurent-rosenfeld/perl6/ch-1.p6 @@ -0,0 +1,4 @@ +use v6; + +my @sexy-primes = grep { .is-prime and ($_ + 6).is-prime}, (2, 3, *+2 ... Inf); +say "@sexy-primes[$_] ", @sexy-primes[$_] + 6 for ^10; diff --git a/challenge-022/laurent-rosenfeld/perl6/ch-2.p6 b/challenge-022/laurent-rosenfeld/perl6/ch-2.p6 new file mode 100644 index 0000000000..32529819d7 --- /dev/null +++ b/challenge-022/laurent-rosenfeld/perl6/ch-2.p6 @@ -0,0 +1,50 @@ +use v6; + +constant $start-dict-size = 26; + +sub encode (Str $in) { + my %dict = map { $_[0] => $_[1] }, + ( ('A'..'Z') Z (^$start-dict-size) ); + my $ω = ""; + my @result = gather { + for $in.comb -> $c { + my $ωc = $ω ~ $c; + if %dict{$ωc}:exists { + $ω = $ωc; + } else { + take %dict{$ω}; + %dict{$ωc} = +%dict; + $ω = $c; + } + } + take %dict{$ω} if $ω.chars; + } + # say %dict; + return @result; +} +sub decode (@encoded) { + my $dict-size = $start-dict-size; + my %dict = map { $_[1] => $_[0] }, + ( ('A'..'Z') Z (^$start-dict-size) ); + my $ω = %dict{shift @encoded}; + my @result = gather { + take $ω; + for @encoded -> $i { + my $str; + if %dict{$i}:exists { + $str = %dict{$i}; + } elsif $i == $dict-size { + $str = $ω ~ $ω.substr(0,1) + } + take $str; + %dict{$dict-size++} = $ω ~ $str.substr(0,1); + $ω = $str; + } + } + return join "", @result; +} + +my $input_str = 'TOBEORNOTTOBETOBEORNOTTOBETOBEORNOTTOBE'; +my @encoded = encode $input_str; +say @encoded; +say decode @encoded; diff --git a/challenge-022/simon-proctor/perl6/ch-2.p6 b/challenge-022/simon-proctor/perl6/ch-2.p6 new file mode 100644 index 0000000000..e0e2ecd0ff --- /dev/null +++ b/challenge-022/simon-proctor/perl6/ch-2.p6 @@ -0,0 +1,116 @@ +#!/usr/bin/env perl6 + +use v6; + +=begin pod + +Encodes and Decodes using the LZW algorithm. + +Encoded data is expected as a series of space seperated numbers because I don't want to mess about bit twiddling. + +=end pod + +sub USAGE() { + say "Encodes and Decodes using the LZW algorithm.\n\nEncoded data is expected as a series of space seperated numbers because I don't want to mess about bit twiddling.\n$*USAGE"; +} + +subset FilePath of Str where *.IO.f; +subset ValidToEncode of Str where m/^<[a..z A..Z 0..9 \ _ \n \. , ]>* \n?$/; +subset ValidToDecode of Str where m/^ [\d+]+ % " " $/; + +multi sub MAIN( Bool :h(:$help) where ?* ) { USAGE() } + +#| Read from STDIN and encode the result +multi sub MAIN( 'encode' ) { + say encode-data( $*IN.slurp.chomp ); +} + +#| Read the text from the given file and output the encoded result +multi sub MAIN( 'encode', FilePath $path ) { + say encode-data( $path.IO.slurp.chomp ); +} + +#| Encode the given string +multi sub MAIN( 'encode', Str $data ) { + say encode-data( $data ); +} + +#| Read from STDIN and decode the result +multi sub MAIN( 'decode' ) { + say decode-data( $*IN.slurp.chomp ); +} + +#| Read the text from the given file and output the decoded result +multi sub MAIN( 'decode', FilePath $path ) { + say decode-data( $path.IO.slurp.chomp ); +} + +#| Decode the given string +multi sub MAIN( 'decode', Str $data ) { + say decode-data( $data ); +} + +#| Decode a list of ints +multi sub MAIN( 'decode', *@data where { @data.all ~~ Int } ) { + say decode-data( @data.join(" ") ); +} + + + +multi sub encode-data( ValidToEncode $str ) { + my @working = $str.comb; + my %dict = (available-characters() Z=> (0,1,2...*)).hash; + my $output = []; + + LOOP: while @working.elems > 0 { + # Find the maximum key length + my $max = %dict.keys.map( *.codes ).sort( { $^b <=> $^a } ).first(); + $max = @working.elems if $max > @working.elems; + + for (1..$max).reverse -> $length { + my $substr = @working[0..^$length].join(""); + if ( %dict{$substr}:exists ) { + $output.push( %dict{$substr} ); + if ( @working.elems > $length ) { + my $next = @working[0..$length].join(""); + %dict{$next} = %dict.values.max+1; + } + @working.splice(0,$length); + next LOOP; + } + } + } + + $output.join(" "); +} + +multi sub encode-data( Str $ ) { + die "Data to encode can include only a-z, A-Z, 0-9, ' ', ',', '.', and '_' newlines"; +} + +multi sub decode-data( ValidToDecode $str ) { + my @input = $str.split(" "); + my %dict = ((0,1,2...*) Z=> available-characters()).hash; + my $next-key = %dict.keys.elems; + my @output = []; + my $last; + + for @input -> $key { + @output.push( %dict{$key} ); + with $last { + %dict{$next-key} = $last ~ %dict{$key}; + $next-key++; + } + $last = %dict{$key} + } + + @output.join(""); +} + +multi sub decode-data( Str $ ) { + die "Data to decode should be a space seperated string of Ints"; +} + +sub available-characters() { + ( |("a".."z"), |("A".."Z"), " ", "_", ",", ".", "\n", |("0".."9") ); +} diff --git a/challenge-022/steven-wilson/perl5/ch-1.pl b/challenge-022/steven-wilson/perl5/ch-1.pl new file mode 100644 index 0000000000..44478e6705 --- /dev/null +++ b/challenge-022/steven-wilson/perl5/ch-1.pl @@ -0,0 +1,35 @@ +#!/usr/bin/env perl +# Author: Steven Wilson +# Date: 2019-08-21 +# Week: 022 +# +# Task #1 +# Write a script to print first 10 Sexy Prime Pairs. Sexy primes are +# prime numbers that differ from each other by 6. For example, the +# numbers 5 and 11 are both sexy primes, because 11 - 5 = 6. The term +# “sexy prime” is a pun stemming from the Latin word for six: sex. For +# more information, please checkout wiki page. +# https://en.wikipedia.org/wiki/Sexy_prime + +use strict; +use warnings; +use feature qw/ say /; + +use Math::Primality qw/ next_prime /; + +my @primes; +my @sexy_prime_pairs; +my $next_prime = 2; + +while ( scalar @sexy_prime_pairs < 10 ) { + push @primes, $next_prime; + if ( grep { $_ == ( $next_prime - 6 ) } @primes ) { + push @sexy_prime_pairs, [ ( $next_prime - 6 ), $next_prime ]; + } + $next_prime = next_prime($next_prime); +} + +say "First 10 Sexy Prime Pairs are:"; +for (@sexy_prime_pairs) { + say @{$_}[0], " ", @{$_}[1]; +} diff --git a/script/refresh-stats.sh b/script/refresh-stats.sh index dab53b6244..6720c15f42 100644 --- a/script/refresh-stats.sh +++ b/script/refresh-stats.sh @@ -39,6 +39,8 @@ mv pwc-current.json stats/pwc-challenge-019.json fetch-pwc-stats --members members.json --guests guests.json --source challenge-020 --current mv pwc-current.json stats/pwc-challenge-020.json fetch-pwc-stats --members members.json --guests guests.json --source challenge-021 --current +mv pwc-current.json stats/pwc-challenge-021.json +fetch-pwc-stats --members members.json --guests guests.json --source challenge-022 --current mv pwc-current.json stats/ fetch-pwc-stats --members members.json --guests guests.json --source challenge-001 --summary @@ -61,6 +63,7 @@ fetch-pwc-stats --members members.json --guests guests.json --source challenge-0 fetch-pwc-stats --members members.json --guests guests.json --source challenge-018 --master pwc-summary.json --update fetch-pwc-stats --members members.json --guests guests.json --source challenge-019 --master pwc-summary.json --update fetch-pwc-stats --members members.json --guests guests.json --source challenge-020 --master pwc-summary.json --update +fetch-pwc-stats --members members.json --guests guests.json --source challenge-021 --master pwc-summary.json --update mv pwc-summary.json stats/pwc-master-stats.json echo Now fetch current stats diff --git a/stats/pwc-challenge-001.json b/stats/pwc-challenge-001.json index ec7df47c3b..aad1f3f904 100644 --- a/stats/pwc-challenge-001.json +++ b/stats/pwc-challenge-001.json @@ -2,313 +2,10 @@ "xAxis" : { "type" : "category" }, - "tooltip" : { - "headerFormat" : "<span style='font-size:11px'>{series.name}</span><br/>", - "pointFormat" : "<span style='color:{point.color}'>{point.name}</span>: <b>{point.y:f}</b><br/>", - "followPointer" : 1 - }, - "legend" : { - "enabled" : 0 - }, - "series" : [ - { - "data" : [ - { - "drilldown" : "Adam Russell", - "y" : 3, - "name" : "Adam Russell" - }, - { - "drilldown" : "Ailbhe Tweedie", - "y" : 1, - "name" : "Ailbhe Tweedie" - }, - { - "name" : "Alex Daniel", - "y" : 2, - "drilldown" : "Alex Daniel" - }, - { - "drilldown" : "Andrezgz", - "name" : "Andrezgz", - "y" : 2 - }, - { - "drilldown" : "Antonio Gamiz", - "y" : 2, - "name" : "Antonio Gamiz" - }, - { - "y" : 2, - "name" : "Arpad Toth", - "drilldown" : "Arpad Toth" - }, - { - "name" : "Athanasius", - "y" : 2, - "drilldown" : "Athanasius" - }, - { - "y" : 2, - "name" : "Bob Kleemann", - "drilldown" : "Bob Kleemann" - }, - { - "drilldown" : "Daniel Mantovani", - "name" : "Daniel Mantovani", - "y" : 1 - }, - { - "name" : "Dave Cross", - "y" : 3, - "drilldown" : "Dave Cross" - }, - { - "y" : 2, - "name" : "Dave Jacoby", - "drilldown" : "Dave Jacoby" - }, - { - "name" : "David Kayal", - "y" : 2, - "drilldown" : "David Kayal" - }, - { - "y" : 2, - "name" : "Doug Schrag", - "drilldown" : "Doug Schrag" - }, - { - "y" : 4, - "name" : "Dr James A. Smith", - "drilldown" : "Dr James A. Smith" - }, - { - "name" : "Duncan C. White", - "y" : 2, - "drilldown" : "Duncan C. White" - }, - { - "drilldown" : "Eddy HS", - "y" : 2, - "name" : "Eddy HS" - }, - { - "drilldown" : "Finley", - "y" : 2, - "name" : "Finley" - }, - { - "y" : 1, - "name" : "Fred Zinn", - "drilldown" : "Fred Zinn" - }, - { - "name" : "Freddie B", - "y" : 2, - "drilldown" : "Freddie B" - }, - { - "name" : "Gustavo Chaves", - "y" : 1, - "drilldown" : "Gustavo Chaves" - }, - { - "y" : 2, - "name" : "JJ Merelo", - "drilldown" : "JJ Merelo" - }, - { - "y" : 4, - "name" : "Jaldhar H. Vyas", - "drilldown" : "Jaldhar H. Vyas" - }, - { - "drilldown" : "Jeff", - "y" : 2, - "name" : "Jeff" - }, - { - "drilldown" : "Jeremy Carman", - "name" : "Jeremy Carman", - "y" : 2 - }, - { - "y" : 1, - "name" : "Jim Bacon", - "drilldown" : "Jim Bacon" - }, - { - "name" : "Jo Christian Oterhals", - "y" : 5, - "drilldown" : "Jo Christian Oterhals" - }, - { - "drilldown" : "Joelle Maslak", - "y" : 4, - "name" : "Joelle Maslak" - }, - { - "y" : 1, - "name" : "John Barrett", - "drilldown" : "John Barrett" - }, - { - "name" : "Juan Caballero", - "y" : 2, - "drilldown" : "Juan Caballero" - }, - { - "y" : 2, - "name" : "Khalid", - "drilldown" : "Khalid" - }, - { - "name" : "Kian-Meng Ang", - "y" : 3, - "drilldown" : "Kian-Meng Ang" - }, - { - "drilldown" : "Kivanc Yazan", - "name" : "Kivanc Yazan", - "y" : 2 - }, - { - "drilldown" : "Lars Balker", - "name" : "Lars Balker", - "y" : 4 - }, - { - "drilldown" : "Laurent Rosenfeld", - "name" : "Laurent Rosenfeld", - "y" : 4 - }, - { - "drilldown" : "Mark Senn", - "y" : 2, - "name" : "Mark Senn" - }, - { - "drilldown" : "Martin Mugeni", - "y" : 2, - "name" : "Martin Mugeni" - }, - { - "drilldown" : "Neil Bowers", - "y" : 1, - "name" : "Neil Bowers" - }, - { - "name" : "Nick Logan", - "y" : 4, - "drilldown" : "Nick Logan" - }, - { - "drilldown" : "Oleksii Tsvietnov", - "y" : 2, - "name" : "Oleksii Tsvietnov" - }, - { - "drilldown" : "Ozzy", - "name" : "Ozzy", - "y" : 2 - }, - { - "drilldown" : "Pavel Jurca", - "name" : "Pavel Jurca", - "y" : 2 - }, - { - "y" : 2, - "name" : "Pete Houston", - "drilldown" : "Pete Houston" - }, - { - "y" : 3, - "name" : "Philippe Bruhat", - "drilldown" : "Philippe Bruhat" - }, - { - "name" : "Prajith P", - "y" : 1, - "drilldown" : "Prajith P" - }, - { - "name" : "Sean Meininger", - "y" : 2, - "drilldown" : "Sean Meininger" - }, - { - "drilldown" : "Simon Proctor", - "y" : 6, - "name" : "Simon Proctor" - }, - { - "drilldown" : "Simon Reinhardt", - "y" : 2, - "name" : "Simon Reinhardt" - }, - { - "drilldown" : "Steve Rogerson", - "y" : 4, - "name" : "Steve Rogerson" - }, - { - "drilldown" : "Steven Wilson", - "name" : "Steven Wilson", - "y" : 2 - }, - { - "drilldown" : "Tiago Stock", - "y" : 1, - "name" : "Tiago Stock" - }, - { - "drilldown" : "Tore Andersson", - "y" : 2, - "name" : "Tore Andersson" - }, - { - "name" : "Veesh Goldman", - "y" : 1, - "drilldown" : "Veesh Goldman" - }, - { - "drilldown" : "William Gilmore", |
