diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-08-29 15:53:15 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-08-29 15:53:15 +0100 |
| commit | 980dec4c8b073520d1aa99137c43fcb2491d116b (patch) | |
| tree | 2c32279e39b4d65f52ca45ea86bf9284cd6c39d9 | |
| parent | 29980202a0585f4ad1820e8a157e13ace2d22ad1 (diff) | |
| parent | e950e60f9c44d1f4d0ea04ab65f30a1ad5f73cc8 (diff) | |
| download | perlweeklychallenge-club-980dec4c8b073520d1aa99137c43fcb2491d116b.tar.gz perlweeklychallenge-club-980dec4c8b073520d1aa99137c43fcb2491d116b.tar.bz2 perlweeklychallenge-club-980dec4c8b073520d1aa99137c43fcb2491d116b.zip | |
Merge pull request #4803 from simongreen-net/master
no solution to challenge 127
| -rw-r--r-- | challenge-127/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-127/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-127/sgreen/perl/ch-1.pl | 22 | ||||
| -rwxr-xr-x | challenge-127/sgreen/perl/ch-2.pl | 32 |
4 files changed, 57 insertions, 2 deletions
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); |
