diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-08-29 17:07:39 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-08-29 17:07:39 +0100 |
| commit | f7c19e9d31cb01ba3f4e78b0b32aaffa3862817c (patch) | |
| tree | e348760f845db6265d717d2bd2e94d690b058c36 /challenge-127 | |
| parent | b34d385b993106a005d026e5a4df8adbc6662945 (diff) | |
| download | perlweeklychallenge-club-f7c19e9d31cb01ba3f4e78b0b32aaffa3862817c.tar.gz perlweeklychallenge-club-f7c19e9d31cb01ba3f4e78b0b32aaffa3862817c.tar.bz2 perlweeklychallenge-club-f7c19e9d31cb01ba3f4e78b0b32aaffa3862817c.zip | |
- Added solutions by Pete Houston.
Diffstat (limited to 'challenge-127')
| -rwxr-xr-x | challenge-127/pete-houston/perl/ch-1.pl | 30 | ||||
| -rwxr-xr-x | challenge-127/pete-houston/perl/ch-2.pl | 49 |
2 files changed, 79 insertions, 0 deletions
diff --git a/challenge-127/pete-houston/perl/ch-1.pl b/challenge-127/pete-houston/perl/ch-1.pl new file mode 100755 index 0000000000..7c9e8867c3 --- /dev/null +++ b/challenge-127/pete-houston/perl/ch-1.pl @@ -0,0 +1,30 @@ +#!/usr/bin/env perl +#=============================================================================== +# +# FILE: 12701.pl +# +# USAGE: ./12701.pl +# +# DESCRIPTION: Read 2 sets of integers and output 1 if they are +# disjoint, 0 otherwise +# +# BUGS: Not particularly clever. +# NOTES: The input values can be separated by anything. +# AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk +# ORGANIZATION: Openstrike +# VERSION: 1.0 +# CREATED: 23/08/21 +#=============================================================================== + +use strict; +use warnings; + +print "Input the first set of integers on one line:\n"; +$_ = <STDIN>; +my %first = map { $_ => 1 } /(-?[0-9]+)/g; + +print "Input the second set of integers on one line:\n"; +$_ = <STDIN>; +my @matches = grep { $first{$_} } /(-?[0-9]+)/g; + +printf "%i\n", $#matches > -1 ? 0 : 1; diff --git a/challenge-127/pete-houston/perl/ch-2.pl b/challenge-127/pete-houston/perl/ch-2.pl new file mode 100755 index 0000000000..44f98395bb --- /dev/null +++ b/challenge-127/pete-houston/perl/ch-2.pl @@ -0,0 +1,49 @@ +#!/usr/bin/env perl +#=============================================================================== +# +# FILE: 12702.pl +# +# USAGE: ./12702.pl A,B C,D [ E,F ... ] +# +# DESCRIPTION: Given pairs of integers treat each as an interval and +# report any which conflict with previous pairs. +# The pairs may each be comma-separated but really anything +# other than digits and hyphens will do to separate them +# +# NOTES: We assume that each supplied number is an integer. +# Further, we assume that "conflict" means that if N is +# the upper bound of one interval and the lower bound of +# another then that is a conflict. +# +# AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk +# ORGANIZATION: Openstrike +# VERSION: 1.0 +# CREATED: 28/08/21 +#=============================================================================== + +use strict; +use warnings; + +my (@ints, @conflicts); + +while (my $pair = shift) { + # Extract 2 integers from each argument + # Don't assume the lower limit comes first + my ($lower, $upper) = sort { $a <=> $b } $pair =~ /(-?[0-9]+)/g; + + # Validate + die "$pair is not an interval" + unless defined ($lower) && defined ($upper); + + # Compare to previous intervals + for my $int (@ints) { + next if $lower > $int->[1] || $upper < $int->[0]; + push @conflicts, [$lower, $upper]; + last; + } + + # Add to the list + push @ints, [$lower, $upper]; +} + +print '[ ', join (', ', map { "($_->[0],$_->[1])" } @conflicts), " ]\n"; |
