diff options
| -rw-r--r-- | challenge-308/steven-wilson/perl/ch-1.pl | 28 | ||||
| -rw-r--r-- | challenge-308/steven-wilson/perl/ch-2.pl | 23 | ||||
| -rw-r--r-- | challenge-308/steven-wilson/python/ch-1.py | 20 | ||||
| -rw-r--r-- | challenge-308/steven-wilson/python/ch-2.py | 25 |
4 files changed, 96 insertions, 0 deletions
diff --git a/challenge-308/steven-wilson/perl/ch-1.pl b/challenge-308/steven-wilson/perl/ch-1.pl new file mode 100644 index 0000000000..76bbb72e2f --- /dev/null +++ b/challenge-308/steven-wilson/perl/ch-1.pl @@ -0,0 +1,28 @@ +#!/usr/bin/env perl + +use v5.35; +use Test2::Bundle::More; + +sub countCommon{ + my ($ref1, $ref2) = @_; + my %strings1; + $strings1{$_}++ for (@$ref1); + my %strings2; + $strings2{$_}++ for (@$ref2); + my @intersection = grep { exists $strings1{$_} } keys %strings2; + return scalar @intersection; +} + +my @str1 = ("perl", "weekly", "challenge"); +my @str2 = ("raku", "weekly", "challenge"); +is(countCommon(\@str1, \@str2), 2, "Test 1"); + +my @str3 = ("perl", "raku", "python"); +my @str4 = ("python", "java"); +is(countCommon(\@str3, \@str4), 1, "Test 2"); + +my @str5 = ("guest", "contribution"); +my @str6 = ("fun", "weekly", "challenge"); +is(countCommon(\@str5, \@str6), 0, "Test 3"); + +done_testing(); diff --git a/challenge-308/steven-wilson/perl/ch-2.pl b/challenge-308/steven-wilson/perl/ch-2.pl new file mode 100644 index 0000000000..4390ef8ca0 --- /dev/null +++ b/challenge-308/steven-wilson/perl/ch-2.pl @@ -0,0 +1,23 @@ +#!/usr/bin/env perl + +use v5.35; +use Test2::Bundle::More; + +sub decodeXor{ + my ($initial, $ref_encoded) = @_; + my @decoded; + push @decoded, $initial; + + for my $e (@{$ref_encoded}){ + push @decoded, $decoded[-1] ^ $e; + } + + return \@decoded; +} + +my @encoded1 = (1, 2, 3); +is_deeply(decodeXor(1, \@encoded1), [1, 0, 2, 1], "Test 1"); +my @encoded2 = (6, 2, 7, 3); +is_deeply(decodeXor(4, \@encoded2), [4, 2, 0, 7, 4], "Test 2"); + +done_testing(); 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) |
