site.base_url }}/">The Bear's Den -->

The Bear's Den

Enter at your own risk

Common Passwords

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:

Following can be considered as one step:

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: 3

Solution

We may calculate three measures that give the number of steps that have to be performed such that the corresponding criterion is satisfied:

With a single step, more than one measure may be reduced:

The only anomaly here is a string made of a sequence of five repeated characters:

But such a string consists of only one character class and thus two steps are required anyway.

Thus the maximum over all three measures gives the required number of steps in all cases.

use strict;
use warnings;
use experimental 'signatures'
use List::Util qw(reduce max);

sub strong_password ($str) {
    max +(length($str) < 6 ? 6 - length($str) : 0),
         (3 - grep {eval "\$str =~ tr/$_//"} qw(a-z A-Z 0-9)),
         (reduce {$a + int length($b) / 3} 0, $str =~ /((.)\2{2,})/g);
}

See the full solution to task 1.

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:

Decimal Number:

A decimal number is defined with an optional sign - or + followed by one of the following definitions:

Exponent:

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

Solution

This task is what Regex::Common was made for.

use strict;
use warnings;
use Regexp::Common;

sub valid_number {
	shift =~ /^$RE{num}{real}\z/;
}

See the full solution to task 2.


If you have a question about this post or if you like to comment on it, feel free to open an issue in my github repository.