TASK #1 › Disjoint Sets
Task
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.
My solution
Not much to see here. I turn the first array into a hash (where the key is the inputs), and then use the any function from List::Util to see if any members of the second array are found in the hash.
Examples
$ ./ch-1.pl "(1, 2, 5, 3, 4)" "(4, 6, 7, 8, 9)"
0
$ ./ch-1.pl "(1, 3, 5, 7, 9)" "(0, 2, 4, 6, 8)"
1
TASK #2 › Conflict Intervals
Task
You are given a list of intervals.
Write a script to find out if the current interval conflicts with any of the previous intervals.
My solution
This is also a relatively straight forward task. We know that an interval conflicts with a previous one if the first number is less than or equal to the last number of a previous interval, and the end is greater than or equal to the first number of a previous interval.
I have two arrays. The first is @sets
which stores the previous intervals as we see them, and @match
which stores the intervals that conflict. I then print the values in the second array.
Examples
$ ./ch-2.pl "[ (1,4), (3,5), (6,8), (12, 13), (3,20) ]"
[ (3,5), (3,20) ]
$ ./ch-2.pl "[ (3,4), (5,7), (6,9), (10, 12), (13,15) ]"
[ (6,9) ]
Top comments (0)