diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-03-03 12:48:34 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-03-03 12:48:34 +0000 |
| commit | 018efded5ff02e4dd01669117f425703b2b3a1ce (patch) | |
| tree | 76b65b8106d3d58d5ebefdaa3698bd11d95f86db | |
| parent | 114996d7c4bb93490d0c65af45f51567d4b0dad7 (diff) | |
| parent | 9e68721a1be2df8124b1cabc10d0ef6a81c3eb0d (diff) | |
| download | perlweeklychallenge-club-018efded5ff02e4dd01669117f425703b2b3a1ce.tar.gz perlweeklychallenge-club-018efded5ff02e4dd01669117f425703b2b3a1ce.tar.bz2 perlweeklychallenge-club-018efded5ff02e4dd01669117f425703b2b3a1ce.zip | |
Merge pull request #1353 from jacoby/master
Challenge 50
| -rw-r--r-- | challenge-050/dave-jacoby/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-050/dave-jacoby/perl/ch-1.pl | 53 | ||||
| -rw-r--r-- | challenge-050/dave-jacoby/perl/ch-2.pl | 70 |
3 files changed, 124 insertions, 0 deletions
diff --git a/challenge-050/dave-jacoby/blog.txt b/challenge-050/dave-jacoby/blog.txt new file mode 100644 index 0000000000..4018d13d2a --- /dev/null +++ b/challenge-050/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby.github.io/2020/03/02/perl-challenge-50.html
\ No newline at end of file diff --git a/challenge-050/dave-jacoby/perl/ch-1.pl b/challenge-050/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..e51166bce0 --- /dev/null +++ b/challenge-050/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,53 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use utf8; +use feature qw{ postderef say signatures state switch }; +no warnings + qw{ experimental::postderef experimental::smartmatch experimental::signatures }; + +use JSON; +my $json = JSON->new; + +my @array = ( [ 2, 7 ], [ 3, 9 ], [ 10, 12 ], [ 15, 19 ], [ 18, 22 ] ); + +# unnecessary in THIS case, but if we take on abstract +# two-dimensional array, we'll have to enforce order + +@array = sort { $a->[0] <=> $b->[0] } @array; +say $json->encode( \@array ); + +LOOP: while (1) { + for my $i ( 0 .. scalar @array - 1 ) { + my @i = $array[$i]->@*; + for my $j ( $i + 1 .. scalar @array - 1 ) { + my @j = $array[$j]->@*; + + if ( $i[0] <= $j[0] && $i[1] >= $j[0] ) { + $array[$i][1] = int $j[1]; + undef $array[$j]; + @array = grep { defined } @array; + next LOOP; + } + } + } + say $json->encode( \@array ); + exit; +} + +__DATA__ + +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] diff --git a/challenge-050/dave-jacoby/perl/ch-2.pl b/challenge-050/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..9961e500c6 --- /dev/null +++ b/challenge-050/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,70 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use utf8; +use feature qw{ postderef say signatures state switch }; +no warnings + qw{ experimental::postderef experimental::smartmatch experimental::signatures }; + +use List::Util qw{ uniq }; + +my @L = map { 1 + int rand 50 } 1 .. 3 + int rand 20; +@L = ( 2, 2, 6, 1, 3 ); +say join ' ', @L; +my @n = nobles(@L); +say join ' ', scalar @n ? @n : 'none'; +exit; + +# Commentary: +# * "random" is perhaps not a good choice for input, because +# it becomes increasingly unlikely that a noble integer +# exists for the set. +# +# * I don't believe a second noble number can exist in a +# set. Let's take @L as an example. Those numbers are in some +# order, but we're talking about them as greater than, so +# we sort them: +# +# 1, 2, 3, 6 +# +# As the integer grows, the number of remaining integers shrinks. +# +# 1 -> 3 integers greater than +# 2 -> 2 integers greater than (WINNER!) +# 3 -> 1 integer greater than +# 6 -> no integers greater than +# +# * if we repeat an integer, like [2, 2, 6, 1, 3] or [6, 2, 6, 1, 3], +# I am counting each number once -- there are still two integers greater +# than 2; 3 and 6, not 3, 6 and 6 -- but returning all the copies of the +# integer that count as noble numbers -- 2, 2 in the first example. + +sub nobles ( @list ) { + my @copy = @list; + @list = uniq sort { $a <=> $b } @list; + my @output; + while (@list) { + my $i = shift @list; + my @i = grep { $_ == $i } @copy; + push @output, @i if $i == scalar @list; + } + return @output; +} + +__DATA__ + +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. |
