diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-09-16 20:23:06 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-09-16 20:23:06 +0100 |
| commit | b5756e07fd4d02a1692ef54d693a24a4d85629c9 (patch) | |
| tree | 5bd7d93312aa658330ab8a58ab5c66d221341bfe | |
| parent | 6ba96d45227883a88bec4e91870d341e097ca8d1 (diff) | |
| parent | d59b6127562ef7d0b9e1e9ee8ae5b554ab7618aa (diff) | |
| download | perlweeklychallenge-club-b5756e07fd4d02a1692ef54d693a24a4d85629c9.tar.gz perlweeklychallenge-club-b5756e07fd4d02a1692ef54d693a24a4d85629c9.tar.bz2 perlweeklychallenge-club-b5756e07fd4d02a1692ef54d693a24a4d85629c9.zip | |
Merge pull request #10853 from pauloscustodio/master
Add Perl solutions
| -rw-r--r-- | challenge-287/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-287/paulo-custodio/perl/ch-1.pl | 83 | ||||
| -rw-r--r-- | challenge-287/paulo-custodio/perl/ch-2.pl | 74 | ||||
| -rw-r--r-- | challenge-287/paulo-custodio/t/test-1.yaml | 25 | ||||
| -rw-r--r-- | challenge-287/paulo-custodio/t/test-2.yaml | 35 |
5 files changed, 219 insertions, 0 deletions
diff --git a/challenge-287/paulo-custodio/Makefile b/challenge-287/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-287/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-287/paulo-custodio/perl/ch-1.pl b/challenge-287/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..792483bb33 --- /dev/null +++ b/challenge-287/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,83 @@ +#!/usr/bin/env perl + +# Challenge 287 +# +# 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 + +use Modern::Perl; + +say steps_to_strong(shift // ""); + +sub steps_to_strong { + my($pass) = @_; + + my %avail_chars; + $avail_chars{$_} = 1 for ('0'..'9', 'A'..'Z', 'a'..'z'); + delete $avail_chars{$_} for (split //, $pass); + + my $steps = 0; + while (!is_strong($pass)) { + if ($pass =~ /(.)\1\1+/) { + my $ch = $1; + my $new_ch = (keys %avail_chars)[0]; + $pass =~ s/$ch{3}/$ch$ch$new_ch$ch/; + delete $avail_chars{$new_ch}; + } + else { + my $new_ch = (keys %avail_chars)[0]; + $pass .= $new_ch; + delete $avail_chars{$new_ch}; + } + $steps++; + } + return $steps; +} + +sub is_strong { + local($_) = @_; + return length($_)>=6 && /[a-z]/ && /[A-Z]/ && /[0-9]/ && !/(.)\1\1+/; +} diff --git a/challenge-287/paulo-custodio/perl/ch-2.pl b/challenge-287/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..be73366763 --- /dev/null +++ b/challenge-287/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,74 @@ +#!/usr/bin/env perl + +# Challenge 287 +# +# 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 + +use Modern::Perl; + +$_ = shift // ""; +say /^ [-+]? + (?: \d+ \. \d* | + \d* \. \d+ | + \d+ + ) + (?: e [-+]? \d+ )? + $/ix ? "true" : "false"; diff --git a/challenge-287/paulo-custodio/t/test-1.yaml b/challenge-287/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..f7e17ccbcd --- /dev/null +++ b/challenge-287/paulo-custodio/t/test-1.yaml @@ -0,0 +1,25 @@ +- setup: + cleanup: + args: a + input: + output: 5 +- setup: + cleanup: + args: aB2 + input: + output: 3 +- setup: + cleanup: + args: PaaSW0rd + input: + output: 0 +- setup: + cleanup: + args: Paaasw0rd + input: + output: 1 +- setup: + cleanup: + args: aaaaa + input: + output: 2 diff --git a/challenge-287/paulo-custodio/t/test-2.yaml b/challenge-287/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..c927b16c6e --- /dev/null +++ b/challenge-287/paulo-custodio/t/test-2.yaml @@ -0,0 +1,35 @@ +- setup: + cleanup: + args: 1 + input: + output: true +- setup: + cleanup: + args: a + input: + output: false +- setup: + cleanup: + args: . + input: + output: false +- setup: + cleanup: + args: 1.2e4.2 + input: + output: false +- setup: + cleanup: + args: -1. + input: + output: true +- setup: + cleanup: + args: +1E-8 + input: + output: true +- setup: + cleanup: + args: .44 + input: + output: true |
