aboutsummaryrefslogtreecommitdiff
path: root/challenge-111
diff options
context:
space:
mode:
authorSimon Green <mail@simon.green>2021-05-09 21:21:52 +1000
committerSimon Green <mail@simon.green>2021-05-09 21:21:52 +1000
commit490b3d0342e8938320be51359631ba2906cd9b90 (patch)
treebbdc587d3b2326be52598150441d274814827863 /challenge-111
parentdab7f9afb80af2e046ed6296fda3c14f16fdaf8c (diff)
downloadperlweeklychallenge-club-490b3d0342e8938320be51359631ba2906cd9b90.tar.gz
perlweeklychallenge-club-490b3d0342e8938320be51359631ba2906cd9b90.tar.bz2
perlweeklychallenge-club-490b3d0342e8938320be51359631ba2906cd9b90.zip
sgreen solution to challenge 111
Diffstat (limited to 'challenge-111')
-rw-r--r--challenge-111/sgreen/README.md4
-rw-r--r--challenge-111/sgreen/blog.txt1
-rwxr-xr-xchallenge-111/sgreen/perl/ch-1.pl44
-rwxr-xr-xchallenge-111/sgreen/perl/ch-2.pl50
4 files changed, 97 insertions, 2 deletions
diff --git a/challenge-111/sgreen/README.md b/challenge-111/sgreen/README.md
index 53f7bdfaba..3d27abbe88 100644
--- a/challenge-111/sgreen/README.md
+++ b/challenge-111/sgreen/README.md
@@ -1,3 +1,3 @@
-# The Weekly Challenge 110
+# The Weekly Challenge 111
-Solution by Simon Green. [Blog](https://dev.to/simongreennet/weekly-challenge-110-22ao)
+Solution by Simon Green. [Blog](https://dev.to/simongreennet/weekly-challenge-111-291m)
diff --git a/challenge-111/sgreen/blog.txt b/challenge-111/sgreen/blog.txt
new file mode 100644
index 0000000000..a9c7d30406
--- /dev/null
+++ b/challenge-111/sgreen/blog.txt
@@ -0,0 +1 @@
+https://dev.to/simongreennet/weekly-challenge-111-291m
diff --git a/challenge-111/sgreen/perl/ch-1.pl b/challenge-111/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..e9400d4edb
--- /dev/null
+++ b/challenge-111/sgreen/perl/ch-1.pl
@@ -0,0 +1,44 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+
+sub main {
+ my $target = pop;
+ my @matrix = ( join( ' ', @_ ) =~ /(\d+)/g );
+
+ my $low = 0;
+ my $high = $#matrix;
+ my $checks = 0;
+ my $found = 0;
+
+ while (1) {
+ ++$checks;
+
+ # Find the middle value (round down if not even)
+ my $index = int( ( $low + $high ) / 2 );
+ my $value = $matrix[$index];
+
+ if ( $matrix[$index] == $target ) {
+ # We've found the target number
+ $found = 1;
+ last;
+ }
+ elsif ( $low >= $high ) {
+ # The number doesn't exist
+ last;
+ }
+ elsif ( $value > $target ) {
+ $high = $index - 1;
+ }
+ else {
+ $low = $index + 1;
+ }
+
+ }
+
+ say "Answer is $found, in $checks check(s)";
+}
+
+main(@ARGV);
diff --git a/challenge-111/sgreen/perl/ch-2.pl b/challenge-111/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..afc239212b
--- /dev/null
+++ b/challenge-111/sgreen/perl/ch-2.pl
@@ -0,0 +1,50 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+
+sub _is_ordered {
+ my $word = lc shift;
+ return if $word !~ /^[a-z]+$/;
+
+ # Order the word by letters
+ my $ordered = join '', sort split '', $word;
+
+ return $ordered eq $word;
+}
+
+sub main {
+ my $file = shift // '/usr/share/dict/words';
+
+ # Define some values
+ my $max_length = 0;
+ my @words = ();
+
+ # Open the file for reading
+ open( my $fh, '<', $file ) or die "Cannot open file $file: $!";
+
+ while ( my $word = <$fh> ) {
+ chomp $word;
+
+ # If the word has all its characters in alphabetical order, either
+ # create a new list if it is longer than the current max_length, or
+ # append it to the list if it is the same.
+ if ( _is_ordered($word) ) {
+ my $length = length($word);
+
+ if ( $length > $max_length ) {
+ $max_length = $length;
+ @words = ($word);
+ }
+ elsif ( $length == $max_length ) {
+ push @words, $word;
+ }
+ }
+ }
+
+ # Disply result
+ say "Longest words are: ", join( ', ', @words );
+}
+
+main(@ARGV);