aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-08-29 15:59:27 +0100
committerGitHub <noreply@github.com>2021-08-29 15:59:27 +0100
commite3237b294e971c254fa078d401a4f1e8e316911f (patch)
treebf8607232f7f6225147e57cad314c188939be3bb
parent795d16f7ff75131f2b8b537b420d9c3215fdae6a (diff)
parent6e07a1c75417265eede6225dd438e8b5b2e581a8 (diff)
downloadperlweeklychallenge-club-e3237b294e971c254fa078d401a4f1e8e316911f.tar.gz
perlweeklychallenge-club-e3237b294e971c254fa078d401a4f1e8e316911f.tar.bz2
perlweeklychallenge-club-e3237b294e971c254fa078d401a4f1e8e316911f.zip
Merge pull request #4805 from wanderdoc/master
Solutions to challenge-127
-rw-r--r--challenge-127/wanderdoc/perl/ch-1.pl37
-rw-r--r--challenge-127/wanderdoc/perl/ch-2.pl77
2 files changed, 114 insertions, 0 deletions
diff --git a/challenge-127/wanderdoc/perl/ch-1.pl b/challenge-127/wanderdoc/perl/ch-1.pl
new file mode 100644
index 0000000000..23cdcb7db2
--- /dev/null
+++ b/challenge-127/wanderdoc/perl/ch-1.pl
@@ -0,0 +1,37 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+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.
+=cut
+
+
+
+
+
+use List::Util qw(uniq);
+
+sub check_sets
+{
+ my ($aref_1, $aref_2) = @_;
+ return (scalar @$aref_1 + scalar @$aref_2 == uniq(@$aref_1, @$aref_2)) ?
+ 1 : 0;
+}
+
+print check_sets([1,2,3,5,4], [4,6,7,8,9]), $/;
+print check_sets([1,3,5,7,9], [0,2,4,6,8]), $/; \ No newline at end of file
diff --git a/challenge-127/wanderdoc/perl/ch-2.pl b/challenge-127/wanderdoc/perl/ch-2.pl
new file mode 100644
index 0000000000..9344932742
--- /dev/null
+++ b/challenge-127/wanderdoc/perl/ch-2.pl
@@ -0,0 +1,77 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+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.
+
+=cut
+
+
+
+
+
+sub find_conflicts
+{
+ my $aref = $_[0];
+ my %output;
+
+
+ for my $idx_1 ( 1 .. $#$aref ) # no conflict at position 0.
+ {
+ for my $idx_2 ( 0 .. $idx_1 - 1 )
+ {
+
+ if ( ($aref->[$idx_1][0] < $aref->[$idx_2][0] and
+ $aref->[$idx_1][1] < $aref->[$idx_2][1] and
+ $aref->[$idx_1][1] < $aref->[$idx_2][0])
+
+ or
+
+ ($aref->[$idx_1][0] > $aref->[$idx_2][0] and
+ $aref->[$idx_1][1] > $aref->[$idx_2][1] and
+ $aref->[$idx_1][0] > $aref->[$idx_2][1])
+ )
+ {
+ next;
+
+ }
+ else
+ {
+ $output{join(", ",@{$aref->[$idx_1]})} = undef;
+ }
+ }
+ }
+
+
+ return [ sort { (split(/, /, $a))[0] <=> (split(/, /, $b))[0] or
+ (split(/, /, $a))[1] <=> (split(/, /, $b))[1] } keys %output];
+
+}
+
+my $result = find_conflicts([ [1,4], [3,5], [6,8], [12, 13], [3,20] ]);
+print join("", "(",join("), (", @$result), ")"), $/;
+$result = find_conflicts([ [3,4], [5,7], [6,9], [10, 12], [13,15] ]);
+print join("", "(",join("), (", @$result), ")"), $/;
+