aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-287/mattneleigh/perl/ch-1.pl122
-rwxr-xr-xchallenge-287/mattneleigh/perl/ch-2.pl96
2 files changed, 218 insertions, 0 deletions
diff --git a/challenge-287/mattneleigh/perl/ch-1.pl b/challenge-287/mattneleigh/perl/ch-1.pl
new file mode 100755
index 0000000000..cdd4875f91
--- /dev/null
+++ b/challenge-287/mattneleigh/perl/ch-1.pl
@@ -0,0 +1,122 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use English;
+
+################################################################################
+# Begin main execution
+################################################################################
+
+my @passwords = (
+ "a",
+ "aB2",
+ "PaaSW0rd",
+ "Paaasw0rd",
+ "aaaaa"
+);
+
+print("\n");
+foreach my $password (@passwords){
+ printf(
+ "Input: \$str = \"%s\"\nOutput: %d\n\n",
+ $password,
+ steps_to_strong_password($password)
+ );
+}
+
+exit(0);
+################################################################################
+# End main execution; subroutines follow
+################################################################################
+
+
+
+################################################################################
+# Given a prospective password, determine the minimum number of steps required
+# to convert the string to a "secure" password; if the password is already
+# "secure" according to the criteria below, 0 is returned
+#
+# Security criteria:
+# * The password must have at least six characters
+# * The password must contain at least one lowercase letter, at least one upper
+# case letter, and at least one digit
+# * The password must not contain three repeating characters in a row
+#
+# Each of the following operations can be considered one step:
+# * Inserting one character
+# * Deleting one character
+# * Replacing one character with another
+#
+# Takes one argument:
+# * The prospective password to examine (e.g. "aaaaa" )
+# Returns:
+# * The number of steps, as defined above, required to make the prospective
+# password secure (e.g. 2 )
+################################################################################
+sub steps_to_strong_password{
+ my $string = shift();
+
+ my $chars_subbed = 0;
+ my $chars_added = 0;
+ my $blocks_of_three = 0;
+
+ # Check to see whether we need upper case, lower
+ # case, or digits, respectively, and add up the
+ # total desired additions/substitutions of each
+ # type
+ $chars_subbed++
+ unless($string =~ m/[[:upper:]]/);
+ $chars_subbed++
+ unless($string =~ m/[[:lower:]]/);
+ $chars_subbed++
+ unless($string =~ m/\d/);
+
+ # See if additional characters are needed to meet
+ # the length requirement
+ $chars_added += 6 - length($string)
+ if(length($string) < 6);
+
+ # See if we have a block of three repeated chars...
+ if($string =~ m/(.)\1{2}/g){
+ $blocks_of_three = 1;
+
+ # See if we have multiple blocks of three repeated
+ # chars...
+ while($string =~ m/\G(.)\1{2}/g){
+ $blocks_of_three++;
+ }
+ }
+
+ # If the password is not long enough, we add enough
+ # characters to fit the requirements- all of which
+ # can be needed substitute characters so we
+ # subtract this from the quantity of required
+ # substitutions
+ $chars_subbed -= $chars_added;
+
+ # Similarly, if we have to replace characters to
+ # break up blocks of three characters, we can use
+ # needed substitute characters, so we subtract from
+ # the substitution count again
+ $chars_subbed -= $blocks_of_three;
+
+ return(
+ $blocks_of_three
+ +
+ $chars_added
+ +
+ (
+ # If we subtracted all (or more than all...) of the
+ # substitution characters, use zero for this term
+ $chars_subbed > 0 ?
+ $chars_subbed
+ :
+ 0
+ )
+ );
+
+}
+
+
+
diff --git a/challenge-287/mattneleigh/perl/ch-2.pl b/challenge-287/mattneleigh/perl/ch-2.pl
new file mode 100755
index 0000000000..fdb03ea155
--- /dev/null
+++ b/challenge-287/mattneleigh/perl/ch-2.pl
@@ -0,0 +1,96 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use English;
+
+################################################################################
+# Begin main execution
+################################################################################
+
+my @strings = (
+ # Given cases
+ "1",
+ "a",
+ ".",
+ "1.2e4.2",
+ "-1.",
+ "+1E-8",
+ ".44",
+
+ # Additional test cases
+ "-0.37",
+ "+.50",
+ "-123.45E+6"
+);
+
+print("\n");
+foreach my $string (@strings){
+ printf(
+ "Input: \$str = \"%s\"\nOutput: %s\n\n",
+ $string,
+ is_valid_number($string) ?
+ "true"
+ :
+ "false"
+ );
+}
+
+exit(0);
+################################################################################
+# End main execution; subroutines follow
+################################################################################
+
+
+
+################################################################################
+# Given a string, determine whether it contains a valid number. A valid number
+# consists of the following:
+#
+# * An Integer Number followed by an optional Exponent
+# or
+# * A Decimal Number followed by an optional Exponent
+#
+# An Integer Number is simply an uninterrupted sequence of digits (e.g. "123" )
+#
+# Decimal Numbers are defined as follows:
+# * Digits followed by a dot '.' (e.g. "123." )
+# * Digits followed by a dot '.' followed by digits (e.g. "123.45")
+# * A dot '.' followed by digits (e.g. ".45" )
+#
+# Numbers of either type may be preceded by an optional sign ('-' or '+')
+#
+# An Exponent is defined as 'e' or 'E' followed by a required sign ('-' or '+')
+# and an integer number (e.g. "E+6" )
+#
+# Takes one argument:
+# * A string containing the alleged number to verify (e.g. "-123.45E+6")
+# Returns:
+# * 0 if the string does not contain a valid number
+# * 1 if the string contains a valid number
+###############################################################################
+sub is_valid_number{
+ my $alleged_number = shift();
+
+ # Check for an invalid number ahead of a possible
+ # exponent...
+ return(0)
+ unless($alleged_number =~ m/^[+-]?(\d+\.?\d*|\.\d+)/g);
+
+ # If that was the whole string, it was a valid
+ # number...
+ return(1)
+ if(length($alleged_number) == pos($alleged_number));
+
+ # There was still more to parse- check for an
+ # invalid exponent
+ return(0)
+ unless($alleged_number =~ m/\G[Ee][+-]\d+$/g);
+
+ # Got here- valid number with exponent
+ return(1);
+
+}
+
+
+