From 490b3d0342e8938320be51359631ba2906cd9b90 Mon Sep 17 00:00:00 2001 From: Simon Green Date: Sun, 9 May 2021 21:21:52 +1000 Subject: sgreen solution to challenge 111 --- challenge-111/sgreen/README.md | 4 ++-- challenge-111/sgreen/blog.txt | 1 + challenge-111/sgreen/perl/ch-1.pl | 44 ++++++++++++++++++++++++++++++++++ challenge-111/sgreen/perl/ch-2.pl | 50 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 challenge-111/sgreen/blog.txt create mode 100755 challenge-111/sgreen/perl/ch-1.pl create mode 100755 challenge-111/sgreen/perl/ch-2.pl 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); -- cgit