aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-04-19 22:01:35 +0100
committerGitHub <noreply@github.com>2024-04-19 22:01:35 +0100
commite8feffec5ae496626ef43d772ea8c956119bf534 (patch)
tree68198b4c737557ce146185b4795925a62b9cd7bc
parentbe33dd2aba1140bd39954f64816a59e4b89e643c (diff)
parentd31dafebd4855cfdbc6453357ed9b0aec8a17bdb (diff)
downloadperlweeklychallenge-club-e8feffec5ae496626ef43d772ea8c956119bf534.tar.gz
perlweeklychallenge-club-e8feffec5ae496626ef43d772ea8c956119bf534.tar.bz2
perlweeklychallenge-club-e8feffec5ae496626ef43d772ea8c956119bf534.zip
Merge pull request #9955 from jo-37/contrib
Solutions to challenge 265
-rw-r--r--challenge-265/jo-37/blog.txt1
-rwxr-xr-xchallenge-265/jo-37/perl/ch-1.pl60
-rwxr-xr-xchallenge-265/jo-37/perl/ch-2.pl72
3 files changed, 133 insertions, 0 deletions
diff --git a/challenge-265/jo-37/blog.txt b/challenge-265/jo-37/blog.txt
new file mode 100644
index 0000000000..7d586274b8
--- /dev/null
+++ b/challenge-265/jo-37/blog.txt
@@ -0,0 +1 @@
+https://github.sommrey.de/the-bears-den/2024/04/19/ch-265.html
diff --git a/challenge-265/jo-37/perl/ch-1.pl b/challenge-265/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..8f44c31fbb
--- /dev/null
+++ b/challenge-265/jo-37/perl/ch-1.pl
@@ -0,0 +1,60 @@
+#!/usr/bin/perl -s
+
+use v5.24;
+use Test2::V0;
+use List::Util 'reduce';
+
+our ($tests, $examples);
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV;
+usage: $0 [-examples] [-tests] [N...]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+N...
+ list of integers
+
+EOS
+
+
+### Input and Output
+
+say appearance33(@ARGV);
+
+
+### Implementation
+
+sub appearance33 {
+ my %count;
+ reduce {
+ ++$count{$b} * 100 / @_ >= 33 && $b < ($a // 'inf') ? $b : $a;
+ } undef, @_;
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is appearance33(1,2,3,3,3,3,4,2), 3, 'example 1';
+ is appearance33(1, 1), 1, 'example 2';
+ is appearance33(1, 2, 3), 1, 'example 3';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+ is appearance33((2, 3) x 33, (1) x 34), 1, 'exactly 33%';
+ is appearance33((1, 2, 3) x 32, (4) x 4), U(), 'nothing found';
+ }
+
+ done_testing;
+ exit;
+}
diff --git a/challenge-265/jo-37/perl/ch-2.pl b/challenge-265/jo-37/perl/ch-2.pl
new file mode 100755
index 0000000000..56929d721f
--- /dev/null
+++ b/challenge-265/jo-37/perl/ch-2.pl
@@ -0,0 +1,72 @@
+#!/usr/bin/perl -s
+
+use v5.24;
+use Test2::V0;
+use List::AllUtils qw(pairmap min_by count_by);
+use experimental 'signatures';
+
+our ($tests, $examples);
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV;
+usage: $0 [-examples] [-tests] [TARGET WORD...]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+TARGET
+ target string
+
+WORD...
+ list of words
+
+EOS
+
+
+### Input and Output
+
+say completing_word(@ARGV);
+
+
+### Implementation
+
+sub completing_word ($str, @str) {
+ my $target = qr{
+ ^
+ @{[
+ pairmap { '(?=' . ".*?$a" x $b . ')'}
+ count_by {$_}
+ lc($str) =~ /([[:alpha:]])/g
+ ]}
+ [[:alpha:]]*
+ $
+ }x;
+
+ min_by {length} grep {lc =~ /$target/} @str;
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+ is completing_word('aBc 11c' => 'accbbb', 'abc', 'abbc'),
+ 'accbbb', 'example 1';
+ is completing_word('Da2 abc' => 'abcm', 'baacd', 'abaadc'),
+ 'baacd', 'example 2';
+ is completing_word('JB 007' => 'jj', 'bb', 'bjb'),
+ 'bjb', 'example 3';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+ }
+
+ done_testing;
+ exit;
+}