Robbie Hatley's Solutions To The Weekly Challenge #287

For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle with two parts, cycling every Sunday. You can find it here:

The Weekly Challenge

The Weekly Challenge for the week of 2024-09-15 through 2024-09-21 is #287. Its tasks are as follows:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 287-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 a
"strong password". If the string is already a "strong password",
then return 0.

The definition of "strong password" is as follows:
- It must have at least 6 characters.
- It must contain at least one lowercase letter
- It must contain at least one uppercase letter
- It must contain at least one digit
- It mustn't contain 3 repeating characters in a row

Each of the following is considered 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

I solved this problem by writing subs to do these two things:

  • Determine the least-abundant type of character in a given string.
  • Make a strong password out of any string.

That second sub involves doing the following:

  1. For each identical triplet, insert a character of least-abundant type between 2nd & 3rd chars of triplet, making sure that it doesn't match what's to its left or right.
  2. While we don't have all required types, tack a least-abundant character to the end, making sure that it doesn't match the character to its left.
  3. While length < 6, tack a symbol to the end, making sure that it doesn't match the character to its left.
  4. Increment a step counter each time we insert a character.
  5. Return strong password and number of steps required to strengthen it.

Robbie Hatley's Perl Solution to The Weekly Challenge 287-1

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 287-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 as an optional sign '-' or '+'
followed by digits.

A "decimal number" is defined as an optional sign '-' or '+'
followed by one of the following:
- Digits followed by a dot '.'.
- Digits followed by a dot '.' followed by digits.
- A dot '.' followed by digits.

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

To solve this problem, I made regular expressions for "integer", "decimal", and "exponent", then tacked them together to make a regular expression for "number", then just tested inputs against that regular expression.

Robbie Hatley's Perl Solution to The Weekly Challenge 287-2

That's it for challenge 287; see you on challenge 288!

Comments

Popular posts from this blog

Robbie Hatley's Solutions To The Weekly Challenge #262

Robbie Hatley's Solutions To The Weekly Challenge #266

Robbie Hatley's Solutions To The Weekly Challenge #273