aboutsummaryrefslogtreecommitdiff
path: root/challenge-130
diff options
context:
space:
mode:
authordrbaggy <js5@sanger.ac.uk>2021-11-29 07:12:59 +0000
committerdrbaggy <js5@sanger.ac.uk>2021-11-29 07:12:59 +0000
commit5d92143764fb5c8fce90edd16f6938a8470622b3 (patch)
treeeb3645549aa1364f81ea6e314692010f305a1668 /challenge-130
parent2b768566060b5d403f2fd3730171f7087e93018a (diff)
downloadperlweeklychallenge-club-5d92143764fb5c8fce90edd16f6938a8470622b3.tar.gz
perlweeklychallenge-club-5d92143764fb5c8fce90edd16f6938a8470622b3.tar.bz2
perlweeklychallenge-club-5d92143764fb5c8fce90edd16f6938a8470622b3.zip
links in blogs < & >
Diffstat (limited to 'challenge-130')
-rw-r--r--challenge-130/james-smith/perl/ch-1.pl24
1 files changed, 22 insertions, 2 deletions
diff --git a/challenge-130/james-smith/perl/ch-1.pl b/challenge-130/james-smith/perl/ch-1.pl
index a1c4b5280e..eab6f82ddb 100644
--- a/challenge-130/james-smith/perl/ch-1.pl
+++ b/challenge-130/james-smith/perl/ch-1.pl
@@ -13,13 +13,33 @@ my @TESTS = (
[ [1, 2, 3, 4, 3, 2, 1, 4, 4], 4 ],
);
-is( find_odd($_->[0]), $_->[1] ) foreach @TESTS;
+is( find_odd_not($_->[0]), $_->[1] ) foreach @TESTS;
+is( find_odd_xor($_->[0]), $_->[1] ) foreach @TESTS;
+is( find_odd_sum($_->[0]), $_->[1] ) foreach @TESTS;
+
+cmpthese(100_000,{
+ 'sum' => sub { find_odd_sum( $_->[0] ) foreach @TESTS; },
+ 'xor' => sub { find_odd_xor( $_->[0] ) foreach @TESTS; },
+ 'not' => sub { find_odd_not( $_->[0] ) foreach @TESTS; },
+});
done_testing();
-sub find_odd {
+sub find_odd_sum {
+ my %x;
+ $x{$_}++ foreach @{$_[0]};
+ return ( grep { $x{$_} & 1 } keys %x )[0];
+}
+
+sub find_odd_xor {
my %x;
$x{$_}^=1 foreach @{$_[0]};
return ( grep { $x{$_} } keys %x )[0];
}
+sub find_odd_not {
+ my %x;
+ $x{$_} = !$x{$_} foreach @{$_[0]};
+ return ( grep { $x{$_} } keys %x )[0];
+}
+