diff options
| author | Adam Russell <ac.russell@live.com> | 2021-02-06 21:39:13 -0500 |
|---|---|---|
| committer | Adam Russell <ac.russell@live.com> | 2021-02-06 21:39:13 -0500 |
| commit | d62afdede27d500ef383c7466edd92ffa909adca (patch) | |
| tree | b6ba52928f3a8175c151766adfc349ed770c0c06 /challenge-098 | |
| parent | cc8f26071ec9e2c5648b65ce0182d0b8c70b11e3 (diff) | |
| download | perlweeklychallenge-club-d62afdede27d500ef383c7466edd92ffa909adca.tar.gz perlweeklychallenge-club-d62afdede27d500ef383c7466edd92ffa909adca.tar.bz2 perlweeklychallenge-club-d62afdede27d500ef383c7466edd92ffa909adca.zip | |
Perl solutions to challenge 098.
Diffstat (limited to 'challenge-098')
| -rw-r--r-- | challenge-098/adam-russell/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-098/adam-russell/ch-1.dat | 1 | ||||
| -rw-r--r-- | challenge-098/adam-russell/perl/ch-1.pl | 58 | ||||
| -rw-r--r-- | challenge-098/adam-russell/perl/ch-2.pl | 50 |
4 files changed, 110 insertions, 0 deletions
diff --git a/challenge-098/adam-russell/blog.txt b/challenge-098/adam-russell/blog.txt new file mode 100644 index 0000000000..e610453765 --- /dev/null +++ b/challenge-098/adam-russell/blog.txt @@ -0,0 +1 @@ +http://www.rabbitfarm.com/cgi-bin/blosxom/perl/2021/02/06 diff --git a/challenge-098/adam-russell/ch-1.dat b/challenge-098/adam-russell/ch-1.dat new file mode 100644 index 0000000000..a32a4347a4 --- /dev/null +++ b/challenge-098/adam-russell/ch-1.dat @@ -0,0 +1 @@ +1234567890 diff --git a/challenge-098/adam-russell/perl/ch-1.pl b/challenge-098/adam-russell/perl/ch-1.pl new file mode 100644 index 0000000000..9349cba3ee --- /dev/null +++ b/challenge-098/adam-russell/perl/ch-1.pl @@ -0,0 +1,58 @@ +use strict; +use warnings; +## +# You are given file $FILE. +# Create subroutine readN($FILE, $number) returns the +# first n-characters and moves the pointer to the (n+1)th character. +## +sub read_maker0{ + my $n = 0; + return sub{ + my($file, $x) = @_; + my $chars; + open(FILE, $file); + unless(seek(FILE, $n, 0)){ + close(FILE); + } + read(FILE, $chars, $x); + $n = $n + $x; + return $chars; + } +} + +sub read_maker1{ + my ($file) = @_; + my $n = 0; + open(FILE, $file); + return sub{ + my($x) = @_; + my $chars; + my $read = read(FILE, $chars, $x); + $n = $n + $x; + unless(seek(FILE, $n, 0)){ + close(FILE); + } + return $chars; + } +} + +MAIN:{ + my($FILE, $number) = ("ch-1.dat", 4); + my($read_n, $chars); + $read_n = read_maker0(); + $chars = $read_n->($FILE, $number); + print "$chars\n"; + $chars = $read_n->($FILE, $number); + print "$chars\n"; + $chars = $read_n->($FILE, $number); + print "$chars\n"; + + + $read_n = read_maker1($FILE); + $chars = $read_n->($number); + print "$chars\n"; + $chars = $read_n->($number); + print "$chars\n"; + $chars = $read_n->($number); + print "$chars\n"; +}
\ No newline at end of file diff --git a/challenge-098/adam-russell/perl/ch-2.pl b/challenge-098/adam-russell/perl/ch-2.pl new file mode 100644 index 0000000000..fc7969d875 --- /dev/null +++ b/challenge-098/adam-russell/perl/ch-2.pl @@ -0,0 +1,50 @@ +use strict; +use warnings; +## +# You are given a sorted array of distinct integers @N and a target $N. +# Write a script to return the index of the given target if found otherwise +# place the target in the sorted array and return the index. +## +sub find_insert{ + my($list, $n) = @_; + if($n < $list->[0]){ + unshift @{$list}, $n; + return 0; + } + if($n > $list->[@{$list} - 1]){ + push @{$list}, $n; + return @{$list} - 1; + } + for(my $i = 0; $i < (@{$list} - 1); $i++){ + return $i if $n == $list->[$i]; + if($n > $list->[$i] && $n < $list->[$i + 1]){ + splice(@{$list}, $i, 2, ($list->[$i], $n, $list->[$i + 1])); + return $i + 1; + } + } +} + + +MAIN:{ + my(@N, $N, $i); + @N = (1, 2, 3, 4); + $N = 3; + $i = find_insert(\@N, $N); + print "$i\n"; + + @N = (1, 3, 5, 7); + $N = 6; + $i = find_insert(\@N, $N); + print "$i\n"; + + @N = (12, 14, 16, 18); + $N = 10; + $i = find_insert(\@N, $N); + print "$i\n"; + + @N = (11, 13, 15, 17); + $N = 19; + $i = find_insert(\@N, $N); + print "$i\n"; +} + |
