diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-06-14 22:04:04 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-06-14 22:04:04 +0100 |
| commit | 9e1f40d2dd4bc28dbd24fb9a1f558335769d990f (patch) | |
| tree | 83aff959a25f8d86dc0ff48171f791fd5dfe5d71 | |
| parent | a26ba4ca39df65aef91304b124c5d9c30099f3a1 (diff) | |
| parent | a9e4f0bb2e941f97b1312ca502269b1b734b5a5d (diff) | |
| download | perlweeklychallenge-club-9e1f40d2dd4bc28dbd24fb9a1f558335769d990f.tar.gz perlweeklychallenge-club-9e1f40d2dd4bc28dbd24fb9a1f558335769d990f.tar.bz2 perlweeklychallenge-club-9e1f40d2dd4bc28dbd24fb9a1f558335769d990f.zip | |
Merge pull request #10261 from jo-37/contrib
Solutions to challenge 273
| -rw-r--r-- | challenge-273/jo-37/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-273/jo-37/perl/ch-1.pl | 68 | ||||
| -rwxr-xr-x | challenge-273/jo-37/perl/ch-2.pl | 62 |
3 files changed, 131 insertions, 0 deletions
diff --git a/challenge-273/jo-37/blog.txt b/challenge-273/jo-37/blog.txt new file mode 100644 index 0000000000..a09857df8d --- /dev/null +++ b/challenge-273/jo-37/blog.txt @@ -0,0 +1 @@ +https://github.sommrey.de/the-bears-den/2024/06/14/ch-273.html diff --git a/challenge-273/jo-37/perl/ch-1.pl b/challenge-273/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..b832bb2989 --- /dev/null +++ b/challenge-273/jo-37/perl/ch-1.pl @@ -0,0 +1,68 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0; +use experimental 'signatures'; + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless @ARGV == 2; +usage: $0 [-examples] [-tests] [C STR] + +-examples + run the examples from the challenge + +-tests + run some tests + +C + a single character (or string) + +STR + a string + +EOS + + +### Input and Output + +say pct_sub(@ARGV); + + +### Implementation +# +# For details see: +# https://github.sommrey.de/the-bears-den/2024/06/14/ch-273.html#task-1 + + +sub pct_sub ($sub, $str) { + return int .5 + 100 * (() = /\Q$sub/g) * length($sub) / length for $str; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is pct_sub("e", "perl"), 25, 'example 1'; + is pct_sub("a", "java"), 50, 'example 2'; + is pct_sub("m", "python"), 0, 'example 3'; + is pct_sub("a", "ada"), 67, 'example 4'; + is pct_sub("l", "ballerina"), 22, 'example 5'; + is pct_sub("k", "analitik"), 13, 'example 6'; + } + + SKIP: { + skip "tests" unless $tests; + + is pct_sub("12", "123"), 67, 'not a single character'; + is pct_sub('.$', '.$--.$'), 67, 'meta characters'; + } + + done_testing; + exit; +} diff --git a/challenge-273/jo-37/perl/ch-2.pl b/challenge-273/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..0079a2fa55 --- /dev/null +++ b/challenge-273/jo-37/perl/ch-2.pl @@ -0,0 +1,62 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0; + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless @ARGV == 1; +usage: $0 [-examples] [-tests] [STR] + +-examples + run the examples from the challenge + +-tests + run some tests + +STR + a string + +EOS + + +### Input and Output + +say +(qw(true false))[!b_after_a(shift)]; + + +### Implementation +# +# For details see: +# https://github.sommrey.de/the-bears-den/2024/06/14/ch-273.html#task-2 + + +sub b_after_a { + shift =~ /^[^b]*+b[^a]*+$/; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + ok b_after_a("aabb"), 'example 1'; + ok !b_after_a("abab"), 'example 2'; + ok !b_after_a("aaa"), 'example 3'; + ok b_after_a("bbb"), 'example 4'; + + } + + SKIP: { + skip "tests" unless $tests; + + ok b_after_a("b"), 'single b'; + } + + done_testing; + exit; +} |
