aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteven <steven1170@zoho.eu>2025-04-25 18:22:33 +0100
committerSteven <steven1170@zoho.eu>2025-04-25 18:22:33 +0100
commit11441619444cc31399699672fef751f9c956a319 (patch)
treec47fd11c88e75c0c726b91774149d205cd1de8c2
parent24acf2e44e8a396e5b345b26398d8472cc4788fc (diff)
downloadperlweeklychallenge-club-11441619444cc31399699672fef751f9c956a319.tar.gz
perlweeklychallenge-club-11441619444cc31399699672fef751f9c956a319.tar.bz2
perlweeklychallenge-club-11441619444cc31399699672fef751f9c956a319.zip
add solutions week 318 in python
-rw-r--r--challenge-318/steven-wilson/python/ch-1.py25
-rw-r--r--challenge-318/steven-wilson/python/ch-2.py26
2 files changed, 51 insertions, 0 deletions
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)