DEV Community

Simon Green
Simon Green

Posted on

Weekly Challenge 127

Challenge, My solutions

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
Enter fullscreen mode Exit fullscreen mode

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) ]
Enter fullscreen mode Exit fullscreen mode

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay