aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-127/andrezgz/perl/ch-1.pl42
-rw-r--r--challenge-127/andrezgz/perl/ch-2.pl65
2 files changed, 107 insertions, 0 deletions
diff --git a/challenge-127/andrezgz/perl/ch-1.pl b/challenge-127/andrezgz/perl/ch-1.pl
new file mode 100644
index 0000000000..4388320297
--- /dev/null
+++ b/challenge-127/andrezgz/perl/ch-1.pl
@@ -0,0 +1,42 @@
+#!/usr/bin/perl
+
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-127/
+# TASK #1
+#
+# Disjoint Sets
+# You are given two sets with unique integers.
+#
+# Write a script to figure out if they are disjoint.
+#
+# The two sets are disjoint if they don't have any common members.
+#
+# Example
+# Input: @S1 = (1, 2, 5, 3, 4)
+# @S2 = (4, 6, 7, 8, 9)
+# Output: 0 as the given two sets have common member 4.
+#
+# Input: @S1 = (1, 3, 5, 7, 9)
+# @S2 = (0, 2, 4, 6, 8)
+# Output: 1 as the given two sets do not have common member.
+
+use strict;
+use warnings;
+use feature 'say';
+
+my @S1 = split /,/, shift;
+my @S2 = split /,/, shift;
+
+my (%union,%inter);
+for (@S1,@S2) {
+ $union{$_}++ && $inter{$_}++
+}
+
+say keys %inter ? 0 : 1;
+
+__END__
+
+$ perl ch-1.pl 1,2,3,4 4,6,7,8,9
+0
+
+$ perl ch-1.pl 1,3,5,7,9 0,2,4,6,8
+1
diff --git a/challenge-127/andrezgz/perl/ch-2.pl b/challenge-127/andrezgz/perl/ch-2.pl
new file mode 100644
index 0000000000..ed957126c7
--- /dev/null
+++ b/challenge-127/andrezgz/perl/ch-2.pl
@@ -0,0 +1,65 @@
+#!/usr/bin/perl
+
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-127/
+# Task #2
+#
+# Conflict Intervals
+# You are given a list of intervals.
+#
+# Write a script to find out if the current interval conflicts with any of the previous intervals.
+#
+# Example
+# Input: @Intervals = [ (1,4), (3,5), (6,8), (12, 13), (3,20) ]
+# Output: [ (3,5), (3,20) ]
+#
+# - The 1st interval (1,4) do not have any previous intervals to compare with, so skip it.
+# - The 2nd interval (3,5) does conflict with previous interval (1,4).
+# - The 3rd interval (6,8) do not conflicts with any of the previous intervals (1,4) and (3,5), so skip it.
+# - The 4th interval (12,13) again do not conflicts with any of the previous intervals (1,4), (3,5) and (6,8), so skip it.
+# - The 5th interval (3,20) conflicts with the first interval (1,4).
+#
+# Input: @Intervals = [ (3,4), (5,7), (6,9), (10, 12), (13,15) ]
+# Output: [ (6,9) ]
+#
+# - The 1st interval (3,4) do not have any previous intervals to compare with, so skip it.
+# - The 2nd interval (5,7) do not conflicts with the previous interval (3,4), so skip it.
+# - The 3rd interval (6,9) does conflict with one of the previous intervals (5,7).
+# - The 4th interval (10,12) do not conflicts with any of the previous intervals (3,4), (5,7) and (6,9), so skip it.
+# - The 5th interval (13,15) do not conflicts with any of the previous intervals (3,4), (5,7), (6,9) and (10,12), so skip it.
+
+use strict;
+use warnings;
+use feature 'say';
+
+my @input;
+push @input, [ split /,/ ] for @ARGV;
+
+my @conflict;
+OUTER:
+for my $cur (1..$#input) {
+ for my $prev (0..$cur-1) {
+ next if $input[$cur]->[0] > $input[$prev]->[1];
+ next if $input[$cur]->[1] < $input[$prev]->[0];
+ push @conflict, $input[$cur];
+ next OUTER;
+ }
+}
+
+say join ' ', map { sprintf '%d,%d', @$_ } @conflict;
+
+
+__END__
+
+$ perl ch-2.pl 1,4 3,5 6,8 12,13 3,20
+3,5 3,20
+
+$ perl ch-2.pl 3,4 5,7 6,9 10,12 13,15
+6,9
+
+$ perl ch-2.pl 3,4 8,9 5,7 12,13 6,7
+6,7
+
+$ perl ch-2.pl 3,4 9,11 6,10 12,13
+6,10
+
+$ perl ch-2.pl 1,2 3,5 6,8 12,13 9,11