From 4e45c44c570fd4a77a5a76236bb763d4d9b859ec Mon Sep 17 00:00:00 2001 From: Steven Date: Tue, 11 Feb 2025 16:31:46 +0000 Subject: add solutions week 308 in python & perl --- challenge-308/steven-wilson/python/ch-1.py | 20 ++++++++++++++++++++ challenge-308/steven-wilson/python/ch-2.py | 25 +++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 challenge-308/steven-wilson/python/ch-1.py create mode 100644 challenge-308/steven-wilson/python/ch-2.py (limited to 'challenge-308/steven-wilson/python') diff --git a/challenge-308/steven-wilson/python/ch-1.py b/challenge-308/steven-wilson/python/ch-1.py new file mode 100644 index 0000000000..64274dd5cc --- /dev/null +++ b/challenge-308/steven-wilson/python/ch-1.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python3 + + +def count_common(strings1, strings2): + """ Given two array of strings, return the count of common strings in both + arrays. + >>> count_common(("perl", "weekly", "challenge"), ("raku", "weekly", "challenge")) + 2 + >>> count_common(("perl", "raku", "python"), ("python", "java")) + 1 + >>> count_common(("guest", "contribution"), ("fun", "weekly", "challenge")) + 0 + """ + return len(set(strings1).intersection(strings2)) + + +if __name__ == "__main__": + import doctest + + doctest.testmod(verbose=True) diff --git a/challenge-308/steven-wilson/python/ch-2.py b/challenge-308/steven-wilson/python/ch-2.py new file mode 100644 index 0000000000..285f6dd2d7 --- /dev/null +++ b/challenge-308/steven-wilson/python/ch-2.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python3 + + +def decode_xor(initial, encoded): + """ Given an encoded array and an initial integer, find the original array + that produced the given encoded array. It was encoded such that + encoded[i] = orig[i] XOR orig[i + 1]. + + >>> decode_xor(1, (1, 2, 3)) + (1, 0, 2, 1) + >>> decode_xor(4, (6, 2, 7, 3)) + (4, 2, 0, 7, 4) + """ + decoded = [initial] + + for e in encoded: + decoded.append(decoded[-1] ^ e) + + return tuple(decoded) + + +if __name__ == "__main__": + import doctest + + doctest.testmod(verbose=True) -- cgit