diff options
| author | Roger Bell_West <roger@firedrake.org> | 2024-08-04 09:20:41 +0100 |
|---|---|---|
| committer | Roger Bell_West <roger@firedrake.org> | 2024-08-04 09:20:41 +0100 |
| commit | 22d323bb2fa927cce8d9e2d81bebc7e3b18d7a8b (patch) | |
| tree | 597d944adc1fcff029cfb7b24fc1d3779fbe68ff | |
| parent | bd9c64bbea0318b67e02d309e65708c214a6ff15 (diff) | |
| parent | 47a6a89dbac521d91d7e8742347faefaa74cca67 (diff) | |
| download | perlweeklychallenge-club-22d323bb2fa927cce8d9e2d81bebc7e3b18d7a8b.tar.gz perlweeklychallenge-club-22d323bb2fa927cce8d9e2d81bebc7e3b18d7a8b.tar.bz2 perlweeklychallenge-club-22d323bb2fa927cce8d9e2d81bebc7e3b18d7a8b.zip | |
Merge remote-tracking branch 'upstream/master'
85 files changed, 4285 insertions, 2440 deletions
diff --git a/challenge-277/ryan-thompson/README.md b/challenge-277/ryan-thompson/README.md index 588de6d8b1..537f4295ce 100644 --- a/challenge-277/ryan-thompson/README.md +++ b/challenge-277/ryan-thompson/README.md @@ -1,17 +1,15 @@ # Ryan Thompson -## Week 276 Solutions +## Week 277 Solutions -### Task 1 › Complete Day +### Task 1 › Count Common * [Perl](perl/ch-1.pl) - * [Python](python/ch-1.py) -### Task 2 › Maximum Frequency +### Task 2 › Strong Pair * [Perl](perl/ch-2.pl) - * [Python](python/ch-2.py) ## Blog - * [Maximum Frequency and now my Day is Complete](https://ry.ca/2024/07/pwc-276-complete-day-and-maximum-frequency/) + * [We make a Strong Pair](https://ry.ca/2024/07/pwc-277-strong-pair-counting-common/) diff --git a/challenge-277/ryan-thompson/blog.txt b/challenge-277/ryan-thompson/blog.txt new file mode 100644 index 0000000000..01ded8abc3 --- /dev/null +++ b/challenge-277/ryan-thompson/blog.txt @@ -0,0 +1 @@ +https://ry.ca/2024/07/pwc-277-strong-pair-counting-common/ diff --git a/challenge-277/ryan-thompson/perl/ch-1.pl b/challenge-277/ryan-thompson/perl/ch-1.pl new file mode 100644 index 0000000000..9446abbad0 --- /dev/null +++ b/challenge-277/ryan-thompson/perl/ch-1.pl @@ -0,0 +1,35 @@ +#!/usr/bin/env perl +# +# ch-1.pl - Count Common +# +# Count of words that appears in both arrays exactly once +# My version supports an arbitrary number of arrays, instead of just two. +# Modifying or wrapping it to limit it to two would be trivial. +# Does NOT sort results by default. This is a minor optimization. +# Sort if you need stable results. +# +# See blog post for more information: +# https://ry.ca/2024/07/pwc-277-strong-pair-counting-common/ +# +# 2024 Ryan Thompson <rjt@cpan.org> + +use 5.010; +use warnings; +use strict; +use Carp; +use List::Util qw< all >; +no warnings 'uninitialized'; + +sub count_common { + 'ARRAY' ne ref and croak 'Arguments must be ARRAY refs' for @_; # VAL + my @once; # $once[$idx]{word} = # True if 'word' appears once in $_[$idx] + + for my $i (keys @_) { + my %freq; $freq{$_}++ for @{ $_[$i] }; + $once[$i]{$_} = 1 for grep { $freq{$_} == 1 } keys %freq; + } + + grep { my $w = $_; all { $_->{$w} } @once } keys %{$once[0]} +} + +1; diff --git a/challenge-277/ryan-thompson/perl/ch-2.pl b/challenge-277/ryan-thompson/perl/ch-2.pl new file mode 100644 index 0000000000..1e7daecc1f --- /dev/null +++ b/challenge-277/ryan-thompson/perl/ch-2.pl @@ -0,0 +1,28 @@ +#!/usr/bin/env perl +# +# ch-2.pl - Strong Pair +# +# Return the count of all strong pairs in the given array +# A pair of integers is strong if 0 < | x - y | < min(x,y) +# +# See blog post for more information: +# https://ry.ca/2024/07/pwc-277-strong-pair-counting-common/ +# +# 2024 Ryan Thompson <rjt@cpan.org> + +use 5.010; +use warnings; +use strict; +use Carp; +use List::Util qw< uniq min >; +no warnings 'uninitialized'; + +sub strong_pair { + ref || $_ !~ /^\-?\d+$/ and croak 'Arguments must be integers' for @_; + my @i = uniq sort { $a <=> $b } @_; + + grep { my ($x,$y) = @$_; $x < $y and $y < 2*$x } + map { my $i = $_; map { [ @_[$i,$_] ] } $i+1..$#i } 0..$#i +} + +1; diff --git a/challenge-280/0rir/raku/ch-1.raku b/challenge-280/0rir/raku/ch-1.raku new file mode 100644 index 0000000000..be79984f82 --- /dev/null +++ b/challenge-280/0rir/raku/ch-1.raku @@ -0,0 +1,68 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ « » ∴ +use v6.d; +use Test; + +=begin comment +280 1: Twice Appearance Submitted by: Mohammad Sajid Anwar +You are given a string, $str, containing lowercase English letters only. + +Write a script to print the first letter that appears twice. + +Example 1 +Input: $str = "acbddbca" +Output: "d" +Example 2 +Input: $str = "abccd" +Output: "c" +Example 3 +Input: $str = "abcdabbb" +Output: "a" +=end comment + +my @Test = + # $in $exp + "acbddbca", "d", + "abccd", "c", + "abcdabbb", "a", + "abcdefgh", Str, + "", Str, +; +my @Mix = + "abcdefghiFi", 'f', +; +plan @Test * 1.5 + @Mix; + +multi task( Str:D $in, Bool:D :$case-force! ) { task( $in.lc); } +multi task( Str:D $in, Bool:D :$case-ck! ) { + die qq{ERROR: "$in" is not lowercase.} if $in ne $in.lc; + task( $in); +} +multi task( Str:D $in --> Str ) { + my $s = ().SetHash; + my $e = $s.elems; + for 0..$in.chars -> \i { + my $c = $in.substr( i, 1); + $s.set: $c; + return $c if $s.elems == $e; + ++$e ; + } + Str; +} + +for @Test -> $in, $exp { + is task($in), $exp, ($exp // "(Str)") ~ " <- $in"; + is task($in, :case-ck), + $exp, ":case-ck " ~ ($exp // "(Str)") ~ " <- $in"; + is task($in, :case-force), + $exp, ":case-force " ~ ($exp // "(Str)") ~ " <- $in"; +} +for @Mix -> $in, $exp { + dies-ok { task($in, :case-ck)}, ":case-ck dies <- $in"; + is task($in, :case-force), $exp, ":case-force $exp <- $in"; +} +done-testing; + +my $str = 'abcdefghijklmnopqrstuvwxyzs'; +say qq{\nInput: \$str = "$str"\nOutput: "&task($str)"}; + diff --git a/challenge-280/0rir/raku/ch-2.raku b/challenge-280/0rir/raku/ch-2.raku new file mode 100644 index 0000000000..c60a29bdcc --- /dev/null +++ b/challenge-280/0rir/raku/ch-2.raku @@ -0,0 +1,140 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ « » ∴ +use v6.d; +use Test; + +=begin comment +Task 2: Count Asterisks +Submitted by: Mohammad Sajid Anwar + REPHASED +Given a string, $str, where two consecutive vertical bars group the chars +between them; a bar can only pair with one or none other bar. +Return the number of asterisks, *, which are not within such pairs. + +Example 1 +Input: $str = "p|*e*rl|w**e|*ekly|" +Ouput: 2 + +The characters we are looking here are "p" and "w**e". +Example 2 +Input: $str = "perl" +Ouput: 0 +Example 3 +Input: $str = "th|ewe|e**|k|l***ych|alleng|e" +Ouput: 5 + +The characters we are looking here are "th", "e**", "l***ych" and "e". +=end comment + +my @Test = + # str exp + '', 0, + # ^ sep $ + '|', 0, + # ^ outer $ + 'x', 0, + '*', 1, + # ^ outer sep $ + '*|', 1, + 'x|', 0, + # ^ sep outer $ + '|*', 1, + '|x', 0, + + # ^ outer sep outer $ + '*|*', 2, + + # ^ outer pair … + 'x||', 0, + 'x|i|', 0, + 'x|*|', 0, + '*||', 1, + '*|i|', 1, + '*|*|', 1, + '*|*||i|', 1, + 'x||x', 0, + 'x|i|x', 0, + 'x|*|x', 0, + 'x|*||i|x', 0, + '*||x', 1, + '*|i|x', 1, + '*|*|x', 1, + '*|*||i|x', 1, + 'x|*||i|', 0, + '*|*|*', 2, + '*|a|a', 1, + 'a|*|a', 0, + 'a|a|*', 1, + '*|*|x', 1, + + # ^ pair … + '||', 0, + '|i|', 0, + '|*|', 0, + + '||x', 0, + '|i|x', 0, + '|*|x', 0, + '|*||i|', 0, + '|*|*', 1, + '|*||i|x', 0, + + # ^ pair … trailing sep + 'p|*e*rl|w**e|*ekly|*x*|', 4, + 'p|*e*rl|w**e|*ekly||', 2, + 'th|ewe|e**|k|l***ych|alleng|e|', 5, + 'th|ewe|e**|k|l***ych|alleng|*e|', 6, + 'th|ewe|e**|k|l***ych|alleng|*e|e', 6, + 'th|ewe|e**|k|l***ych|alleng|*e|*', 7, + + # given + 'p|*e*rl|w**e|*ekly|', 2, + 'perl', 0, + 'th|ewe|e**|k|l***ych|alleng|e', 5, +; +plan @Test ÷ 2; + +#use Grammar::Tracer; + +grammar pair-exclus { + rule TOP { # The idea is to never re-traverse an <outer> … + ^ [ # shorts + || $ + || <sep> $ + || <sep> <outer> $ + || <outer> [ $ || <sep> $ || <sep> <outer> ] $ + ] + || + [ + || [ <pair>+ <outer>? ]+ [ <sep> <outer>? ]? $ + || <outer> [ <pair>+ <outer>? ]+ [ <sep> <outer>? ]? $ + ] + } + rule pair { <sep> <inner>? <sep> } + token inner { <-[ | ]>+ } + token outer { <-[ | ]>+ } # Rename/alias text w/o capture? + rule text { <-[ | ]>+ } # How to use named regex here? + token sep { '|' } +} + +class Count { + # … so that no containing rule needs to filter <outer>.made + my $char-sot = '*'; + method TOP($/) { make sum $<outer>».made } + method outer($/) { make $/.comb.grep( $char-sot ).elems } +} + +multi task( Any:U $data ) { die 'No match' } +multi task( $data -->Int) { + my $match = pair-exclus.parse( $data, :actions(Count.new )); + $match.made +} +for @Test -> $data, $exp { + is task( $data), $exp, "$exp <~∘ $data"; +} + +done-testing; + +my $str = 'th|ewe|e**|k|l***ych|alleng|*e|'; + +say "\nInput: \$str = $str.raku()\nOutput: &task( $str)" diff --git a/challenge-280/alexander-karelas/perl/ch-2.pl b/challenge-280/alexander-karelas/perl/ch-2.pl new file mode 100755 index 0000000000..2788ae1f71 --- /dev/null +++ b/challenge-280/alexander-karelas/perl/ch-2.pl @@ -0,0 +1,16 @@ +#!/usr/bin/env perl + +use v5.40; + +use Test::More; + +sub do_it ($str) { + $str =~ s/\|.*?\|//sg; + return scalar(() = $str =~ /\*/g); +} + +is do_it("p|*e*rl|w**e|*ekly|"), 2, 'Example 1'; +is do_it("perl"), 0, 'Example 2'; +is do_it("th|ewe|e**|k|l***ych|alleng|e"), 5, 'Example 3'; + +done_testing; diff --git a/challenge-280/bob-lied/README b/challenge-280/bob-lied/README index c57c6cd9ae..ad25c661d8 100644 --- a/challenge-280/bob-lied/README +++ b/challenge-280/bob-lied/README @@ -1,4 +1,4 @@ -Solutions to weekly challenge 279 by Bob Lied +Solutions to weekly challenge 280 by Bob Lied -https://perlweeklychallenge.org/blog/perl-weekly-challenge-279/ -https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-279/bob-lied +https://perlweeklychallenge.org/blog/perl-weekly-challenge-280/ +https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge |
