From ea3681793dbe32012a6d3ea2824b1f59452958eb Mon Sep 17 00:00:00 2001 From: Packy Anderson Date: Thu, 19 Sep 2024 22:17:57 -0400 Subject: Challenge 287 solutions by Packy Anderson * Raku that maybe looks like Raku, but mostly like Perl * Perl * Python that definitely looks like Perl * Elixir 1 Blog post --- challenge-287/packy-anderson/python/ch-1.py | 87 +++++++++++++++++++++++++++++ challenge-287/packy-anderson/python/ch-2.py | 53 ++++++++++++++++++ 2 files changed, 140 insertions(+) create mode 100755 challenge-287/packy-anderson/python/ch-1.py create mode 100755 challenge-287/packy-anderson/python/ch-2.py (limited to 'challenge-287/packy-anderson/python') diff --git a/challenge-287/packy-anderson/python/ch-1.py b/challenge-287/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..f6f70a80c7 --- /dev/null +++ b/challenge-287/packy-anderson/python/ch-1.py @@ -0,0 +1,87 @@ +#!/usr/bin/env python + +import regex + +def strongPassword(str): + hasUpper = 0 + hasLower = 0 + hasDigit = 0 + runs = [] + + for char in str: + # identify runs of characters + if len(runs) == 0 or runs[-1][-1] != char: + # we have no previous run of characters, or the last + # character of the last run doesn't match this character + runs.append(char) + else: + # append the latest character to the run + runs[-1] += char + + # count the character classes we're interested in + if regex.match(r"\p{Lu}", char): + hasUpper += 1 + elif regex.match(r"\p{Ll}", char): + hasLower += 1 + elif regex.match(r"\p{N}", char): + hasDigit += 1 + + # count how many characters need to be REPLACED + replacements = 0 + for run in runs: + # if the run is 3 or more characters + if len(run) >= 3: + # we need one replacement per multiple of 3 + replacements += int(len(run) / 3) + + # figure out how many changes are needed + changes = 0 + length = len(str) + + for has in [hasUpper, hasLower, hasDigit]: + if has == 0: + changes += 1 # we need to add a character of this type + + # if we have characters that need to be REPLACED, we don't + # need to add to the string length to make the change + if replacements > 0: + replacements -= 1 + else: + # we need to add characters to make the change + length += 1 + + if length < 6: + # not enough characters, we need MORE! + changes += 6 - length + + if replacements > 0: + # if we need more replacements, add them to the total + changes += replacements + + return changes + + +def solution(str): + print(f'Input: $str = "{str}"') + print(f'Output: {strongPassword(str)}') + +print('Example 1:') +solution("a") + +print('\nExample 2:') +solution("aB2") + +print('\nExample 3:') +solution("PaaSW0rd") + +print('\nExample 4:') +solution("Paaasw0rd") + +print('\nExample 5:') +solution("aaaaa") + +print('\nExample 6:') +solution("aaaaaabbbb") + +print('\nExample 7:') +solution("voilÀ३") diff --git a/challenge-287/packy-anderson/python/ch-2.py b/challenge-287/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..7a21d88a20 --- /dev/null +++ b/challenge-287/packy-anderson/python/ch-2.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python + +import re + +sign = r'[+\-]' + +digits = r'\d+' + +integerNumber = fr' {sign}? {digits} ' + +decimalNumber = fr""" + {sign}? (?: {digits}\. | {digits}\.{digits} | \.{digits} ) +""" + +exponent = fr'(?:[eE]{integerNumber})' + +TOP = re.compile(fr""" + ^ (?: + {integerNumber}{exponent}? | + {decimalNumber}{exponent}? + ) $ + """, + re.VERBOSE +) + +def solution(str): + print(f'Input: $str = "{str}"') + match = True if re.search(TOP, str) else False + print(f'Output: {match}') + +print('Example 1:') +solution("1") + +print('\nExample 2:') +solution("a") + +print('\nExample 3:') +solution(".") + +print('\nExample 4:') +solution("1.2e4.2") + +print('\nExample 5:') +solution("-1.") + +print('\nExample 6:') +solution("+1E-8") + +print('\nExample 7:') +solution(".44") + +print('\nExample 8:') +solution("-३.१") -- cgit