Initial commit with BinaryGap solution

This commit is contained in:
Tracey Clark 2023-09-20 21:50:08 -05:00
commit 8f07d88496
6 changed files with 150 additions and 0 deletions

26
1-BinaryGap.md Normal file
View file

@ -0,0 +1,26 @@
# Binary Gap
A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of N.
For example, number 9 has binary representation 1001 and contains a binary gap of length 2. The number 529 has binary representation 1000010001 and contains two binary gaps: one of length 4 and one of length 3. The number 20 has binary representation 10100 and contains one binary gap of length 1. The number 15 has binary representation 1111 and has no binary gaps. The number 32 has binary representation 100000 and has no binary gaps.
Write a function:
```perl
sub solution { my ($N) = @_; ... }
```
that, given a positive integer N, returns the length of its longest binary gap. The function should return 0 if N doesn't contain a binary gap.
For example, given N = 1041 the function should return 5, because N has binary representation 10000010001 and so its longest binary gap is of length 5. Given N = 32 the function should return 0, because N has binary representation '100000' and thus no binary gaps.
Write an efficient algorithm for the following assumptions:
N is an integer within the range [1..2,147,483,647].
Copyright 20092023 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
## Advice
https://codingwithmanny.medium.com/how-to-solve-binary-gap-cda3c3e980b8

19
1-binary_gap.pl Executable file
View file

@ -0,0 +1,19 @@
#!/usr/bin/env perl
use strict;
use warnings;
use FindBin;
use lib "$FindBin::RealBin/lib";
use lib 'lib';
use BinaryGap;
use Data::Dumper;
print "Input: " . Dumper($ARGV[0]);
my $number = $ARGV[0];
my $answer = BinaryGap::solution($number);
print "Got answer $answer\n";
exit;

5
2-arrays.pl Executable file
View file

@ -0,0 +1,5 @@
#!/usr/bin/env perl
use strict;
use warnings;

6
README_SCORES.md Normal file
View file

@ -0,0 +1,6 @@
# Scores for codility tests
1. [BinaryGap](https://app.codility.com/demo/results/trainingCYCZA3-GQA/)
2. [CyclicRotation]
[OddOccurrencesInArray]
3.

64
lib/BinaryGap.pm Normal file
View file

@ -0,0 +1,64 @@
package BinaryGap;
use Modern::Perl;
# use List::MoreUtils qw(first_index); # codility does not have
use List::Util qw(max first);
use Data::Dumper;
sub solution {
# N is an integer within the range [1..2,147,483,647]
my ($N) = @_;
my $max_gap = 0;
# Make sure number is in acceptable range, if not return 0
if (($N < 1) || ($N > '2147483647')) {
return $max_gap;
}
# convert integer to binary
my $bin_num = unpack("B*", pack("N", $N));
# convert to array
my @bin_array = split('',$bin_num);
# find first 1. If there is not one, return for performance
my $first_index;
for (0 .. $#bin_array) {
if ($bin_array[$_] == 1) {
$first_index = $_;
last;
}
}
return $max_gap unless $first_index;
# If there are less than two 1s in the array, return 0
my $ones_total_count = grep { $_ == 1 } @bin_array;
if ($ones_total_count < 2) {
return $max_gap;
}
# If there is just one 0 in the array, return 0
my $zeros_total_count = grep { $_ == 0 } @bin_array;
if ($zeros_total_count < 2) {
return $max_gap;
}
# Get all 1 indexes
my @i = grep { $bin_array[$_] == 1 } 0 .. $#bin_array;
my $last_index = $i[$#i];
my $zero_count = 0;
for my $index ($first_index .. $last_index) {
my $num = $bin_array[$index];
if ($num == 1) {
$zero_count = 0;
}
else {
$zero_count++;
$max_gap = max($zero_count, $max_gap);
}
}
return $max_gap;
}
1;

30
t/01-binary_gap.t Normal file
View file

@ -0,0 +1,30 @@
use Test2::V0;
use Test2::Plugin::ExitSummary;
use FindBin qw($Bin);
use lib "$Bin/../lib";
use BinaryGap;
# plan(2);
my %test_data = (
1 => '0',
9 => '2',
529 => '4',
20 => '1',
15 => '0',
32 => '0',
1041 => '5',
6 => '0',
328 => '2',
51712 => '2',
20 => '1',
);
plan(scalar(keys %test_data));
for my $key (keys %test_data) {
my $binary_gap = BinaryGap::solution($key);
is($binary_gap, $test_data{$key}, "Binary gap for $key is correct");
}