From 11441619444cc31399699672fef751f9c956a319 Mon Sep 17 00:00:00 2001 From: Steven Date: Fri, 25 Apr 2025 18:22:33 +0100 Subject: add solutions week 318 in python --- challenge-318/steven-wilson/python/ch-1.py | 25 +++++++++++++++++++++++++ challenge-318/steven-wilson/python/ch-2.py | 26 ++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 challenge-318/steven-wilson/python/ch-1.py create mode 100644 challenge-318/steven-wilson/python/ch-2.py diff --git a/challenge-318/steven-wilson/python/ch-1.py b/challenge-318/steven-wilson/python/ch-1.py new file mode 100644 index 0000000000..078a5146cf --- /dev/null +++ b/challenge-318/steven-wilson/python/ch-1.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python3 + +from itertools import groupby + + +def group_position(string): + """ Given a string of lowercase letters, find the position of all groups in + the given string. Three or more consecutive letters form a group. Return "” + if none found. + + >>> group_position("abccccd") + ('cccc',) + >>> group_position("aaabcddddeefff") + ('aaa', 'dddd', 'fff') + >>> group_position("abcdd") + () + """ + return tuple("".join(g) for g in (list(g) for _, g in + groupby(string, key=None)) if len(g) >= 3) + + +if __name__ == "__main__": + import doctest + + doctest.testmod(verbose=True) diff --git a/challenge-318/steven-wilson/python/ch-2.py b/challenge-318/steven-wilson/python/ch-2.py new file mode 100644 index 0000000000..9848edcac5 --- /dev/null +++ b/challenge-318/steven-wilson/python/ch-2.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 + + +def reverse_equals(source, target): + """ Given two arrays of integers, each containing the same elements as the + other, return true if one array can be made to equal the other by reversing + exactly one contiguous subarray. + + >>> reverse_equals([3, 2, 1, 4], [1, 2, 3, 4]) + True + >>> reverse_equals([1, 3, 4], [4, 1, 3]) + False + >>> reverse_equals([2], [2]) + True + """ + if source == target: + return True + + (first, *middle, last) = [n for n, i in enumerate(source) if i != target[n]] + return source[first:last+1][::-1] == target[first:last+1] + + +if __name__ == "__main__": + import doctest + + doctest.testmod(verbose=True) -- cgit