diff options
| author | Fung Cheok Yin <61836418+E7-87-83@users.noreply.github.com> | 2020-05-05 14:46:38 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-05-05 14:46:38 +0800 |
| commit | 41c9c5663f649c712e23c6df760e35d7e4644295 (patch) | |
| tree | cc01f9d8230034f809d5d820714f2ea81777cd9b | |
| parent | ba861d6f8c5c0920f8bfc948c87047d2ad7bd0e9 (diff) | |
| download | perlweeklychallenge-club-41c9c5663f649c712e23c6df760e35d7e4644295.tar.gz perlweeklychallenge-club-41c9c5663f649c712e23c6df760e35d7e4644295.tar.bz2 perlweeklychallenge-club-41c9c5663f649c712e23c6df760e35d7e4644295.zip | |
Add files via upload
| -rw-r--r-- | ch-2.pl | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/ch-2.pl b/ch-2.pl new file mode 100644 index 0000000000..420c20d172 --- /dev/null +++ b/ch-2.pl @@ -0,0 +1,31 @@ +#!/usr/bin/perl +use strict; +use Test::Simple tests => 2; +use List::Util qw/max sum/; + +#reference: https://www.tutorialspoint.com/perl/bitwise_operators_example.htm + + +sub f { # compare two bit-strings by xor + my $hint = ($_[0] ^ $_[1]) ; + my $ans = sum (split //, sprintf("%b", $hint)); + return $ans; +} + +# first parameter: number of terms; remaining: terms in the list +sub conclude { + my ($N, @val) = @_; + my $sum = 0; + for my $i (0..$N-2) { + for my $j ($i+1..$N-1) { + $sum += f($val[$i], $val[$j]); + } + } + return $sum; +} + + +ok (conclude(3,2,3,4) == 6), "bad"; +ok (conclude(2,1,3)==1), "too bad"; + + |
