commit 8f07d8849680d30a57773a2fa31b3d898566c8be Author: Tracey Clark Date: Wed Sep 20 21:50:08 2023 -0500 Initial commit with BinaryGap solution diff --git a/1-BinaryGap.md b/1-BinaryGap.md new file mode 100644 index 0000000..8655be9 --- /dev/null +++ b/1-BinaryGap.md @@ -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 2009–2023 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited. + +## Advice + +https://codingwithmanny.medium.com/how-to-solve-binary-gap-cda3c3e980b8 + diff --git a/1-binary_gap.pl b/1-binary_gap.pl new file mode 100755 index 0000000..3b51bac --- /dev/null +++ b/1-binary_gap.pl @@ -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; \ No newline at end of file diff --git a/2-arrays.pl b/2-arrays.pl new file mode 100755 index 0000000..55fc12d --- /dev/null +++ b/2-arrays.pl @@ -0,0 +1,5 @@ +#!/usr/bin/env perl + +use strict; +use warnings; + diff --git a/README_SCORES.md b/README_SCORES.md new file mode 100644 index 0000000..8b95eec --- /dev/null +++ b/README_SCORES.md @@ -0,0 +1,6 @@ +# Scores for codility tests + +1. [BinaryGap](https://app.codility.com/demo/results/trainingCYCZA3-GQA/) +2. [CyclicRotation] + [OddOccurrencesInArray] +3. \ No newline at end of file diff --git a/lib/BinaryGap.pm b/lib/BinaryGap.pm new file mode 100644 index 0000000..2dcb118 --- /dev/null +++ b/lib/BinaryGap.pm @@ -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; \ No newline at end of file diff --git a/t/01-binary_gap.t b/t/01-binary_gap.t new file mode 100644 index 0000000..4c8eae6 --- /dev/null +++ b/t/01-binary_gap.t @@ -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"); +} \ No newline at end of file