aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-02-02 07:33:47 +0000
committerGitHub <noreply@github.com>2021-02-02 07:33:47 +0000
commit327d15f5021d6f3a1d5792ae629ec748823d9ec4 (patch)
tree4f69573f92214bdf85479ca6d88e8bfe4a17fda0
parentac60bddb13f96402c3026283fe223388ed54fc27 (diff)
parent8ec4565fe8979eebefd0e3932d4b2e2c73b87dda (diff)
downloadperlweeklychallenge-club-327d15f5021d6f3a1d5792ae629ec748823d9ec4.tar.gz
perlweeklychallenge-club-327d15f5021d6f3a1d5792ae629ec748823d9ec4.tar.bz2
perlweeklychallenge-club-327d15f5021d6f3a1d5792ae629ec748823d9ec4.zip
Merge pull request #3435 from drbaggy/master
first pass at code - with annotations
-rw-r--r--challenge-098/james-smith/perl/bob.txt1
-rw-r--r--challenge-098/james-smith/perl/ch-1.pl62
-rw-r--r--challenge-098/james-smith/perl/ch-2.pl32
-rw-r--r--challenge-098/james-smith/perl/input.txt1
4 files changed, 96 insertions, 0 deletions
diff --git a/challenge-098/james-smith/perl/bob.txt b/challenge-098/james-smith/perl/bob.txt
new file mode 100644
index 0000000000..70e6a83d8d
--- /dev/null
+++ b/challenge-098/james-smith/perl/bob.txt
@@ -0,0 +1 @@
+abcdefghijklmnop \ No newline at end of file
diff --git a/challenge-098/james-smith/perl/ch-1.pl b/challenge-098/james-smith/perl/ch-1.pl
new file mode 100644
index 0000000000..2d12b507cc
--- /dev/null
+++ b/challenge-098/james-smith/perl/ch-1.pl
@@ -0,0 +1,62 @@
+#!/usr/local/bin/perl
+
+use strict;
+
+use warnings;
+use feature qw(say);
+use Test::More;
+
+my %handles;
+
+is( readN( 'bob.txt', 4 ), 'abcd' );
+is( readN( 'input.txt', 4 ), '1234' );
+is( readN( 'bob.txt', 4 ), 'efgh' );
+is( readN( 'input.txt', 4 ), '5678' );
+is( readN( 'bob.txt', 4 ), 'ijkl' );
+is( readN( 'bob.txt', 4 ), 'mnop' );
+is( readN( 'input.txt', 4 ), '90' );
+is( readN( 'bob.txt', 4 ), '' );
+is( readN( 'input.txt', 4 ), '' );
+is( readN( 'bob.txt', 4 ), '' );
+is( readN( 'bob.txt', 4 ), '' );
+
+## Ignore this bit checking clean up works!
+done_testing();
+ print "Open @{[ show_open() ]}\n";
+my $t = $handles{'bob.txt'};
+cleanup('fred.txt');
+cleanup('bob.txt'); print "Open @{[ show_open() ]}\n";
+<$t>; ## This will warn - prove that file handle has been closed
+cleanup(); print "Open @{[ show_open() ]}\n";
+## End of bit to ignore..
+
+sub readN {
+ my( $fn, $bytes ) = @_;
+
+ ## Create a file handle if we don't already have one
+ unless( exists $handles{$fn} ) {
+ open $handles{$fn}, '<', $fn;
+ }
+
+ ## Create a buffer for the return value
+ my $t = '';
+
+ ## Use "read" to read the $bytes bytes - these are put into 2nd parameter
+ ## If read returns undef it means it has reached the end of the file...
+ warn "Reached end of file $fn\n" unless read ${$handles{$fn}}, $t, $bytes;
+
+ ## Return string
+ return $t;
+}
+
+sub cleanup {
+ ## For neatness close all handles
+ ## delete returns the value of the has deleted
+ ## if filenames are passed then only those are cleaned up
+ close delete $handles{$_} foreach @_ ? grep { exists $handles{$_} } @_ : keys %handles;
+}
+
+sub show_open {
+ ## Return a list of open filenames
+ return keys %handles;
+}
diff --git a/challenge-098/james-smith/perl/ch-2.pl b/challenge-098/james-smith/perl/ch-2.pl
new file mode 100644
index 0000000000..7e2867ade6
--- /dev/null
+++ b/challenge-098/james-smith/perl/ch-2.pl
@@ -0,0 +1,32 @@
+#!/usr/local/bin/perl
+
+use strict;
+
+use warnings;
+use feature qw(say);
+use Test::More;
+
+is( insert_pos( [qw( 1 2 3 4)], 3), 2 );
+is( insert_pos( [qw( 1 3 5 7)], 6), 3 );
+is( insert_pos( [qw(12 14 16 18)], 10), 0 );
+is( insert_pos( [qw(11 13 15 17)], 19), 4 );
+
+done_testing();
+
+sub insert_pos {
+ my( $t, $l, $val ) = (0,@_);
+
+ ## Repeat unless we have got to end of list or the new entry is greater than val
+ $t++ while $t < @{$l} && $l->[$t] < $val;
+
+ ## If we are after the end of the list (to avoid warning) OR
+ ## If we haven't found the entry then we use splice to insert it
+ splice @{$l},$t,0,$val if $t == @{$l} || $l->[$t] != $val;
+
+ ## Warn to show splice has worked...
+ warn ">> $t ( @{$l} )\n"; ## Demonstrate splice
+
+ ## Return the index of the number!
+ return $t;
+}
+
diff --git a/challenge-098/james-smith/perl/input.txt b/challenge-098/james-smith/perl/input.txt
new file mode 100644
index 0000000000..6a537b5b36
--- /dev/null
+++ b/challenge-098/james-smith/perl/input.txt
@@ -0,0 +1 @@
+1234567890 \ No newline at end of file