diff options
| author | Packy Anderson <packy@cpan.org> | 2025-04-25 01:07:37 -0400 |
|---|---|---|
| committer | Packy Anderson <packy@cpan.org> | 2025-04-25 01:07:37 -0400 |
| commit | 07792952c5ec12552fd809d9b796fc8bdb72cfc6 (patch) | |
| tree | fd0f9584738f3ef2d5e9f4a76f93f741fd64f7a4 /challenge-318/packy-anderson/python | |
| parent | 1a3965d948a3400778249c2c33f063b06473fcd3 (diff) | |
| download | perlweeklychallenge-club-07792952c5ec12552fd809d9b796fc8bdb72cfc6.tar.gz perlweeklychallenge-club-07792952c5ec12552fd809d9b796fc8bdb72cfc6.tar.bz2 perlweeklychallenge-club-07792952c5ec12552fd809d9b796fc8bdb72cfc6.zip | |
Challenge 318 solutions by Packy Anderson
* Raku that maybe looks like Raku, but mostly like Perl
* Perl
* Python that definitely looks like Perl
* Elixir that looks kinda like Elixir
1 Blog post
Diffstat (limited to 'challenge-318/packy-anderson/python')
| -rwxr-xr-x | challenge-318/packy-anderson/python/ch-1.py | 38 | ||||
| -rwxr-xr-x | challenge-318/packy-anderson/python/ch-2.py | 45 |
2 files changed, 83 insertions, 0 deletions
diff --git a/challenge-318/packy-anderson/python/ch-1.py b/challenge-318/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..966e662963 --- /dev/null +++ b/challenge-318/packy-anderson/python/ch-1.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python + +import re + +def groupPosition(str): + # python doesn't support nested groups, + # so let's match just the first occurence + hasGroups = re.compile(r"([a-z])\1\1+", re.I) + groups = [] + + match = hasGroups.search(str) + while match: + # push the occurrence onto a list of groups + # the 0th group is the entire match + groups.append(match.group(0)) + # then remove from the start of the string to + # the end of the first occurence + str = str[match.end():] + # and match against the new string + match = hasGroups.search(str) + + if groups: + return ', '.join([ f'"{s}"' for s in groups ]) + else: + return '""' + +def solution(str): + print(f'Input: $str = "{str}"') + print(f'Output: { groupPosition(str) }') + +print('Example 1:') +solution("abccccd") + +print('\nExample 2:') +solution("aaabcddddeefff") + +print('\nExample 3:') +solution("abcdd") diff --git a/challenge-318/packy-anderson/python/ch-2.py b/challenge-318/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..2c129b2aec --- /dev/null +++ b/challenge-318/packy-anderson/python/ch-2.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python + +def reverseEquals(source, target): + # deal with the trivial cases + if len(source) != len(target): + # @source and @target are different lengths + return (False, "") + + if source == target: + # they're already the same + return (True, "") + + # ok, now we start checking subarrays + for i in range(len(source)-1): + for j in range(i+1, len(source)): + rev = [] + if i > 0: + rev = source[0:i] + rev.extend(list(reversed(source[i:j+1]))) + if j < len(source)-1: + rev.extend(source[j+1:]) + if rev == target: + return (True, f'{i}-{j}') + + return (False, "") + +def comma_join(arr): + return ', '.join(map(lambda i: str(i), arr)) + +def solution(source, target): + print(f'Input: @source = ({comma_join(source)})') + print(f' @target = ({comma_join(target)})') + (equals, elements) = reverseEquals(source, target) + print(f'Output: {equals}') + if elements: + print(f'\nReverse elements: {elements}') + +print('Example 1:') +solution([3, 2, 1, 4], [1, 2, 3, 4]) + +print('\nExample 2:') +solution([1, 3, 4], [4, 1, 3]) + +print('\nExample 3:') +solution([2], [2]) |
