aboutsummaryrefslogtreecommitdiff
path: root/challenge-287
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <mohammad.anwar@yahoo.com>2024-09-22 21:12:26 +0100
committerMohammad Sajid Anwar <mohammad.anwar@yahoo.com>2024-09-22 21:12:26 +0100
commita27dc5f5d4c66e571ad85cc0bb6a79008b800833 (patch)
tree8935c7511cf3ae86072dd53712ff9efcf19d6728 /challenge-287
parentf8b75f12d59a33e777b433c638538b6fe9261978 (diff)
downloadperlweeklychallenge-club-a27dc5f5d4c66e571ad85cc0bb6a79008b800833.tar.gz
perlweeklychallenge-club-a27dc5f5d4c66e571ad85cc0bb6a79008b800833.tar.bz2
perlweeklychallenge-club-a27dc5f5d4c66e571ad85cc0bb6a79008b800833.zip
- Added blog post by E. Choroba.
- Added blog post by Roger Bell_West. - Added solutions by Andrew Schneider. - Added solutions by Mark Anderson. - Added solutions by Simon Green. - Added solutions by Peter Meszaros. - Added solutions by BarrOff. - Added solutions by Jorg Sommrey. - Added solutions by Reinier Maliepaard. - Added solutions by Wanderdoc.
Diffstat (limited to 'challenge-287')
-rw-r--r--challenge-287/reinier-maliepaard/blog.txt1
-rw-r--r--challenge-287/reinier-maliepaard/perl/ch-1.pl82
-rwxr-xr-xchallenge-287/wanderdoc/perl/ch-1.pl76
-rwxr-xr-xchallenge-287/wanderdoc/perl/ch-2.pl98
4 files changed, 257 insertions, 0 deletions
diff --git a/challenge-287/reinier-maliepaard/blog.txt b/challenge-287/reinier-maliepaard/blog.txt
new file mode 100644
index 0000000000..0f1302ab60
--- /dev/null
+++ b/challenge-287/reinier-maliepaard/blog.txt
@@ -0,0 +1 @@
+https://reiniermaliepaard.nl/perl/pwc/index.php?id=pwc287
diff --git a/challenge-287/reinier-maliepaard/perl/ch-1.pl b/challenge-287/reinier-maliepaard/perl/ch-1.pl
new file mode 100644
index 0000000000..e51389c722
--- /dev/null
+++ b/challenge-287/reinier-maliepaard/perl/ch-1.pl
@@ -0,0 +1,82 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+
+=begin
+The task isn't to generate a strong password, but simply to check how many
+steps are needed to turn a given string into a strong password. If a character
+is missing, you only need to count the insertion of that character. In the case
+of repeating character triplets, you just need to break the triplet by replacing
+one of the characters with another. I used a space to count the number of triplet
+replacements. And if the number of triplet breaks is greater than the number of
+missing characters, then the total number of steps is equal to the number of
+triplet breaks: after all, a missing character could have been used to break the
+triplet.
+=cut
+
+sub strong_password {
+ my ($pw) = @_;
+ my $count = 0;
+ my $min_len_pw = 6;
+ my $len = length($pw);
+
+ # Simultaneously check for missing lowercase, uppercase, and digit characters
+ # If a character is missing, simulate its addition
+ ($len++ && $count++) if ($pw !~ /[a-z]/);
+ ($len++ && $count++) if ($pw !~ /[A-Z]/);
+ ($len++ && $count++) if ($pw !~ /[0-9]/);
+
+ # Replace the third character of triplets with a space and count the replacement
+ # only if no missing characters were found previously
+ my $count_triplets = 0;
+ while ($pw =~ /(.)\1\1/) {
+
+ # If a triplet is found, replace the third character of the triplet
+ # with a space to break the repetition and maintain the string length.
+ $pw =~ s/(.)\1\1/${1}${1} /;
+ $count_triplets++;
+ }
+ # A missing character could have been used to break the triplet
+ $count = $count_triplets if ($count_triplets > $count);
+
+ # Ensure password meets minimum length
+ $count += $min_len_pw - $len if ($len < $min_len_pw);
+
+ return ($count);
+}
+
+# TESTS
+
+my $str;
+
+# Example 1
+$str = "a";
+print(strong_password($str), "\n"); # Output: 5
+
+# Example 2
+$str = "aB2";
+print(strong_password($str), "\n"); # Output: 3
+
+# Example 3
+$str = "PaaSW0rd";
+print(strong_password($str), "\n"); # Output: 0
+
+# Example 4
+$str = "Paaasw0rd";
+print(strong_password($str), "\n"); # Output: 1
+
+# Example 5
+$str = "aaaaa";
+print(strong_password($str), "\n"); # Output: 2
+
+# Example 6
+$str = "Abbbbbb";
+print(strong_password($str), "\n"); # Output: 2
+
+# Example 7
+$str = "bbbAb0bbb";
+print(strong_password($str), "\n"); # Output: 2
+
+# Example 8
+$str = "bbbABC";
+print(strong_password($str), "\n"); # Output: 1
diff --git a/challenge-287/wanderdoc/perl/ch-1.pl b/challenge-287/wanderdoc/perl/ch-1.pl
new file mode 100755
index 0000000000..5e05c1e806
--- /dev/null
+++ b/challenge-287/wanderdoc/perl/ch-1.pl
@@ -0,0 +1,76 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+You are given a string, $str.
+
+Write a program to return the minimum number of steps required to make the given string very strong password. If it is already strong then return 0.
+
+Criteria:
+
+- It must have at least 6 characters.
+- It must contains at least one lowercase letter, at least one upper case letter and at least one digit.
+- It shouldn't contain 3 repeating characters in a row.
+
+Following can be considered as one step:
+
+- Insert one character
+- Delete one character
+- Replace one character with another
+
+Example 1
+
+Input: $str = "a"
+Output: 5
+
+Example 2
+
+Input: $str = "aB2"
+Output: 3
+
+Example 3
+
+Input: $str = "PaaSW0rd"
+Output: 0
+
+Example 4
+
+Input: $str = "Paaasw0rd"
+Output: 1
+
+Example 5
+
+Input: $str = "aaaaa"
+Output: 2
+=cut
+
+
+
+use Test2::V0;
+use List::Util qw(sum max);
+
+is(evaluate("a"), 5, 'Example 1');
+is(evaluate("aB2"), 3, 'Example 2');
+is(evaluate("PaaSW0rd"), 0, 'Example 3');
+is(evaluate("Paaasw0rd"), 1, 'Example 4');
+is(evaluate("aaaaa"), 2, 'Example 5');
+
+done_testing();
+
+sub evaluate
+{
+ my $fragment = $_[0];
+
+ my $steps = 0;
+ my $steps_length = max(0, 6 - length($fragment));
+
+ my $steps_repeating = $fragment =~ /((?<char>.)\k<char>{2,})/ ?
+ int(length($1)/3) : 0;
+ my $steps_lc = $fragment =~ /[a-z]/ ? 0 : 1;
+ my $steps_uc = $fragment =~ /[A-Z]/ ? 0 : 1;
+ my $steps_num = $fragment =~ /[0-9]/ ? 0 : 1;
+ $steps =
+ max($steps_length, $steps_repeating, sum($steps_lc, $steps_uc, $steps_num));
+ return $steps;
+}
diff --git a/challenge-287/wanderdoc/perl/ch-2.pl b/challenge-287/wanderdoc/perl/ch-2.pl
new file mode 100755
index 0000000000..72fd4d8d91
--- /dev/null
+++ b/challenge-287/wanderdoc/perl/ch-2.pl
@@ -0,0 +1,98 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+You are given a string, $str.
+
+Write a script to find if it is a valid number.
+
+Conditions for a valid number:
+
+- An integer number followed by an optional exponent.
+- A decimal number followed by an optional exponent.
+- An integer number is defined with an optional sign '-' or '+' followed by digits.
+
+Decimal Number:
+
+A decimal number is defined with an optional sign '-' or '+' followed by one of the following definitions:
+- Digits followed by a dot '.'.
+- Digits followed by a dot '.' followed by digits.
+- A dot '.' followed by digits.
+
+Exponent:
+
+An exponent is defined with an exponent notation 'e' or 'E' followed by an integer number.
+
+Example 1
+
+Input: $str = "1"
+Output: true
+
+Example 2
+
+Input: $str = "a"
+Output: false
+
+Example 3
+
+Input: $str = "."
+Output: false
+
+Example 4
+
+Input: $str = "1.2e4.2"
+Output: false
+
+Example 5
+
+Input: $str = "-1."
+Output: true
+
+Example 6
+
+Input: $str = "+1E-8"
+Output: true
+
+Example 7
+
+Input: $str = ".44"
+Output: true
+=cut
+
+
+
+use constant {true => 1, false => 0};
+use Test2::V0;
+
+
+is(validate_number('1'), true, 'Example 1');
+is(validate_number('a'), false, 'Example 2');
+is(validate_number('.'), false, 'Example 3');
+is(validate_number('1.2e4.2'), false, 'Example 4');
+is(validate_number('-1.'), true, 'Example 5');
+is(validate_number('+1E-8'), true, 'Example 6');
+is(validate_number('.44'), true, 'Example 7');
+
+is(validate_number('-1.2e-3'), true, 'Example 8 (added)');
+is(validate_number('+1.e-3'), true, 'Example 9 (added)');
+
+
+done_testing();
+
+
+
+
+sub validate_number
+{
+ my $str = $_[0];
+ # These are just strings (q or qq):
+ my $integer = q/[-+]?\d+/;
+ my $exponent = qq/[eE]${integer}/;
+ my $decimal = q/[-+]?(?:\d+\.|\d+\.\d+|\.\d+)/;
+
+
+ # This is the only qr statement:
+ my $valid_num = qr/(?:${integer}|${decimal})(?:${exponent})?/;
+ return $str =~ /\A${valid_num}\z/ ? true : false;
+} \ No newline at end of file