diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2022-04-12 16:03:27 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2022-04-12 16:03:27 +0100 |
| commit | 2f4f8ac7670d802ff5a5bb76d0d3294725c59603 (patch) | |
| tree | 13625eac1578aa4e86f620df52b9823a6d3c17f2 /challenge-050 | |
| parent | 4da3eb91ec0f729447e5d0819168eb411bff2cd1 (diff) | |
| download | perlweeklychallenge-club-2f4f8ac7670d802ff5a5bb76d0d3294725c59603.tar.gz perlweeklychallenge-club-2f4f8ac7670d802ff5a5bb76d0d3294725c59603.tar.bz2 perlweeklychallenge-club-2f4f8ac7670d802ff5a5bb76d0d3294725c59603.zip | |
Add Perl solution to challenge 050
Diffstat (limited to 'challenge-050')
| -rw-r--r-- | challenge-050/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-050/paulo-custodio/README | 1 | ||||
| -rw-r--r-- | challenge-050/paulo-custodio/perl/ch-1.pl | 52 | ||||
| -rw-r--r-- | challenge-050/paulo-custodio/perl/ch-2.pl | 38 | ||||
| -rw-r--r-- | challenge-050/paulo-custodio/t/test-1.yaml | 5 | ||||
| -rw-r--r-- | challenge-050/paulo-custodio/t/test-2.yaml | 10 |
6 files changed, 108 insertions, 0 deletions
diff --git a/challenge-050/paulo-custodio/Makefile b/challenge-050/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-050/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-050/paulo-custodio/README b/challenge-050/paulo-custodio/README new file mode 100644 index 0000000000..87dc0b2fbd --- /dev/null +++ b/challenge-050/paulo-custodio/README @@ -0,0 +1 @@ +Solution by Paulo Custodio diff --git a/challenge-050/paulo-custodio/perl/ch-1.pl b/challenge-050/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..86e7fbbb3f --- /dev/null +++ b/challenge-050/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,52 @@ +#!/usr/bin/env perl + +# Challenge 050 +# +# TASK #1 +# Merge Intervals +# Write a script to merge the given intervals where ever possible. +# +# [2,7], [3,9], [10,12], [15,19], [18,22] +# +# The script should merge [2, 7] and [3, 9] together to return [2, 9]. +# +# Similarly it should also merge [15, 19] and [18, 22] together to return +# [15, 22]. +# +# The final result should be something like below: +# +# [2, 9], [10, 12], [15, 22] + +use Modern::Perl; +use List::Util qw( max ); + +my @int; +while (my($a, $b) = splice(@ARGV, 0, 2)) { + push @int, [$a, $b]; +} + +@int = merge_intervals(@int); + +my @out = map {"[".$_->[0].", ".$_->[1]."]"} @int; +say join(", ", @out); + + +sub merge_intervals { + my(@int) = @_; + + # sort by start time + @int = sort {$a->[0] <=> $b->[0]} @int; + + # merge consecutive intervals + for (my $i = 0; $i < @int-1; $i++) { + my $a = $int[$i]; + my $b = $int[$i+1]; + if ($a->[1] >= $b->[0]) { + $a->[1] = max($a->[1], $b->[1]); + splice(@int, $i+1, 1); + $i--; + } + } + + return @int; +} diff --git a/challenge-050/paulo-custodio/perl/ch-2.pl b/challenge-050/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..8d67fa32c3 --- /dev/null +++ b/challenge-050/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,38 @@ +#!/usr/bin/env perl + +# Challenge 050 +# +# TASK #2 +# Contributed by Ryan Thompson. +# Noble Integer +# You are given a list, @L, of three or more random integers between 1 and 50. +# A Noble Integer is an integer N in @L, such that there are exactly N integers +# greater than N in @L. Output any Noble Integer found in @L, or an empty list +# if none were found. +# +# An interesting question is whether or not there can be multiple Noble Integers +# in a list. +# +# For example, +# +# Suppose we have list of 4 integers [2, 6, 1, 3]. +# +# Here we have 2 in the above list, known as Noble Integer, since there are +# exactly 2 integers in the list i.e.3 and 6, which are greater than 2. +# +# Therefore the script would print 2. + +use Modern::Perl; + +say "(",join(", ", noble_int(@ARGV)),")"; + + +sub noble_int { + my(@n) = @_; + my @out; + for my $n (@n) { + my $num_greater = scalar(grep {$_>$n} @n); + push @out, $n if $num_greater==$n; + } + return @out; +} diff --git a/challenge-050/paulo-custodio/t/test-1.yaml b/challenge-050/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..3adf6165f9 --- /dev/null +++ b/challenge-050/paulo-custodio/t/test-1.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: 2 7 3 9 10 12 15 19 18 22 + input: + output: [2, 9], [10, 12], [15, 22] diff --git a/challenge-050/paulo-custodio/t/test-2.yaml b/challenge-050/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..f85f70c305 --- /dev/null +++ b/challenge-050/paulo-custodio/t/test-2.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: 2 6 1 3 + input: + output: (2) +- setup: + cleanup: + args: 2 6 1 + input: + output: () |
