From fe5f04e11db88e9cc57d20d5827396252efc0577 Mon Sep 17 00:00:00 2001 From: Fung Cheok Yin <61836418+E7-87-83@users.noreply.github.com> Date: Tue, 5 May 2020 14:47:23 +0800 Subject: Rename ch-2.pl to challenge-059/cheok-yin-fung/perl/ch-2.pl --- ch-2.pl | 31 ------------------------------- challenge-059/cheok-yin-fung/perl/ch-2.pl | 31 +++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 31 deletions(-) delete mode 100644 ch-2.pl create mode 100644 challenge-059/cheok-yin-fung/perl/ch-2.pl diff --git a/ch-2.pl b/ch-2.pl deleted file mode 100644 index 420c20d172..0000000000 --- a/ch-2.pl +++ /dev/null @@ -1,31 +0,0 @@ -#!/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"; - - diff --git a/challenge-059/cheok-yin-fung/perl/ch-2.pl b/challenge-059/cheok-yin-fung/perl/ch-2.pl new file mode 100644 index 0000000000..420c20d172 --- /dev/null +++ b/challenge-059/cheok-yin-fung/perl/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"; + + -- cgit