diff --git a/2-arrays.pl b/2-arrays.pl deleted file mode 100755 index 55fc12d..0000000 --- a/2-arrays.pl +++ /dev/null @@ -1,5 +0,0 @@ -#!/usr/bin/env perl - -use strict; -use warnings; - diff --git a/2-cyclic_rotation.pl b/2-cyclic_rotation.pl new file mode 100755 index 0000000..31fb9f6 --- /dev/null +++ b/2-cyclic_rotation.pl @@ -0,0 +1,39 @@ +#!/usr/bin/env perl + +use Modern::Perl; + +use FindBin; +use lib "$FindBin::RealBin/lib"; +use lib 'lib'; +use CyclicRotation; + + +my $array_ref, $shift_int = @ARGV; +if (scalar(@ARGV) != 2) { + die "Usage: $0 \$array_ref \$integer\n"; +} + +my $answer = CyclicRotation::solution(@ARGV); +print "The rotated array ref is\n"; +print Dumper($answer); + +__END__ + +=pod + +=encoding utf8 + +=head1 NAME + +cyclic_rotation.pl - Given an array reference $A and integer $K rotate the array A a number of times specified by K. + +=head1 SYNOPSIS + +cyclic_rotation.pl $array_ref $integer + +=head1 DESCRIPTION + +This script is used to exercise CyclicRotation.pm. It takes one array ref and one integer as input. It returns the array ref rotated the amount of times indicated by the integer. + + + diff --git a/BinaryGap.md b/BinaryGap.md new file mode 100644 index 0000000..51a9c4b --- /dev/null +++ b/BinaryGap.md @@ -0,0 +1,21 @@ +# BinaryGap + + + +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: + + 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. \ No newline at end of file diff --git a/CyclicRotation.md b/CyclicRotation.md new file mode 100644 index 0000000..0b34c21 --- /dev/null +++ b/CyclicRotation.md @@ -0,0 +1,40 @@ +# CyclicRotation + +An array A consisting of N integers is given. Rotation of the array means that each element is shifted right by one index, and the last element of the array is moved to the first place. For example, the rotation of array A = [3, 8, 9, 7, 6] is [6, 3, 8, 9, 7] (elements are shifted right by one index and 6 is moved to the first place). + +The goal is to rotate array A K times; that is, each element of A will be shifted to the right K times. + +Write a function: + + sub solution { my ($A, $K) = @_; my @A = @$A; ... } + +that, given an array A consisting of N integers and an integer K, returns the array A rotated K times. + +For example, given + A = [3, 8, 9, 7, 6] + K = 3 + +the function should return [9, 7, 6, 3, 8]. Three rotations were made: + [3, 8, 9, 7, 6] -> [6, 3, 8, 9, 7] + [6, 3, 8, 9, 7] -> [7, 6, 3, 8, 9] + [7, 6, 3, 8, 9] -> [9, 7, 6, 3, 8] + +For another example, given + A = [0, 0, 0] + K = 1 + +the function should return [0, 0, 0] + +Given + A = [1, 2, 3, 4] + K = 4 + +the function should return [1, 2, 3, 4] + +Assume that: + + N and K are integers within the range [0..100]; + each element of array A is an integer within the range [−1,000..1,000]. + +In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment. +Copyright 2009–2023 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited. diff --git a/lib/CyclicRotation.pm b/lib/CyclicRotation.pm new file mode 100644 index 0000000..b0e0790 --- /dev/null +++ b/lib/CyclicRotation.pm @@ -0,0 +1,36 @@ +package CyclicRotation; + +use Modern::Perl; + +sub solution { + my ($A, $K) = @_; + + die "You must provide an array reference to rotate" unless defined $A; + die "You must provide an number of rotations" unless defined $K; + my @A = @$A; + + push(@A, splice(@A, 0, $K)); + + + return \@A; +} + +1; + +__END__ + +=pod + +=encoding utf8 + +=head1 NAME + +CyclicRotation - Given an array reference $A and integer $K rotate the array A a number of times specified by K. + +=head1 SYNOPSIS + +cyclic_rotation.pl $array $integer + +=head1 DESCRIPTION + +This module is used to exercise CyclicRotation.pm. It takes one array ref and one integer as input. It returns the array ref rotated the amount of times indicated by the integer. diff --git a/t/02-cyclic_rotation.t b/t/02-cyclic_rotation.t new file mode 100644 index 0000000..7565167 --- /dev/null +++ b/t/02-cyclic_rotation.t @@ -0,0 +1,37 @@ +use Test2::V0; +use Test2::Plugin::ExitSummary; + +use FindBin qw($Bin); +use lib "$Bin/../lib"; +use CyclicRotation; + +my @array1 = qw(1 2 3 4 5 6 7); +my $shift_int1 = '3'; + +my $array2 = [0, 0, 0]; +my $shift_int2 = '1'; + +my $array3 = [1, 2, 3, 4]; +my $shift_int3 = '4'; + +# my $array4 = [3, 8, 9, 7, 6]; +# my $shift_int4 = '3'; + +my $expected_rotated_arr1 = [4, 5, 6, 7, 1, 2, 3]; +my $expected_rotated_arr2 = [0, 0, 0]; +my $expected_rotated_arr3 = [1, 2, 3, 4]; +# my $expected_rotated_arr4 = [4,5,6,7,1,2,3]; + +plan(3); + +my $rotated_arr1 = CyclicRotation::solution(\@array1,$shift_int1); +is($rotated_arr1, $expected_rotated_arr1, "Rotated array 1 is correct"); + +my $rotated_arr2 = CyclicRotation::solution($array2,$shift_int2); +is($rotated_arr2, $expected_rotated_arr2, "Rotated array 2 is correct"); + +my $rotated_arr3 = CyclicRotation::solution($array3, $shift_int3); +is($rotated_arr3, $expected_rotated_arr3, "Rotated array 3 is correct"); + +# my $rotated_arr4 = CyclicRotation::solution(\$array4,$shift_int4); +# is($rotated_arr4, $expected_rotated_arr4, "Rotated array 4 is correct"); \ No newline at end of file