diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-02-02 08:05:05 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-02-02 08:05:05 +0000 |
| commit | 1133178e435e5c6ed2df46557637d5f102c4ee54 (patch) | |
| tree | bde6985fc27c05cef8fb8ac4d767a0bb74b939f6 | |
| parent | d618bfb8b07e25b4604bf0816727e76950175ec4 (diff) | |
| parent | d990590ad3e57aebfaf119f17b0f3b989355ad39 (diff) | |
| download | perlweeklychallenge-club-1133178e435e5c6ed2df46557637d5f102c4ee54.tar.gz perlweeklychallenge-club-1133178e435e5c6ed2df46557637d5f102c4ee54.tar.bz2 perlweeklychallenge-club-1133178e435e5c6ed2df46557637d5f102c4ee54.zip | |
Merge pull request #3442 from davorg/master
Week #98
| -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 |
