aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-05-04 13:20:02 +0200
committerAbigail <abigail@abigail.be>2021-05-04 13:20:02 +0200
commitc8bdf59902b53b33005f98d3538fcf57329bf7c8 (patch)
treedac2fe4b702bbc38012a935cf41d018ce261da26
parent57ec29ba5148345cc41835591e77f567d70aa348 (diff)
downloadperlweeklychallenge-club-c8bdf59902b53b33005f98d3538fcf57329bf7c8.tar.gz
perlweeklychallenge-club-c8bdf59902b53b33005f98d3538fcf57329bf7c8.tar.bz2
perlweeklychallenge-club-c8bdf59902b53b33005f98d3538fcf57329bf7c8.zip
Perl solution for week 111, part 1
-rw-r--r--challenge-111/abigail/README.md21
-rw-r--r--challenge-111/abigail/perl/ch-1.pl50
-rw-r--r--challenge-111/abigail/t/ctest.ini1
-rw-r--r--challenge-111/abigail/t/input-1-17
-rw-r--r--challenge-111/abigail/t/output-1-1.exp2
5 files changed, 81 insertions, 0 deletions
diff --git a/challenge-111/abigail/README.md b/challenge-111/abigail/README.md
index ac8ba329ad..c72b062759 100644
--- a/challenge-111/abigail/README.md
+++ b/challenge-111/abigail/README.md
@@ -9,8 +9,29 @@
> efficient search algorithm.
### Notes
+This challenge confuses me. We're basically asked to find a number
+in a sorted list. Which in languages without hashes one would solve
+with binary search (yielding an O (log N) solution), and in languages
+with hashes you'd use a hash (yielding an O (1) (expected) time solution).
+Sure, the hash takes linear preprocessing time, but since we're asked
+to write a script, we're spending linear time reading in the data
+anyway.
+
+Perhaps the intend was a subroutine which takes a matrix and a target
+number, but that was not what is being asked. The challenge explicitly
+asks for *a script*, which means we have to spend a linear amount of
+time reading data anyway. So, that's what you get.
+
+The only part where we use the fact we are given a matrix is for the
+input: the first five lines are assumed to contain the matrix. The
+rest of the input is taken as numbers to search for.
+
+Only for language lacking hashes/maps/dictionaries/tables, we will
+make use of the fact the input is sorted. For the majority of
+languages, the fact input is sorted does not offer additional benefits.
### Solutions
+* [Perl](perl/ch-1.pl)
### Blog
diff --git a/challenge-111/abigail/perl/ch-1.pl b/challenge-111/abigail/perl/ch-1.pl
new file mode 100644
index 0000000000..00f6e2502d
--- /dev/null
+++ b/challenge-111/abigail/perl/ch-1.pl
@@ -0,0 +1,50 @@
+#!/opt/perl/bin/perl
+
+use 5.032;
+
+use strict;
+use warnings;
+no warnings 'syntax';
+
+use experimental 'signatures';
+use experimental 'lexical_subs';
+
+#
+# See ../README.md
+#
+
+#
+# Run as: perl ch-1.pl < input-file
+#
+
+#
+# This challenge confuses me. We're basically asked to find a number
+# in a sorted list. Which in languages without hashes one would solve
+# with binary search (yielding an O (log N) solution), and in languages
+# with hashes you'd use a hash (yielding an O (1) (expected) time solution).
+# Sure, the hash takes linear preprocessing time, but since we're asked
+# to write a script, we're spending linear time reading in the data
+# anyway.
+#
+# Perhaps the intend was a subroutine which takes a matrix and a target
+# number, but that was not what is being asked. The challenge explicitly
+# asks for *a script*, which means we have to spend a linear amount of
+# time reading data anyway. So, that's what you get.
+#
+# The only part where we use the fact we are given a matrix is for the
+# input: the first five lines are assumed to contain the matrix. The
+# rest of the input is taken as numbers to search for.
+#
+
+my $MATRIX_SIZE = 5;
+
+#
+# Read in a matrix of data
+#
+my %matrix;
+@matrix {<> =~ /-?[0-9]+/g} = () for 1 .. $MATRIX_SIZE;
+
+#
+# Print 0/1 depending on whether the given number is in the matrix or not.
+#
+chomp, say exists $matrix {$_} || 0 while <>
diff --git a/challenge-111/abigail/t/ctest.ini b/challenge-111/abigail/t/ctest.ini
index 0c67225c11..593491a556 100644
--- a/challenge-111/abigail/t/ctest.ini
+++ b/challenge-111/abigail/t/ctest.ini
@@ -4,6 +4,7 @@
#
[names]
+1-1 = Given example
2-1 = Enable.lst
2-2 = /usr/share/dict/words
2-3 = infochimps.com
diff --git a/challenge-111/abigail/t/input-1-1 b/challenge-111/abigail/t/input-1-1
new file mode 100644
index 0000000000..fa2f7e3fa1
--- /dev/null
+++ b/challenge-111/abigail/t/input-1-1
@@ -0,0 +1,7 @@
+ 1 2 3 5 7
+ 9 11 15 19 20
+23 24 25 29 31
+32 33 39 40 42
+45 47 48 49 50
+35
+39
diff --git a/challenge-111/abigail/t/output-1-1.exp b/challenge-111/abigail/t/output-1-1.exp
new file mode 100644
index 0000000000..0d66ea1aee
--- /dev/null
+++ b/challenge-111/abigail/t/output-1-1.exp
@@ -0,0 +1,2 @@
+0
+1