aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-127/paulo-custodio/perl/ch-1.pl28
-rw-r--r--challenge-127/paulo-custodio/perl/ch-2.pl85
-rw-r--r--challenge-127/paulo-custodio/t/test-1.yaml14
-rw-r--r--challenge-127/paulo-custodio/t/test-2.yaml10
-rw-r--r--challenge-127/paulo-custodio/test.pl4
5 files changed, 141 insertions, 0 deletions
diff --git a/challenge-127/paulo-custodio/perl/ch-1.pl b/challenge-127/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..dd8de28b85
--- /dev/null
+++ b/challenge-127/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,28 @@
+#!/usr/bin/env perl
+
+# Challenge 127
+#
+# TASK #1 > Disjoint Sets
+# Submitted by: Mohammad S Anwar
+# 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 Modern::Perl;
+use Set::Intersection;
+
+my @s1 = split(' ', scalar(<>));
+my @s2 = split(' ', scalar(<>));
+my @intersection = get_intersection(\@s1, \@s2);
+say @intersection ? 0 : 1;
diff --git a/challenge-127/paulo-custodio/perl/ch-2.pl b/challenge-127/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..3fefaca3e5
--- /dev/null
+++ b/challenge-127/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,85 @@
+#!/usr/bin/env perl
+
+# TASK #2 > Conflict Intervals
+# Submitted by: Mohammad S Anwar
+# 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 Modern::Perl;
+
+my @intervals = parse_intervals(@ARGV);
+my @conflicts = find_conflicts(@intervals);
+print_conflicts(@conflicts);
+
+
+sub parse_intervals {
+ my(@in) = @_;
+ my @out;
+ while (@in) {
+ my($s, $e) = splice(@in, 0, 2);
+ push @out, [$s, $e];
+ }
+ return @out;
+}
+
+sub find_conflicts {
+ my(@intervals) = @_;
+ my @conflicts;
+ for my $i (1 .. $#intervals) {
+ for my $j (0 .. $i-1) {
+ my $i1s = $intervals[$i][0];
+ my $i1e = $intervals[$i][1];
+ my $i2s = $intervals[$j][0];
+ my $i2e = $intervals[$j][1];
+ if (($i1s >= $i2s && $i1s < $i2e) ||
+ ($i1e >= $i2s && $i1e < $i2e) ||
+ ($i1s < $i2s && $i1e >= $i2e)) {
+ push @conflicts, $intervals[$i];
+ last;
+ }
+ }
+ }
+
+ return @conflicts;
+}
+
+sub print_conflicts {
+ my(@conflicts) = @_;
+ use Data::Dump 'dump';
+ print "[ ";
+ my $sep = "";
+ for (@conflicts) {
+ my($s, $e) = @$_;
+ print $sep,"($s,$e)";
+ $sep = ", ";
+ }
+ print " ]\n";
+}
diff --git a/challenge-127/paulo-custodio/t/test-1.yaml b/challenge-127/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..be7529e7e4
--- /dev/null
+++ b/challenge-127/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,14 @@
+- setup:
+ cleanup:
+ args:
+ input: |
+ 1 2 5 3 4
+ 4 6 7 8 9
+ output: 0
+- setup:
+ cleanup:
+ args:
+ input: |
+ 1 3 5 7 9
+ 0 2 4 6 8
+ output: 1
diff --git a/challenge-127/paulo-custodio/t/test-2.yaml b/challenge-127/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..23446cc7dc
--- /dev/null
+++ b/challenge-127/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,10 @@
+- setup:
+ cleanup:
+ args: 1 4 3 5 6 8 12 13 3 20
+ input:
+ output: [ (3,5), (3,20) ]
+- setup:
+ cleanup:
+ args: 3 4 5 7 6 9 10 12 13 15
+ input:
+ output: [ (6,9) ]
diff --git a/challenge-127/paulo-custodio/test.pl b/challenge-127/paulo-custodio/test.pl
new file mode 100644
index 0000000000..ba6c37260b
--- /dev/null
+++ b/challenge-127/paulo-custodio/test.pl
@@ -0,0 +1,4 @@
+#!/usr/bin/env perl
+use Modern::Perl;
+use Test::More;
+require '../../challenge-001/paulo-custodio/test.pl';