From bde6bd6403b1d25a37db6133cbb90a47699884c3 Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 21 Nov 2022 09:04:24 +0000 Subject: w192 - Task 1 --- challenge-192/perlboy1967/perl/ch-1.pl | 59 ++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100755 challenge-192/perlboy1967/perl/ch-1.pl diff --git a/challenge-192/perlboy1967/perl/ch-1.pl b/challenge-192/perlboy1967/perl/ch-1.pl new file mode 100755 index 0000000000..106fed0019 --- /dev/null +++ b/challenge-192/perlboy1967/perl/ch-1.pl @@ -0,0 +1,59 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 192 + - https://theweeklychallenge.org/blog/perl-weekly-challenge-192/#TASK1 + +Author: Niels 'PerlBoy' van Dijke + +Task 1: Binary Flip +Submitted by: Mohammad S Anwar + +You are given a positive integer, $n. + +Write a script to find the binary flip. + +=cut + +use v5.16; +use warnings; + +use Test::More; +use Benchmark qw(:all); + + +sub binaryFlipString ($) { + my $n = sprintf('%b',$_[0]); + $n =~ tr/01/10/; + return oct('0b'.$n); +} + +sub binaryFlipBinary ($) { + my ($i,$m) = ($_[0],0); + # Create bitmask for 'AND' below + while ($i) { + $i >>= 1; $m = ($m << 1) + 1; + } + return ~$_[0] & $m; +} + + +is(binaryFlipString(5),2); +is(binaryFlipString(5),2); +is(binaryFlipString(4),3); +is(binaryFlipString(6),1); +is(binaryFlipString(0b10101),0b1010); + +is(binaryFlipBinary(5),2); +is(binaryFlipBinary(5),2); +is(binaryFlipBinary(4),3); +is(binaryFlipBinary(6),1); +is(binaryFlipBinary(0b10101),0b1010); + +done_testing; + +cmpthese(1_000_000, { + 'Binary' => sub{binaryFlipBinary(12345678)}, + 'String' => sub{binaryFlipString(12345678)}, +}); -- cgit From 93e16aedac1f6a51b5227a3510ef3cb125b2ff75 Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 21 Nov 2022 12:26:47 +0000 Subject: Turn binaryFlipString into a one-liner subroutine --- challenge-192/perlboy1967/perl/ch-1.pl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/challenge-192/perlboy1967/perl/ch-1.pl b/challenge-192/perlboy1967/perl/ch-1.pl index 106fed0019..f3c1f5abbf 100755 --- a/challenge-192/perlboy1967/perl/ch-1.pl +++ b/challenge-192/perlboy1967/perl/ch-1.pl @@ -24,17 +24,17 @@ use Benchmark qw(:all); sub binaryFlipString ($) { - my $n = sprintf('%b',$_[0]); - $n =~ tr/01/10/; - return oct('0b'.$n); + return oct('0b'.sprintf('%b',$_[0]) =~ tr/01/10/r); } sub binaryFlipBinary ($) { my ($i,$m) = ($_[0],0); + # Create bitmask for 'AND' below while ($i) { $i >>= 1; $m = ($m << 1) + 1; } + return ~$_[0] & $m; } -- cgit From 9d1c9ef1077c6d54721eb76604d310522439c97f Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 21 Nov 2022 12:40:00 +0000 Subject: Add Inline 'C' implemenaion (FAST!) --- challenge-192/perlboy1967/perl/ch-1.pl | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/challenge-192/perlboy1967/perl/ch-1.pl b/challenge-192/perlboy1967/perl/ch-1.pl index f3c1f5abbf..f40a39c3a1 100755 --- a/challenge-192/perlboy1967/perl/ch-1.pl +++ b/challenge-192/perlboy1967/perl/ch-1.pl @@ -21,6 +21,7 @@ use warnings; use Test::More; use Benchmark qw(:all); +use Inline 'C'; sub binaryFlipString ($) { @@ -51,9 +52,31 @@ is(binaryFlipBinary(4),3); is(binaryFlipBinary(6),1); is(binaryFlipBinary(0b10101),0b1010); +is(binaryFlipC(5),2); +is(binaryFlipC(5),2); +is(binaryFlipC(4),3); +is(binaryFlipC(6),1); +is(binaryFlipC(0b10101),0b1010); done_testing; -cmpthese(1_000_000, { +cmpthese(5_000_000, { 'Binary' => sub{binaryFlipBinary(12345678)}, + 'BinaryC' => sub{binaryFlipC(12345678)}, 'String' => sub{binaryFlipString(12345678)}, }); + +__END__ +__C__ + +int binaryFlipC (int i) { + int j = i; + int m = 0; + + while (j) { + j >>= 1; + m <<= 1; + m++; + } + + return ~i & m; +} -- cgit From 304dabde4db121ff97d6090e537b96d892d51294 Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 21 Nov 2022 12:53:48 +0000 Subject: Edit to make Perl look like C source implementation --- challenge-192/perlboy1967/perl/ch-1.pl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/challenge-192/perlboy1967/perl/ch-1.pl b/challenge-192/perlboy1967/perl/ch-1.pl index f40a39c3a1..fb4e36b839 100755 --- a/challenge-192/perlboy1967/perl/ch-1.pl +++ b/challenge-192/perlboy1967/perl/ch-1.pl @@ -31,9 +31,11 @@ sub binaryFlipString ($) { sub binaryFlipBinary ($) { my ($i,$m) = ($_[0],0); - # Create bitmask for 'AND' below + # Create bitmask for 'AND' in 'return' while ($i) { - $i >>= 1; $m = ($m << 1) + 1; + $i >>= 1; + $m <<= 1; + $m++; } return ~$_[0] & $m; -- cgit