From 993b8be6de0c326497f7cd7d51e1c4f6664fb2a1 Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Mon, 25 Dec 2023 16:45:16 +0100 Subject: Add Python solutions to 249 by E. Choroba --- challenge-249/e-choroba/python/ch-1.py | 20 ++++++++++++++++++++ challenge-249/e-choroba/python/ch-2.py | 23 +++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100755 challenge-249/e-choroba/python/ch-1.py create mode 100755 challenge-249/e-choroba/python/ch-2.py (limited to 'challenge-249/e-choroba/python') 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] -- cgit