diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-02-07 12:04:54 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-02-07 12:04:54 +0000 |
| commit | ab8e34c130808e3dc150dca73c1b8bae9bfe380a (patch) | |
| tree | fae3b9dd00cde0006fddbe5344ad4bfdb77b0612 | |
| parent | 244c14d21a1e84c360a7f4d6e17af8281b7222f1 (diff) | |
| parent | 58d2b0e3c7be66de2261cf0931d5b89285c10724 (diff) | |
| download | perlweeklychallenge-club-ab8e34c130808e3dc150dca73c1b8bae9bfe380a.tar.gz perlweeklychallenge-club-ab8e34c130808e3dc150dca73c1b8bae9bfe380a.tar.bz2 perlweeklychallenge-club-ab8e34c130808e3dc150dca73c1b8bae9bfe380a.zip | |
Merge pull request #3469 from wanderdoc/master
Solutions to challenge-098.
| -rw-r--r-- | challenge-098/wanderdoc/perl/ch-1.pl | 37 | ||||
| -rw-r--r-- | challenge-098/wanderdoc/perl/ch-2.pl | 72 | ||||
| -rw-r--r-- | challenge-098/wanderdoc/perl/input.txt | 1 |
3 files changed, 110 insertions, 0 deletions
diff --git a/challenge-098/wanderdoc/perl/ch-1.pl b/challenge-098/wanderdoc/perl/ch-1.pl new file mode 100644 index 0000000000..ee7ee9b603 --- /dev/null +++ b/challenge-098/wanderdoc/perl/ch-1.pl @@ -0,0 +1,37 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +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. +Example: +Input: Suppose the file (input.txt) contains "1234567890" +Output: + print readN("input.txt", 4); # returns "1234" + print readN("input.txt", 4); # returns "5678" + print readN("input.txt", 4); # returns "90" + +=cut + + + + + + +use FindBin qw($Bin); # Windows. +use IO::File; +use feature 'state'; + + + +sub readN +{ + my ($file, $num) = @_; + state $handle = (IO::File->new("$Bin/$file", "r") or die "$!"); + my $into; + read($handle, $into, $num); + return $into; +} +print readN("input.txt", 4), $/; # 1234 +print readN("input.txt", 4), $/; # 5678 +print readN("input.txt", 4), $/; # 90
\ No newline at end of file diff --git a/challenge-098/wanderdoc/perl/ch-2.pl b/challenge-098/wanderdoc/perl/ch-2.pl new file mode 100644 index 0000000000..47b299d304 --- /dev/null +++ b/challenge-098/wanderdoc/perl/ch-2.pl @@ -0,0 +1,72 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +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. +Example 1: + +Input: @N = (1, 2, 3, 4) and $N = 3 +Output: 2 since the target 3 is in the array at the index 2. + +Example 2: + +Input: @N = (1, 3, 5, 7) and $N = 6 +Output: 3 since the target 6 is missing and should be placed at the index 3. + +Example 3: + +Input: @N = (12, 14, 16, 18) and $N = 10 +Output: 0 since the target 10 is missing and should be placed at the index 0. + +Example 4: + +Input: @N = (11, 13, 15, 17) and $N = 19 +Output: 4 since the target 19 is missing and should be placed at the index 4. + +=cut + +use List::Util qw(first); + +# Actually we have to return index only, thus: + +sub return_idx +{ + my ($aref, $num) = @_; + my %idx; + @idx{@$aref} = keys @$aref; + return $idx{$num} if exists $idx{$num}; + + + my $neighbour = first { $_ > $num } @$aref; + my $should_be_placed = $neighbour ? $idx{$neighbour} : scalar @$aref; + return $should_be_placed; +} + +print return_idx([1, 2, 3, 4], 3), $/; # 2 +print return_idx([1, 3, 5, 7], 6), $/; # 3 + + +print return_idx([12, 14, 16, 18], 10), $/; # 0 +print return_idx([11, 13, 15, 17], 19), $/, $/; # 4 + +# If we do have to modify the array: + +sub search_insert +{ + my ($aref, $num) = @_; + + my %idx; + @idx{@$aref} = keys @$aref; + return $idx{$num} if exists $idx{$num}; + @$aref = sort {$a <=> $b} @$aref, $num; + @idx{@$aref} = keys @$aref; + return $idx{$num}; +} + + +print search_insert([1, 2, 3, 4], 3), $/; # 2 +print search_insert([1, 3, 5, 7], 6), $/; # 3 + +print search_insert([12, 14, 16, 18], 10), $/; # 0 +print search_insert([11, 13, 15, 17], 19), $/; # 4
\ No newline at end of file diff --git a/challenge-098/wanderdoc/perl/input.txt b/challenge-098/wanderdoc/perl/input.txt new file mode 100644 index 0000000000..6a537b5b36 --- /dev/null +++ b/challenge-098/wanderdoc/perl/input.txt @@ -0,0 +1 @@ +1234567890
\ No newline at end of file |
