diff options
| -rw-r--r-- | challenge-098/dave-cross/perl/ch-1.pl | 21 | ||||
| -rw-r--r-- | challenge-098/dave-cross/perl/ch-2.pl | 42 | ||||
| -rw-r--r-- | challenge-098/dave-cross/perl/input.txt | 1 |
3 files changed, 64 insertions, 0 deletions
diff --git a/challenge-098/dave-cross/perl/ch-1.pl b/challenge-098/dave-cross/perl/ch-1.pl new file mode 100644 index 0000000000..b5dc6933da --- /dev/null +++ b/challenge-098/dave-cross/perl/ch-1.pl @@ -0,0 +1,21 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use feature qw[signatures say state]; +no warnings 'experimental::signatures'; + +say readN('input.txt', 4) for 1 .. 3; + +sub readN ($filename, $n) { + state %fh; + + unless ($fh{$filename}) { + open $fh{$filename}, '<', $filename + or die "Cannot open '$filename': $!\n"; + } + + read $fh{$filename}, my ($buf), $n; + + return $buf; +} diff --git a/challenge-098/dave-cross/perl/ch-2.pl b/challenge-098/dave-cross/perl/ch-2.pl new file mode 100644 index 0000000000..2848332d15 --- /dev/null +++ b/challenge-098/dave-cross/perl/ch-2.pl @@ -0,0 +1,42 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use feature qw[say signatures]; +no warnings 'experimental::signatures'; + +use Test::More; + +my @tests = ({ + array => [1, 2, 3, 4], + n => 3, + exp => 2, +}, { + array => [1, 3, 5, 7], + n => 6, + exp => 3, +}, { + array => [12, 14, 16, 18], + n => 10, + exp => 0, +}, { + array => [11, 13, 15, 17], + n => 19, + exp => 4, +}); + +for (@tests) { + is get_index($_->{array}, $_->{n}), $_->{exp}; +} + +done_testing; + +sub get_index { + my ($arr, $n) = @_; + + for (0 .. $#$arr) { + return $_ if $arr->[$_] >= $n; + } + + return scalar @$arr; +} diff --git a/challenge-098/dave-cross/perl/input.txt b/challenge-098/dave-cross/perl/input.txt new file mode 100644 index 0000000000..a32a4347a4 --- /dev/null +++ b/challenge-098/dave-cross/perl/input.txt @@ -0,0 +1 @@ +1234567890 |
