aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorE. Choroba <choroba@matfyz.cz>2023-12-25 16:45:16 +0100
committerE. Choroba <choroba@matfyz.cz>2023-12-25 16:45:16 +0100
commit993b8be6de0c326497f7cd7d51e1c4f6664fb2a1 (patch)
tree9073df5660181b57b99529a411235ad176357989
parent25544ece2cc3a152d5e91b0971fec10538b72b56 (diff)
downloadperlweeklychallenge-club-993b8be6de0c326497f7cd7d51e1c4f6664fb2a1.tar.gz
perlweeklychallenge-club-993b8be6de0c326497f7cd7d51e1c4f6664fb2a1.tar.bz2
perlweeklychallenge-club-993b8be6de0c326497f7cd7d51e1c4f6664fb2a1.zip
Add Python solutions to 249 by E. Choroba
-rwxr-xr-xchallenge-249/e-choroba/python/ch-1.py20
-rwxr-xr-xchallenge-249/e-choroba/python/ch-2.py23
2 files changed, 43 insertions, 0 deletions
diff --git a/challenge-249/e-choroba/python/ch-1.py b/challenge-249/e-choroba/python/ch-1.py
new file mode 100755
index 0000000000..8804c18a26
--- /dev/null
+++ b/challenge-249/e-choroba/python/ch-1.py
@@ -0,0 +1,20 @@
+#! /usr/bin/python3
+from collections import Counter
+
+def equal_pairs(*ints):
+ seen = Counter(ints)
+ pairs = []
+ for c in seen:
+ if seen[c] % 2 == 1:
+ return []
+ else:
+ pairs += map(lambda _i: [c, c], range(seen[c] // 2))
+ return pairs
+
+assert equal_pairs(3, 2, 3, 2, 2, 2) == [[3, 3], [2, 2], [2, 2]], 'Example 1'
+assert equal_pairs(1, 2, 3, 4) == [], 'Example 2'
+assert equal_pairs(-1, -1, -2, -2) == [[-1, -1], [-2, -2]], 'Negative numbers'
+assert equal_pairs(1, 1, 1, 1, 2, 2, 2, 2) == [
+ [1, 1], [1, 1], [2, 2], [2, 2]], \
+ 'More than once'
+assert equal_pairs(1, 1, 1, 1, 2, 2, 2, 2, 1) == [], 'More than once odd';
diff --git a/challenge-249/e-choroba/python/ch-2.py b/challenge-249/e-choroba/python/ch-2.py
new file mode 100755
index 0000000000..9cff6bee20
--- /dev/null
+++ b/challenge-249/e-choroba/python/ch-2.py
@@ -0,0 +1,23 @@
+#! /usr/bin/python3
+class DiStringMatch:
+ def __init__(self):
+ self.p = [0]
+ self.mx = 1
+
+ def process(self, s):
+ def inc():
+ self.p.append(self.mx)
+ self.mx += 1
+
+ def dec():
+ self.p = [x + 1 for x in self.p]
+ self.mx += 1
+ self.p.append(0)
+
+ for ch in s:
+ inc() if ch == 'I' else dec()
+ return self.p
+
+assert DiStringMatch().process('IDID') == [2, 3, 1, 4, 0]
+assert DiStringMatch().process('III') == [0, 1, 2, 3]
+assert DiStringMatch().process('DDI') == [2, 1, 0, 3]