aboutsummaryrefslogtreecommitdiff
path: root/challenge-249/e-choroba/python
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-12-25 21:23:27 +0000
committerGitHub <noreply@github.com>2023-12-25 21:23:27 +0000
commit952ef809a502dfd374f143c787ee4a1ae8b9678e (patch)
tree1446b385952adb74db6c06cd1d1c5b969e723d13 /challenge-249/e-choroba/python
parent604839e3f6c93bc0004d5d6e2e31e1eaf9606d76 (diff)
parent993b8be6de0c326497f7cd7d51e1c4f6664fb2a1 (diff)
downloadperlweeklychallenge-club-952ef809a502dfd374f143c787ee4a1ae8b9678e.tar.gz
perlweeklychallenge-club-952ef809a502dfd374f143c787ee4a1ae8b9678e.tar.bz2
perlweeklychallenge-club-952ef809a502dfd374f143c787ee4a1ae8b9678e.zip
Merge pull request #9292 from choroba/ech249
Add solutions to 249: Equal Pairs & DI String Match by E. Choroba
Diffstat (limited to 'challenge-249/e-choroba/python')
-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]