diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-04-26 02:14:19 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-04-26 02:14:19 +0100 |
| commit | 5c8004e8a84d3b46db4dd2c88e8578f1f5c5e9bf (patch) | |
| tree | 92146669b955449905053f40d4fbca968749eca1 | |
| parent | c068431c52e837de4d5b5f156921b05399beb4eb (diff) | |
| parent | 11441619444cc31399699672fef751f9c956a319 (diff) | |
| download | perlweeklychallenge-club-5c8004e8a84d3b46db4dd2c88e8578f1f5c5e9bf.tar.gz perlweeklychallenge-club-5c8004e8a84d3b46db4dd2c88e8578f1f5c5e9bf.tar.bz2 perlweeklychallenge-club-5c8004e8a84d3b46db4dd2c88e8578f1f5c5e9bf.zip | |
Merge pull request #11931 from oWnOIzRi/week318
add solutions week 318 in python
| -rw-r--r-- | challenge-318/steven-wilson/python/ch-1.py | 25 | ||||
| -rw-r--r-- | challenge-318/steven-wilson/python/ch-2.py | 26 |
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) |
