diff options
Diffstat (limited to 'challenge-098')
| -rw-r--r-- | challenge-098/lubos-kolouch/perl/ch-1.pl | 40 | ||||
| -rw-r--r-- | challenge-098/lubos-kolouch/perl/ch-2.pl | 55 |
2 files changed, 95 insertions, 0 deletions
diff --git a/challenge-098/lubos-kolouch/perl/ch-1.pl b/challenge-098/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..52c290a3f2 --- /dev/null +++ b/challenge-098/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,40 @@ +#!/usr/bin/perl +#=============================================================================== +# +# FILE: ch-1.pl +# +# USAGE: ./ch-1.pl +# +# DESCRIPTION: Perl Weekly Challenge 098 +# Read N-characters +# https://www.perlweeklychallenge.org +# +# AUTHOR: Lubos Kolouch +# CREATED: 02/06/2021 10:10:41 AM +#=============================================================================== + +use strict; +use warnings; + +my $offset = 0; + +sub get_n_characters { + my $what = shift; + + open my $file, '<', $what->[0]; + seek $file, $offset, 0; + read $file, my $output, $what->[1]; + $offset += $what->[1]; + + # chomp to fix the problem with end of file + chomp $output; + return $output; +} + +use Test::More; + +is(get_n_characters(['file.txt', 4]), '1234'); +is(get_n_characters(['file.txt', 4]), '5678'); +is(get_n_characters(['file.txt', 4]), '90'); + +done_testing; diff --git a/challenge-098/lubos-kolouch/perl/ch-2.pl b/challenge-098/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..b0d6af5be2 --- /dev/null +++ b/challenge-098/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,55 @@ +#!/usr/bin/perl +#=============================================================================== +# +# FILE: ch-2.pl +# +# USAGE: ./ch-2.pl +# +# DESCRIPTION: Perl Weekly Challenge 098 +# Task 2 - Search Insert Position +# https://www.perlweeklychallenge.org +# +# AUTHOR: Lubos Kolouch +# CREATED: 02/06/2021 10:10:41 AM +#=============================================================================== + +use strict; +use warnings; +use feature qw/say/; + +sub search_position { + my $what = shift; + + my $num = $what->[0]; + my $arr = $what->[1]; + + my $arr_len = scalar(@{$arr}); + my $pos = int($arr_len / 2); + + # handle special cases first + return 0 if $num <= $arr->[0]; + return $arr_len if $num >= $arr->[$arr_len-1]; + + # half the interval until found or jump 1 + my $jump = $pos; + while ($jump > 1) { + return $pos if $arr->[$pos] == $num; + $jump = int($jump / 2); + if ($num > $arr->[$pos]) { + $pos += $jump; + } else { + $pos -= $jump; + } + } + + return $pos; +} +use Test::More; + +is(search_position([3, [1, 2, 3, 4]]), 2); +is(search_position([6, [1, 3, 5, 7]]), 3); +is(search_position([10, [12, 14, 16, 18]]), 0); +is(search_position([19, [11, 13, 15, 17]]), 4); + + +done_testing; |
