aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDave Cross <dave@dave.org.uk>2021-02-01 16:06:18 +0000
committerDave Cross <dave@dave.org.uk>2021-02-01 16:06:18 +0000
commitd990590ad3e57aebfaf119f17b0f3b989355ad39 (patch)
treef0a264ad7ef20143bcc5dce63f289bf4514c90e8
parentac60bddb13f96402c3026283fe223388ed54fc27 (diff)
downloadperlweeklychallenge-club-d990590ad3e57aebfaf119f17b0f3b989355ad39.tar.gz
perlweeklychallenge-club-d990590ad3e57aebfaf119f17b0f3b989355ad39.tar.bz2
perlweeklychallenge-club-d990590ad3e57aebfaf119f17b0f3b989355ad39.zip
Week #98
-rw-r--r--challenge-098/dave-cross/perl/ch-1.pl21
-rw-r--r--challenge-098/dave-cross/perl/ch-2.pl42
-rw-r--r--challenge-098/dave-cross/perl/input.txt1
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