From 07792952c5ec12552fd809d9b796fc8bdb72cfc6 Mon Sep 17 00:00:00 2001 From: Packy Anderson Date: Fri, 25 Apr 2025 01:07:37 -0400 Subject: 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 --- challenge-318/packy-anderson/python/ch-1.py | 38 +++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100755 challenge-318/packy-anderson/python/ch-1.py (limited to 'challenge-318/packy-anderson/python/ch-1.py') 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") -- cgit