aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-03-08 10:42:27 +0000
committerGitHub <noreply@github.com>2020-03-08 10:42:27 +0000
commitc59fee98f356a5b46866aee489469cd1dbff32cb (patch)
tree4fb92b416f9633f1a988d3b79578a2ce2089295d
parentbeb86a874ae03d3d898affc70d92a649edc48aba (diff)
parent7a5f32f7e4047317cb9fd0791faa17b385a51b48 (diff)
downloadperlweeklychallenge-club-c59fee98f356a5b46866aee489469cd1dbff32cb.tar.gz
perlweeklychallenge-club-c59fee98f356a5b46866aee489469cd1dbff32cb.tar.bz2
perlweeklychallenge-club-c59fee98f356a5b46866aee489469cd1dbff32cb.zip
Merge pull request #1363 from phillip-harris/branch-050
My attempts at challenge 50
-rw-r--r--challenge-050/phillip-harris/perl/ch-1.pl45
-rw-r--r--challenge-050/phillip-harris/perl/ch-2.pl23
2 files changed, 68 insertions, 0 deletions
diff --git a/challenge-050/phillip-harris/perl/ch-1.pl b/challenge-050/phillip-harris/perl/ch-1.pl
new file mode 100644
index 0000000000..3e41f36017
--- /dev/null
+++ b/challenge-050/phillip-harris/perl/ch-1.pl
@@ -0,0 +1,45 @@
+#!/usr/env/perl
+# Task 1 Challenge 050 Solution by phillip-harris
+# 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]
+# This seemed so easy at first but took me longer than I care to admit
+
+use strict;
+use Data::Dumper;
+
+my @in =
+ ( [ 2, 7 ], [ 3, 9 ], [ 10, 12 ], [ 15, 19 ], [ 18, 22 ] );
+
+for ( my $x = 0 ; $x <= $#in ; $x++ ) {
+ for ( my $y = 0 ; $y <= $#in ; $y++ ) {
+ if ( $y == $x ) { next }
+ my $s1 = $in[$x][0];
+ my $e1 = $in[$x][1];
+ my $s2 = $in[$y][0];
+ my $e2 = $in[$y][1];
+ my $splice;
+ my $target;
+ if ( ( $s2 >= $s1 and $s2 <= $e1 )
+ or ( $e2 >= $s1 and $e2 <= $e1 )
+ or ( $s2 <= $s1 and $e2 >= $s1 ) )
+ {
+ my @sort = sort { $a <=> $b } ( $s1, $e1, $s2, $e2 );
+ if ( $y > $x ) { $splice = $y, $target = $x }
+ if ( $y < $x ) { $splice = $x, $target = $y }
+ my $temp = splice( @in, $splice, 1 );
+
+ #print "splice $splice [@$temp[0],@$temp[1]] -> $target [$sort[0],$sort[3]]\n";
+ $in[$target][0] = $sort[0];
+ $in[$target][1] = $sort[3];
+ $y--;
+ }
+ }
+}
+print Dumper(@in);
diff --git a/challenge-050/phillip-harris/perl/ch-2.pl b/challenge-050/phillip-harris/perl/ch-2.pl
new file mode 100644
index 0000000000..837fac9f0e
--- /dev/null
+++ b/challenge-050/phillip-harris/perl/ch-2.pl
@@ -0,0 +1,23 @@
+#!/usr/env/perl
+# Task 2 Challenge 050 Solution by phillip-harris
+# 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.
+
+@L = sort { $a <=> $b } ( 2, 6, 1, 3 );
+for ( $pos = 0 ; $pos <= $#L ; $pos++ ) {
+ if ( $L[$pos] == $#L - $pos ) {
+ print $L[$pos] . "\n";
+ }
+}
+