diff options
| -rw-r--r-- | challenge-287/jo-37/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-287/jo-37/perl/ch-1.pl | 66 | ||||
| -rwxr-xr-x | challenge-287/jo-37/perl/ch-2.pl | 65 |
3 files changed, 132 insertions, 0 deletions
diff --git a/challenge-287/jo-37/blog.txt b/challenge-287/jo-37/blog.txt new file mode 100644 index 0000000000..696b50bc78 --- /dev/null +++ b/challenge-287/jo-37/blog.txt @@ -0,0 +1 @@ +https://github.sommrey.de/the-bears-den/2024/09/22/ch-287.html diff --git a/challenge-287/jo-37/perl/ch-1.pl b/challenge-287/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..3e2f5dd8e1 --- /dev/null +++ b/challenge-287/jo-37/perl/ch-1.pl @@ -0,0 +1,66 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0; +use List::Util qw(reduce max); +use experimental 'signatures'; + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless @ARGV; +usage: $0 [-examples] [-tests] [STR] + +-examples + run the examples from the challenge + +-tests + run some tests + +STR + a string + +EOS + + +### Input and Output + +say strong_password(shift); + + +### Implementation +# +# For details see: +# https://github.sommrey.de/the-bears-den/2024/09/22/ch-287.html#task-1 + + +sub strong_password ($str) { + max +(length($str) < 6 ? 6 - length($str) : 0), + (3 - grep eval "\$str =~ tr/$_//", qw(a-z A-Z 0-9)), + (reduce {$a + int length($b) / 3} 0, $str =~ /((.)\2{2,})/g); +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is strong_password("a"), 5, "example 1"; + is strong_password("aB2"), 3, "example 2"; + is strong_password("Paasw0rd"), 0, "example 3"; + is strong_password("Paaasw0rd"), 1, "example 4"; + is strong_password("aaaaaa"), 2, "example 5"; + } + + SKIP: { + skip "tests" unless $tests; + + is strong_password("aaaaaaaa"), 2, "sequence and classes"; + } + + done_testing; + exit; +} diff --git a/challenge-287/jo-37/perl/ch-2.pl b/challenge-287/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..27124c11b7 --- /dev/null +++ b/challenge-287/jo-37/perl/ch-2.pl @@ -0,0 +1,65 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0; +use Regexp::Common; + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless @ARGV; +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))[!valid_number(shift)]; + + +### Implementation +# +# For details see: +# https://github.sommrey.de/the-bears-den/2024/09/22/ch-287.html#task-2 + + +sub valid_number { + shift =~ /^$RE{num}{real}\z/; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + ok valid_number("1"), "example 1"; + ok !valid_number("a"), "example 2"; + ok !valid_number("."), "example 3"; + ok !valid_number("1.2e4.2"), "example 4"; + ok valid_number("-1."), "example 5"; + ok valid_number("+1E-8"), "example 6"; + ok valid_number(".44"), "example 7"; + } + + SKIP: { + skip "tests" unless $tests; + + ok !valid_number("42\n"), "newline not allowed"; + } + + done_testing; + exit; +} |
