From 16db0546109fb4abdb625272ebe379dbd2f9b72f Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Sat, 28 Aug 2021 13:39:32 +0200 Subject: Challenge 127 Task 1 2 Perl Python LK --- challenge-127/lubos-kolouch/perl/ch-1.pl | 40 +++++++++++++++++++++++++++ challenge-127/lubos-kolouch/perl/ch-2.pl | 44 ++++++++++++++++++++++++++++++ challenge-127/lubos-kolouch/python/ch-1.py | 22 +++++++++++++++ challenge-127/lubos-kolouch/python/ch-2.py | 34 +++++++++++++++++++++++ 4 files changed, 140 insertions(+) create mode 100644 challenge-127/lubos-kolouch/perl/ch-1.pl create mode 100644 challenge-127/lubos-kolouch/perl/ch-2.pl create mode 100644 challenge-127/lubos-kolouch/python/ch-1.py create mode 100644 challenge-127/lubos-kolouch/python/ch-2.py diff --git a/challenge-127/lubos-kolouch/perl/ch-1.pl b/challenge-127/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..8327f310f6 --- /dev/null +++ b/challenge-127/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,40 @@ +#!/usr/bin/perl +#=============================================================================== +# +# FILE: ch-1.pl +# +# USAGE: ./ch-1.pl +# +# DESCRIPTION: The Weekly Challenge #127 +# Task 1 - Disjoint Sets +# +# AUTHOR: Lubos Kolouch +# CREATED: 08/27/2021 11:45:07 AM +#=============================================================================== + +use strict; +use warnings; + + +# Why Perl does not have a set type like Python? +# then it is an one-liner using set unions... + +sub compare_sets { + my $what = shift; + + my %h1 = map{ $_ => 1} @{$what->[0]}; + my %h2 = map{ $_ => 1} @{$what->[1]}; + + for my $key (keys %h1) { + return 0 if $h2{$key}; + } + + return 1; +} + +use Test::More; +is(compare_sets([[1, 2, 3], [3, 2, 1]]), 0); +is(compare_sets([[1, 2, 5, 3, 4], [4, 6, 7, 8, 9]]), 0); +is(compare_sets([[1, 3, 5, 7, 9], [0, 2, 4, 6, 8]]), 1); +done_testing(); + diff --git a/challenge-127/lubos-kolouch/perl/ch-2.pl b/challenge-127/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..3118d1a0d9 --- /dev/null +++ b/challenge-127/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,44 @@ +#!/usr/bin/perl +#=============================================================================== +# +# FILE: ch-2.pl +# +# USAGE: ./ch-2.pl +# +# DESCRIPTION: The Weekly Challenge #127 +# Task 2 - Conflict Intervals +# +# AUTHOR: Lubos Kolouch +# CREATED: 08/27/2021 11:45:07 AM +#=============================================================================== + +use strict; +use warnings; + + +sub check_intervals { + my $what = shift; + + my @intervals; + my @result; + for my $interval (@$what) { + for my $seen_interval (@intervals) { + if ( + (($interval->[1]> $seen_interval->[0]) and ($interval->[0] <= $seen_interval->[0])) or + (($interval->[0] < $seen_interval->[1]) and ($interval->[0] >= $seen_interval->[0])) + ) { + push @result, $interval; + last; + } + } + + push @intervals, $interval; + } + + return \@result; +} + +use Test::More; +is_deeply(check_intervals([ [1,4], [3,5], [6,8], [12, 13], [3,20] ]), [ [3,5], [3,20] ]); +is_deeply(check_intervals([ [3,4], [5,7], [6,9], [10, 12], [13,15] ]), [ [6,9] ]); +done_testing(); diff --git a/challenge-127/lubos-kolouch/python/ch-1.py b/challenge-127/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..04e270ead2 --- /dev/null +++ b/challenge-127/lubos-kolouch/python/ch-1.py @@ -0,0 +1,22 @@ +#!python +# =============================================================================== +# +# FILE: ch-1.py +# +# USAGE: ./ch-1.py +# +# DESCRIPTION: The Weekly Challenge #127 +# Task 1 - Disjoint Sets +# +# AUTHOR: Lubos Kolouch +# CREATED: 08/27/2021 11:45:07 AM +# =============================================================================== + +def compare_sets(set1: set, set2: set): + """ Use the set arithmetics to provide the answer """ + return not set1 & set2 + + +assert compare_sets({1, 2, 3}, {3, 2, 1}) == 0 +assert compare_sets({1, 2, 5, 3, 4}, {4, 6, 7, 8, 9}) == 0 +assert compare_sets({1, 3, 5, 7, 9}, {0, 2, 4, 6, 8}) == 1 diff --git a/challenge-127/lubos-kolouch/python/ch-2.py b/challenge-127/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..f409af416b --- /dev/null +++ b/challenge-127/lubos-kolouch/python/ch-2.py @@ -0,0 +1,34 @@ +#!python +# =============================================================================== +# +# FILE: ch-2.py +# +# USAGE: ./ch-2.py +# +# DESCRIPTION: The Weekly Challenge #127 +# Task 2 - Conflict Intervals +# +# AUTHOR: Lubos Kolouch +# CREATED: 08/27/2021 11:45:07 AM +# =============================================================================== + +def check_intervals(what): + """ Check the intervals overlap """ + + intervals = [] + result = [] + + for interval in what: + for seen_interval in intervals: + if ((interval[1] > seen_interval[0]) and (interval[0] <= seen_interval[0])) or \ + ((interval[0] < seen_interval[1]) and (interval[0] >= seen_interval[0])): + result.append(interval) + break + + intervals.append(interval) + + return result + + +assert check_intervals([[1, 4], [3, 5], [6, 8], [12, 13], [3, 20]]) == [[3, 5], [3, 20]] +assert check_intervals([[3, 4], [5, 7], [6, 9], [10, 12], [13, 15]]) == [[6, 9]] -- cgit