aboutsummaryrefslogtreecommitdiff
path: root/challenge-287
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2024-09-22 20:17:22 +0200
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2024-09-22 20:19:40 +0200
commit7d0a73e6eab06187e61b16b59726f4b2798527cd (patch)
tree487a7f0710b50c6f5c09aa231f358b6c9d14787b /challenge-287
parent9691c964bfe50886d505401ee3db7ae6f3ef96a3 (diff)
downloadperlweeklychallenge-club-7d0a73e6eab06187e61b16b59726f4b2798527cd.tar.gz
perlweeklychallenge-club-7d0a73e6eab06187e61b16b59726f4b2798527cd.tar.bz2
perlweeklychallenge-club-7d0a73e6eab06187e61b16b59726f4b2798527cd.zip
Solution to task 1
Diffstat (limited to 'challenge-287')
-rwxr-xr-xchallenge-287/jo-37/perl/ch-1.pl66
1 files changed, 66 insertions, 0 deletions
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;
+}