diff options
| author | Packy Anderson <packy@cpan.org> | 2024-09-19 22:17:57 -0400 |
|---|---|---|
| committer | Packy Anderson <packy@cpan.org> | 2024-09-19 22:17:57 -0400 |
| commit | ea3681793dbe32012a6d3ea2824b1f59452958eb (patch) | |
| tree | 6c1d1ffbb86bc690effec37e55a388b929821547 /challenge-287/packy-anderson/python/ch-1.py | |
| parent | 6025c6799cf59bf7619b36916a2fcbe7ed4d687b (diff) | |
| download | perlweeklychallenge-club-ea3681793dbe32012a6d3ea2824b1f59452958eb.tar.gz perlweeklychallenge-club-ea3681793dbe32012a6d3ea2824b1f59452958eb.tar.bz2 perlweeklychallenge-club-ea3681793dbe32012a6d3ea2824b1f59452958eb.zip | |
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
Diffstat (limited to 'challenge-287/packy-anderson/python/ch-1.py')
| -rwxr-xr-x | challenge-287/packy-anderson/python/ch-1.py | 87 |
1 files changed, 87 insertions, 0 deletions
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À३") |
