diff options
| author | Mohammad Sajid Anwar <mohammad.anwar@yahoo.com> | 2024-09-22 21:12:26 +0100 |
|---|---|---|
| committer | Mohammad Sajid Anwar <mohammad.anwar@yahoo.com> | 2024-09-22 21:12:26 +0100 |
| commit | a27dc5f5d4c66e571ad85cc0bb6a79008b800833 (patch) | |
| tree | 8935c7511cf3ae86072dd53712ff9efcf19d6728 | |
| parent | f8b75f12d59a33e777b433c638538b6fe9261978 (diff) | |
| download | perlweeklychallenge-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.
27 files changed, 3518 insertions, 3117 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 diff --git a/stats/pwc-challenge-197.json b/stats/pwc-challenge-197.json index d413f0e8ed..fd42134ac5 100644 --- a/stats/pwc-challenge-197.json +++ b/stats/pwc-challenge-197.json @@ -1,228 +1,32 @@ { - "series" : [ - { - "colorByPoint" : 1, - "name" : "The Weekly Challenge - 197", - "data" : [ - { - "y" : 2, - "drilldown" : "Adam Russell", - "name" : "Adam Russell" - }, - { - "name" : "Ali Moradi", - "drilldown" : "Ali Moradi", - "y" : 4 - }, - { - "y" : 3, - "drilldown" : "Arne Sommer", - "name" : "Arne Sommer" - }, - { - "name" : "Athanasius", - "y" : 4, - "drilldown" : "Athanasius" - }, - { - "name" : "Bob Lied", - "drilldown" : "Bob Lied", - "y" : 2 - }, - { - "drilldown" : "Bruce Gray", - "y" : 2, - "name" : "Bruce Gray" - }, - { - "drilldown" : "Carlos Oliveira", - "y" : 2, - "name" : "Carlos Oliveira" - }, - { - "y" : 2, - "drilldown" : "Cheok-Yin Fung", - "name" : "Cheok-Yin Fung" - }, - { - "name" : "Colin Crain", - "drilldown" : "Colin Crain", - "y" : 3 - }, - { - "drilldown" : "Dave Jacoby", - "y" : 2, - "name" : "Dave Jacoby" - }, - { - "drilldown" : "David Ferrone", - "y" : 2, - "name" : "David Ferrone" - }, - { - "name" : "E. Choroba", - "drilldown" : "E. Choroba", - "y" : 2 - }, - { - "name" : "Feng Chang", - "drilldown" : "Feng Chang", - "y" : 2 - }, - { - "name" : "Flavio Poletti", - "drilldown" : "Flavio Poletti", - "y" : 6 - }, - { - "name" : "Jaldhar H. Vyas", - "y" : 5, - "drilldown" : "Jaldhar H. Vyas" - }, - { - "name" : "James Smith", - "y" : 3, - "drilldown" : "James Smith" - }, - { - "name" : "Jan Krnavek", - "y" : 2, - "drilldown" : "Jan Krnavek" - }, - { - "name" : "Jorg Sommrey", - "drilldown" : "Jorg Sommrey", - "y" : 2 - }, - { - "name" : "Laurent Rosenfeld", - "y" : 5, - "drilldown" : "Laurent Rosenfeld" - }, - { - "drilldown" : "Lubos Kolouch", - "y" : 3, - "name" : "Lubos Kolouch" - }, - { - "name" : "Luca Ferrari", - "drilldown" : "Luca Ferrari", - "y" : 8 - }, - { - "drilldown" : "Mark Anderson", - "y" : 2, - "name" : "Mark Anderson" - }, - { - "y" : 2, - "drilldown" : "Niels van Dijke", - "name" : "Niels van Dijke" - }, - { - "name" : "Paulo Custodio", - "y" : 2, - "drilldown" : "Paulo Custodio" - }, - { - "drilldown" : "Pip Stuart", - "y" : 2, - "name" : "Pip Stuart" - }, - { - "name" : "Robbie Hatley", - "drilldown" : "Robbie Hatley", - "y" : 3 - }, - { - "y" : 2, - "drilldown" : "Robert DiCicco", - "name" : "Robert DiCicco" - }, - { - "y" : 2, - "drilldown" : "Robert Ransbottom", - "name" : "Robert Ransbottom" - }, - { - "drilldown" : "Roger Bell_West", - "y" : 5, - "name" : "Roger Bell_West" - }, - { - "name" : "Simon Green", - "drilldown" : "Simon Green", - "y" : 3 - }, - { - "name" : "Stephen G. Lynn", - "y" : 5, - "drilldown" : "Stephen G. Lynn" - }, - { - "name" : "Thomas Kohler", - "drilldown" : "Thomas Kohler", - "y" : 4 - }, - { - "name" : "Ulrich Rieke", - "drilldown" : "Ulrich Rieke", - "y" : 4 - }, - { - "name" : "W. Luis Mochan", - "y" : 3, - "drilldown" : "W. Luis Mochan" - } - ] - } - ], - "xAxis" : { - "type" : "category" - }, - "title" : { - "text" : "The Weekly Challenge - 197" - }, "plotOptions" : { "series" : { - "borderWidth" : 0, "dataLabels" : { "enabled" : 1, "format" : "{point.y}" - } + }, + "borderWidth" : 0 } }, - "chart" : { - "type" : "column" - }, - "tooltip" : { - "pointFormat" : "<span style='color:{point.color}'>{point.name}</span>: <b>{point.y:f}</b><br/>", - "followPointer" : 1, - "headerFormat" : "<span style='font-size:11px'>{series.name}</span><br/>" - }, "subtitle" : { - "text" : "[Champions: 34] Last updated at 2024-07-29 17:29:09 GMT" + "text" : "[Champions: 35] Last updated at 2024-09-22 20:09:14 GMT" }, - "yAxis" : { - "title" : { - "text" : "Total Solutions" - } + "legend" : { + "enabled" : 0 }, "drilldown" : { "series" : [ { - "id" : "Adam Russell", - "name" : "Adam Russell", "data" : [ [ "Perl", 2 ] - ] + ], + "id" : "Adam Russell", + "name" : "Adam Russell" }, { - "id" : "Ali Moradi", - "name" : "Ali Moradi", "data" : [ [ "Perl", @@ -232,11 +36,11 @@ "Raku", 2 ] - ] + ], + "name" : "Ali Moradi", + "id" : "Ali Moradi" }, { - "id" : "Arne Sommer", - "name" : "Arne Sommer", "data" : [ [ "Raku", @@ -246,7 +50,9 @@ "Blog", 1 ] - ] + ], + "id" : "Arne Sommer", + "name" : "Arne Sommer" }, { "id" : "Athanasius", @@ -263,18 +69,18 @@ ] }, { - "id" : "Bob Lied", - "name" : "Bob Lied", "data" : [ [ "Perl", 2 ] - ] + ], + "id" : "Bob Lied", + "name" : "Bob Lied" }, { - "name" : "Bruce Gray", "id" : "Bruce Gray", + "name" : "Bruce Gray", "data" : [ [ "Raku", @@ -283,14 +89,14 @@ ] }, { + "id" : "Carlos Oliveira", + "name" : "Carlos Oliveira", "data" : [ [ "Perl", 2 ] - ], - "name" : "Carlos Oliveira", - "id" : "Carlos Oliveira" + ] }, { "id" : "Cheok-Yin Fung", @@ -313,18 +119,18 @@ 1 ] ], - "id" : "Colin Crain", - "name" : "Colin Crain" + "name" : "Colin Crain", + "id" : "Colin Crain" }, { - "id" : "Dave Jacoby", - "name" : "Dave Jacoby", "data" : [ [ "Perl", 2 ] - ] + ], + "id" : "Dave Jacoby", + "name" : "Dave Jacoby" }, { "data" : [ @@ -333,8 +139,8 @@ 2 ] ], - "name" : "David Ferrone", - "id" : "David Ferrone" + "id" : "David Ferrone", + "name" : "David Ferrone" }, { "data" : [ @@ -343,8 +149,8 @@ 2 ] ], - "id" : "E. Choroba", - "name" : "E. Choroba" + "name" : "E. Choroba", + "id" : "E. Choroba" }, { "data" : [ @@ -357,8 +163,8 @@ "name" : "Feng Chang" }, { - "id" : "Flavio Poletti", "name" : "Flavio Poletti", + "id" : "Flavio Poletti", "data" : [ [ "Perl", @@ -389,12 +195,12 @@ 1 ] ], - "id" : "Jaldhar H. Vyas", - "name" : "Jaldhar H. Vyas" + "name" : "Jaldhar H. Vyas", + "id" : "Jaldhar H. Vyas" }, { - "name" : "James Smith", "id" : "James Smith", + "name" : "James Smith", "data" : [ [ "Perl", @@ -407,28 +213,26 @@ ] }, { + "name" : "Jan Krnavek", + "id" : "Jan Krnavek", "data" : [ [ "Raku", 2 ] - ], - "id" : "Jan Krnavek", - "name" : "Jan Krnavek" + ] }, { - "name" : "Jorg Sommrey", - "id" : "Jorg Sommrey", "data" : [ [ "Perl", 2 ] - ] + ], + "name" : "Jorg Sommrey", + "id" : "Jorg Sommrey" }, { - "name" : "Laurent Rosenfeld", - "id" : "Laurent Rosenfeld", "data" : [ [ "Perl", @@ -442,9 +246,13 @@ "Blog", 1 ] - ] + ], + "id" : "Laurent Rosenfeld", + "name" : "Laurent Rosenfeld" }, { + "id" : "Lubos Kolouch", + "name" : "Lubos Kolouch", "data" : [ [ "Perl", @@ -454,9 +262,7 @@ "Blog", 1 ] - ], - "id" : "Lubos Kolouch", - "name" : "Lubos Kolouch" + ] }, { "data" : [ @@ -473,14 +279,14 @@ "name" : "Luca Ferrari" }, { + "id" : "Mark Anderson", + "name" : "Mark Anderson", "data" : [ [ "Raku", 2 ] - ], - "id" : "Mark Anderson", - "name" : "Mark Anderson" + ] }, { "data" : [ @@ -489,12 +295,22 @@ 2 ] ], - "id" : "Niels van Dijke", - "name" : "Niels van Dijke" + "name" : "Niels van Dijke", + "id" : "Niels van Dijke" }, { + "data" : [ + [ + "Perl", + 2 + ] + ], "name" : "Paulo Custodio", - "id" : "Paulo Custodio", + "id" : "Paulo Custodio" + }, + { + "id" : "Peter Meszaros", + "name" : "Peter Meszaros", "data" : [ [ "Perl", @@ -503,14 +319,14 @@ ] }, { - "id" : "Pip Stuart", - "name" : "Pip Stuart", "data" : [ [ "Perl", 2 ] - ] + ], + "name" : "Pip Stuart", + "id" : "Pip Stuart" }, { "id" : "Robbie Hatley", @@ -527,8 +343,6 @@ ] }, { - "name" : "Robert DiCicco", - "id" : "Robert DiCicco", "data" : [ [ "Perl", @@ -538,7 +352,9 @@ "Raku", 1 ] - ] + ], + "name" : "Robert DiCicco", + "id" : "Robert DiCicco" }, { "data" : [ @@ -551,8 +367,6 @@ "name" : "Robert Ransbottom" }, { - "id" : "Roger Bell_West", - "name" : "Roger Bell_West", "data" : [ [ "Perl", @@ -566,7 +380,9 @@ "Blog", 1 ] - ] + ], + "name" : "Roger Bell_West", + "id" : "Roger Bell_West" }, { "data" : [ @@ -644,7 +460,206 @@ } ] }, - "legend" : { - "enabled" : 0 - } + "yAxis" : { + "title" : { + "text" : "Total Solutions" + } + }, + "chart" : { + "type" : "column" + }, + "title" : { + "text" : "The Weekly Challenge - 197" + }, + "xAxis" : { + "type" : "category" + }, + "tooltip" : { + "headerFormat" : "<span style='font-size:11px'>{series.name}</span><br/>", + "followPointer" : 1, + "pointFormat" : "<span style='color:{point.color}'>{point.name}</span>: <b>{point.y:f}</b><br/>" + }, + "series" : [ + { + "data" : [ + { + "y" : 2, + "drilldown" : "Adam Russell", + "name" : "Adam Russell" + }, + { + "drilldown" : "Ali Moradi", + "y" : 4, + "name" : "Ali Moradi" + }, + { + "name" : "Arne Sommer", + "y" : 3, + "drilldown" : "Arne Sommer" + }, + { + "name" : "Athanasius", + "drilldown" : "Athanasius", + "y" : 4 + }, + { + "name" : "Bob Lied", + "drilldown" : "Bob Lied", + "y" : 2 + }, + { + "drilldown" : "Bruce Gray", + "y" : 2, + "name" : "Bruce Gray" + }, + { + "name" : "Carlos Oliveira", + "y" : 2, + "drilldown" : "Carlos Oliveira" + }, + { + "drilldown" : "Cheok-Yin Fung", + "y" : 2, + "name" : "Cheok-Yin Fung" + }, + { + "name" : "Colin Crain", + "drilldown" : "Colin Crain", + "y" : 3 + }, + { + "drilldown" : "Dave Jacoby", + "y" : 2, + "name" : "Dave Jacoby" + }, + { + "name" : "David Ferrone", + "drilldown" : "David Ferrone", + "y" : 2 + }, + { + "drilldown" : "E. Choroba", + "y" : 2, + "name" : "E. Choroba" + }, + { + "drilldown" : "Feng Chang", + "y" : 2, + "name" : "Feng Chang" + }, + { + "y" : 6, + "drilldown" : "Flavio Poletti", + "name" : "Flavio Poletti" + }, + { + "drilldown" : "Jaldhar H. Vyas", + "y" : 5, + "name" : "Jaldhar H. Vyas" + }, + { + "name" : "James Smith", + "drilldown" : "James Smith", + "y" : 3 + }, + { + "name" : "Jan Krnavek", + "y" : 2, + "drilldown" : "Jan Krnavek" + }, + { + "name" : "Jorg Sommrey", + "drilldown" : "Jorg Sommrey", + "y" : 2 + }, + { + "name" : "Laurent Rosenfeld", + "drilldown" : "Laurent Rosenfeld", + "y" : 5 + }, + { + "name" : "Lubos Kolouch", + "y" : 3, + "drilldown" : "Lubos Kolouch" + }, + { + "y" : 8, + "drilldown" : "Luca Ferrari", + "name" : "Luca Ferrari" + }, + { |
