aboutsummaryrefslogtreecommitdiff
path: root/challenge-098
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-02-06 10:12:23 +0000
committerGitHub <noreply@github.com>2021-02-06 10:12:23 +0000
commitb93f4cfa4b2ea07d2a4b203ce0d69accd8a1ec7b (patch)
tree69ce05f2c6de28ac484c4869054b26032743ed30 /challenge-098
parent121645271f75ea17ea9c55cf8c87ad08ada308a6 (diff)
parente90e79218d9fb4e396fb90450d11cb5b4ed5a081 (diff)
downloadperlweeklychallenge-club-b93f4cfa4b2ea07d2a4b203ce0d69accd8a1ec7b.tar.gz
perlweeklychallenge-club-b93f4cfa4b2ea07d2a4b203ce0d69accd8a1ec7b.tar.bz2
perlweeklychallenge-club-b93f4cfa4b2ea07d2a4b203ce0d69accd8a1ec7b.zip
Merge pull request #3462 from LubosKolouch/master
Challenge 098 LK Perl
Diffstat (limited to 'challenge-098')
-rw-r--r--challenge-098/lubos-kolouch/perl/ch-1.pl40
-rw-r--r--challenge-098/lubos-kolouch/perl/ch-2.pl55
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;