aboutsummaryrefslogtreecommitdiff
path: root/challenge-111/james-smith
diff options
context:
space:
mode:
authorroot <root@curtissmith.me.uk>2021-05-03 20:11:34 +0100
committerroot <root@curtissmith.me.uk>2021-05-03 20:11:34 +0100
commit0eefe3f74139bb656a70eb3f8c38b67a5f4067eb (patch)
tree62e9591b71d5ba34df719335085b54fa4475a9c2 /challenge-111/james-smith
parent0381a39b17ccd040302474f25d3c1cbbef703327 (diff)
downloadperlweeklychallenge-club-0eefe3f74139bb656a70eb3f8c38b67a5f4067eb.tar.gz
perlweeklychallenge-club-0eefe3f74139bb656a70eb3f8c38b67a5f4067eb.tar.bz2
perlweeklychallenge-club-0eefe3f74139bb656a70eb3f8c38b67a5f4067eb.zip
solutions for 111
Diffstat (limited to 'challenge-111/james-smith')
-rw-r--r--challenge-111/james-smith/perl/ch-1.pl36
-rw-r--r--challenge-111/james-smith/perl/ch-2.pl46
2 files changed, 82 insertions, 0 deletions
diff --git a/challenge-111/james-smith/perl/ch-1.pl b/challenge-111/james-smith/perl/ch-1.pl
new file mode 100644
index 0000000000..2c46d1bdeb
--- /dev/null
+++ b/challenge-111/james-smith/perl/ch-1.pl
@@ -0,0 +1,36 @@
+#!/usr/local/bin/perl
+
+use strict;
+
+use warnings;
+use feature qw(say);
+use Test::More;
+
+my $matrix = [
+ [ 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 ],
+];
+
+is( find_value( 35, $matrix ), 0 );
+is( find_value( 39, $matrix ), 1 );
+is( find_value( 3, $matrix ), 1 );
+is( find_value( 8, $matrix ), 0 );
+is( find_value( 23, $matrix ), 1 );
+is( find_value( 41, $matrix ), 0 );
+
+done_testing();
+
+sub find_value {
+ my( $value, $m, @list ) = ( $_[0], 0, map { @{$_} } @{$_[1]} );
+ ( $m = @list >> 1 )
+ && ( $list[$m] == $value )
+ ? ( return 1 )
+ : ( $list[$m] > $value )
+ ? ( @list = @list[0 .. $m-1] )
+ : ( splice @list, 0, $m ) while @list>1;
+ return 0;
+}
+
diff --git a/challenge-111/james-smith/perl/ch-2.pl b/challenge-111/james-smith/perl/ch-2.pl
new file mode 100644
index 0000000000..b069866080
--- /dev/null
+++ b/challenge-111/james-smith/perl/ch-2.pl
@@ -0,0 +1,46 @@
+#!/usr/local/bin/perl
+
+use strict;
+
+use warnings;
+use feature qw(say);
+
+## These are the dictionary's supplied by Ubuntu.
+
+say longest('/usr/share/dict/british-english-small');
+say longest('/usr/share/dict/british-english-large');
+say longest('/usr/share/dict/british-english-huge');
+say longest('/usr/share/dict/british-english-insane');
+
+## Aegilops is a genus of Eurasian and North American
+## plants in the grass family, Poaceae. They are known
+## generally as goatgrasses. Some species are known
+## as invasive weeds in parts of North America.
+
+sub longest {
+ open my $fh, q(<), $_[0];
+ my @max = (0);
+ (chomp) ## Remove newline character
+ && (!/\W/) ## Remove words with non-alpha chars
+ && (!/^[A-Z]/) ## Remove words starting with a capital
+ && (join( q(), sort { $a cmp $b } split //,lc $_) eq lc $_)
+ ## Check the word is unchanged when the
+ ## letters are sorted
+ && ( ($max[0] < length $_)
+ ? ( @max = ( length $_, $_ ) )
+ ## If the word is longer than the max length (1st entry
+ ## in @max - reset max to include the new max length and
+ ## the word.
+ : ( ($max[0] == length $_)
+ && (push @max,$_)
+ )
+ )
+ ## If the word is the same length as the maximal word
+ ## push it onto @max - so we store all the longest words
+ ## with maximum length.
+ while <$fh>;
+ return "$_[0] > @max";
+ ## Return the name of the file used, the size of the words and
+ ## a complete list of the words of that length.
+}
+