From e950e60f9c44d1f4d0ea04ab65f30a1ad5f73cc8 Mon Sep 17 00:00:00 2001 From: Simon Green Date: Sun, 29 Aug 2021 16:04:38 +1000 Subject: no solution to challenge 127 --- challenge-127/sgreen/README.md | 4 ++-- challenge-127/sgreen/blog.txt | 1 + challenge-127/sgreen/perl/ch-1.pl | 22 ++++++++++++++++++++++ challenge-127/sgreen/perl/ch-2.pl | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 challenge-127/sgreen/blog.txt create mode 100755 challenge-127/sgreen/perl/ch-1.pl create mode 100755 challenge-127/sgreen/perl/ch-2.pl diff --git a/challenge-127/sgreen/README.md b/challenge-127/sgreen/README.md index f9b8c29dad..a8fab60739 100644 --- a/challenge-127/sgreen/README.md +++ b/challenge-127/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 126 +# The Weekly Challenge 127 -Solution by Simon Green. [Blog](https://dev.to/simongreennet/weekly-challenge-126-19fg) +Solution by Simon Green. [Blog](https://dev.to/simongreennet/weekly-challenge-127-4k6j) diff --git a/challenge-127/sgreen/blog.txt b/challenge-127/sgreen/blog.txt new file mode 100644 index 0000000000..ba34b341fc --- /dev/null +++ b/challenge-127/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/weekly-challenge-127-4k6j diff --git a/challenge-127/sgreen/perl/ch-1.pl b/challenge-127/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..796c65dd69 --- /dev/null +++ b/challenge-127/sgreen/perl/ch-1.pl @@ -0,0 +1,22 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use feature 'say'; +use List::Util 'any'; + +sub main { + # Turn the set into two arrays + my ( $set1, $set2 ) = @_; + my @set1 = ( $set1 =~ /(\d+)/g ); + my @set2 = ( $set2 =~ /(\d+)/g ); + + # Turn the first set into a hash + my %set1 = map { $_, 1 } @set1; + + # See if any values in the second set are in the hash + my $disjoined = ( any { exists $set1{$_} } @set2 ) ? 0 : 1; + say $disjoined; +} + +main(@ARGV); diff --git a/challenge-127/sgreen/perl/ch-2.pl b/challenge-127/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..85b90fb1f4 --- /dev/null +++ b/challenge-127/sgreen/perl/ch-2.pl @@ -0,0 +1,32 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use List::Util 'any'; + +sub main { + my @numbers = ( join( ' ', @_ ) =~ /(\d+)/g ); + + # Sanity check and pair up numbers + die "You must provide an even amount of numbers" if $#numbers % 2 == 0; + + my @sets = (); + my @match = (); + + while (@numbers) { + my $first = shift @numbers; + my $last = shift @numbers; + + if ( any { $first <= $_->[1] and $last >= $_->[0] } @sets ) { + # This interval intersects with a previous one + push @match, [ $first, $last ]; + } + # Add this interval to a list of previous intervals + push @sets, [ $first, $last ]; + } + + say '[ ', join( ', ', map { "($_->[0],$_->[1])" } @match ), ' ]'; +} + +main(@ARGV); -- cgit