diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-10-27 10:27:54 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-10-27 10:27:54 +0100 |
| commit | 3b176120bc8f6b3cf386a429f7a04d9c337959d6 (patch) | |
| tree | 683216168345537272a6ec766639b594e9566af9 | |
| parent | 5c77c18046bf2e6b0e4639f0d4c7856eeb99142a (diff) | |
| download | perlweeklychallenge-club-3b176120bc8f6b3cf386a429f7a04d9c337959d6.tar.gz perlweeklychallenge-club-3b176120bc8f6b3cf386a429f7a04d9c337959d6.tar.bz2 perlweeklychallenge-club-3b176120bc8f6b3cf386a429f7a04d9c337959d6.zip | |
Add Python solution to challenge 127
| -rw-r--r-- | challenge-127/paulo-custodio/perl/ch-1.pl | 2 | ||||
| -rw-r--r-- | challenge-127/paulo-custodio/perl/ch-2.pl | 9 | ||||
| -rw-r--r-- | challenge-127/paulo-custodio/python/ch-1.py | 28 | ||||
| -rw-r--r-- | challenge-127/paulo-custodio/python/ch-2.py | 71 |
4 files changed, 104 insertions, 6 deletions
diff --git a/challenge-127/paulo-custodio/perl/ch-1.pl b/challenge-127/paulo-custodio/perl/ch-1.pl index dd8de28b85..60134894d8 100644 --- a/challenge-127/paulo-custodio/perl/ch-1.pl +++ b/challenge-127/paulo-custodio/perl/ch-1.pl @@ -8,7 +8,7 @@ # # Write a script to figure out if they are disjoint. # -# The two sets are disjoint if they don’t have any common members. +# The two sets are disjoint if they don't have any common members. # # Example # Input: @S1 = (1, 2, 5, 3, 4) diff --git a/challenge-127/paulo-custodio/perl/ch-2.pl b/challenge-127/paulo-custodio/perl/ch-2.pl index 3fefaca3e5..39f76f858d 100644 --- a/challenge-127/paulo-custodio/perl/ch-2.pl +++ b/challenge-127/paulo-custodio/perl/ch-2.pl @@ -37,7 +37,7 @@ use Modern::Perl; my @intervals = parse_intervals(@ARGV); my @conflicts = find_conflicts(@intervals); -print_conflicts(@conflicts); +print_intervals(@conflicts); sub parse_intervals { @@ -71,12 +71,11 @@ sub find_conflicts { return @conflicts; } -sub print_conflicts { - my(@conflicts) = @_; - use Data::Dump 'dump'; +sub print_intervals { + my(@intervals) = @_; print "[ "; my $sep = ""; - for (@conflicts) { + for (@intervals) { my($s, $e) = @$_; print $sep,"($s,$e)"; $sep = ", "; diff --git a/challenge-127/paulo-custodio/python/ch-1.py b/challenge-127/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..7313b831b1 --- /dev/null +++ b/challenge-127/paulo-custodio/python/ch-1.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python3 + +# 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. + +s1 = set([int(x) for x in input().split()]) +s2 = set([int(x) for x in input().split()]) +z = s1.intersection(s2) +if len(z)==0: + print(1) +else: + print(0) diff --git a/challenge-127/paulo-custodio/python/ch-2.py b/challenge-127/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..e72a3f39b9 --- /dev/null +++ b/challenge-127/paulo-custodio/python/ch-2.py @@ -0,0 +1,71 @@ +#!/usr/bin/env python3 + +# 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. + +import sys + +def parse_intervals(args): + args = [int(x) for x in args] + out = [] + while args: + out.append([args.pop(0), args.pop(0)]) + return out + +def find_conflicts(intervals): + conflicts = [] + for i in range(1, len(intervals)): + for j in range(0, i): + i1s = intervals[i][0] + i1e = intervals[i][1] + i2s = intervals[j][0] + i2e = intervals[j][1] + if (i1s >= i2s and i1s < i2e) or \ + (i1e >= i2s and i1e < i2e) or \ + (i1s < i2s and i1e >= i2e): + conflicts.append(intervals[i]) + break; + return conflicts + +def intervals_as_string(intervals): + out = "[ " + sep = "" + for interval in intervals: + out += f"{sep}({interval[0]},{interval[1]})" + sep = ", " + out += " ]" + return out + +intervals = parse_intervals(sys.argv[1:]) +conflicts = find_conflicts(intervals) +print(intervals_as_string(conflicts)) |
