diff options
| -rwxr-xr-x | challenge-287/peter-meszaros/perl/ch-1.pl | 103 | ||||
| -rwxr-xr-x | challenge-287/peter-meszaros/perl/ch-2.pl | 96 | ||||
| -rwxr-xr-x | challenge-287/peter-meszaros/tcl/ch-1.tcl | 122 | ||||
| -rwxr-xr-x | challenge-287/peter-meszaros/tcl/ch-2.tcl | 90 |
4 files changed, 411 insertions, 0 deletions
diff --git a/challenge-287/peter-meszaros/perl/ch-1.pl b/challenge-287/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..2e71c0e810 --- /dev/null +++ b/challenge-287/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,103 @@ +#!/usr/bin/env perl +# +=head1 Task 1: Strong Password + +Submitted by: Mohammad Sajid Anwar + +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 + +=head2 Example 1 + + Input: $str = "a" + Output: 5 + +=head2 Example 2 + + Input: $str = "aB2" + Output: 3 + +=head2 Example 3 + + Input: $str = "PaaSW0rd" + Output: 0 + +=head2 Example 4 + + Input: $str = "Paaasw0rd" + Output: 1 + +=head2 Example 5 + + Input: $str = "aaaaa" + Output: 2 + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + ['a', 5, 'Example 1'], + ['aB2', 3, 'Example 2'], + ['PaaSW0rd', 0, 'Example 3'], + ['Paaasw0rd', 1, 'Example 4'], + ['aaaaa', 2, 'Example 5'], +]; + +sub strong_password { + my $p = shift; + + my $cnt = 0; + + if ($p !~ /[a-z]/) { + $p =~ s/(.)\1{2}/$1$1x/ or $p .= 'x'; + ++$cnt; + } + if ($p !~ /[A-Z]/) { + $p =~ s/(.)\1{2}/$1$1X/ or $p .= 'X'; + ++$cnt; + } + if ($p !~ /[0-9]/) { + $p =~ s/(.)\1{2}/$1${1}0/ or $p .= '0'; + ++$cnt; + } + + while ($p =~ /(.)\1{2}/) { + $p =~ s/(.)\1{2}/$1 . $1 eq 'x' ? 'y' : 'x' . $1/e; + ++$cnt; + } + + if (length($p) < 6) { + my $c = substr($p, -1, 1) eq 'x' ? 'y' : 'x'; + my $n = 6 - length($p); + $p .= $c x $n; + $cnt += $n; + } + + return $cnt; +} + +for (@$cases) { + is(strong_password($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; diff --git a/challenge-287/peter-meszaros/perl/ch-2.pl b/challenge-287/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..369df8a595 --- /dev/null +++ b/challenge-287/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,96 @@ +#!/usr/bin/env perl +# +=head1 Task 2: Valid Number + +Submitted by: Mohammad Sajid Anwar + +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. + +=head2 Example 1 + + Input: $str = "1" + Output: true + +=head2 Example 2 + + Input: $str = "a" + Output: false + +=head2 Example 3 + + Input: $str = "." + Output: false + +=head2 Example 4 + + Input: $str = "1.2e4.2" + Output: false + +=head2 Example 5 + + Input: $str = "-1." + Output: true + +=head2 Example 6 + + Input: $str = "+1E-8" + Output: true + +=head2 Example 7 + + Input: $str = ".44" + Output: true + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; +use Scalar::Util qw/looks_like_number/; + +my $cases = [ + ['1', 1, 'Example 1'], + ['a', 0, 'Example 2'], + ['.', 0, 'Example 3'], + ['1.2e4.2', 0, 'Example 4'], + ['-1.', 1, 'Example 5'], + ['+1E-8', 1, 'Example 6'], + ['.44', 1, 'Example 7'], +]; + +sub valid_number +{ + my $n = shift; + return looks_like_number($n) eq '' ? 0 : 1; +} + +for (@$cases) { + is(valid_number($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; diff --git a/challenge-287/peter-meszaros/tcl/ch-1.tcl b/challenge-287/peter-meszaros/tcl/ch-1.tcl new file mode 100755 index 0000000000..5db1823daf --- /dev/null +++ b/challenge-287/peter-meszaros/tcl/ch-1.tcl @@ -0,0 +1,122 @@ +#!/usr/bin/env tclsh +# +# Task 1: Strong Password +# +# Submitted by: Mohammad Sajid Anwar +# +# 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 +# + +package require tcltest + +set cases { + {a 5 "Example 1"} + {aB2 3 "Example 2"} + {PaaSW0rd 0 "Example 3"} + {Paaasw0rd 1 "Example 4"} + {aaaaa 2 "Example 5"} +} + +proc strong_password {p} { + set cnt 0 + + if {![regexp {[a-z]} $p]} { + if {[regsub {(.)\1{2}} $p {\1\1x} x] == 1} { + set p $x + } else { + append p "x" + } + incr cnt + } + if {![regexp {[A-Z]} $p]} { + if {[regsub {(.)\1{2}} $p {\1\1X} x] == 1} { + set p $x + } else { + append p "X" + } + incr cnt + } + if {![regexp {[0-9]} $p]} { + if {[regsub {(.)\1{2}} $p {\1\10} x] == 1} { + set p $x + } else { + append p "0" + } + incr cnt + } + + while {[regexp {(.)\1{2}} $p _ c]} { + if {$c == "x"} { + set c "y" + } + if {[regsub {(.)\1{2}} $p {\1\1$c} x] == 1} { + set p $x + incr cnt + } + } + + set l [string length $p] + if {$l < 6} { + if {[string index $p end] == "x"} { + set c "y" + } else { + set c "x" + } + set n [expr 6 - $l] + append p string repeat $c $n] + incr cnt $n + } + + return $cnt +} + +tcltest::configure -verbose {pass} +foreach case $cases { + tcltest::test [lindex $case 2] {} { + strong_password [lindex $case 0] + } [lindex $case 1] +} + +exit 0 + diff --git a/challenge-287/peter-meszaros/tcl/ch-2.tcl b/challenge-287/peter-meszaros/tcl/ch-2.tcl new file mode 100755 index 0000000000..b18d5591c2 --- /dev/null +++ b/challenge-287/peter-meszaros/tcl/ch-2.tcl @@ -0,0 +1,90 @@ +#!/usr/bin/env tclsh +# +# +# Task 2: Valid Number +# +# Submitted by: Mohammad Sajid Anwar +# +# 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 +# + +package require tcltest + +set cases { + {1 1 "Example 1"} + {a 0 "Example 2"} + {. 0 "Example 3"} + {1.2e4.2 0 "Example 4"} + {-1. 1 "Example 5"} + {+1E-8 1 "Example 6"} + {.44 1 "Example 7"} +} + +proc valid_number {str} { + return [string is double $str] +} + +tcltest::configure -verbose {pass} +foreach case $cases { + tcltest::test [lindex $case 2] {} { + valid_number [lindex $case 0] + } [lindex $case 1] +} + +exit 0 |
