diff options
| -rw-r--r-- | challenge-050/ianrifkin/perl/ch-1.pl | 31 | ||||
| -rw-r--r-- | challenge-050/ianrifkin/perl/ch-2.pl | 40 |
2 files changed, 71 insertions, 0 deletions
diff --git a/challenge-050/ianrifkin/perl/ch-1.pl b/challenge-050/ianrifkin/perl/ch-1.pl new file mode 100644 index 0000000000..045e51fca1 --- /dev/null +++ b/challenge-050/ianrifkin/perl/ch-1.pl @@ -0,0 +1,31 @@ +#!/usr/bin/perl +use strict; +use feature qw( say ); + + +# Perl Weekly Challenge 050 - Task 1 +# Write a script to merge the given intervals where ever possible. +# [2,7], [3,9], [10,12], [15,19], [18,22] + +# Assumption: Input numbers, similarlily to example are in order: +# - the first number in a pair is <= the first number in the next pair +# - the second number in a pair is <= the second number in the next pair +# Solution by Ian Rifkin + +my @numbers = ( [2,7], [3,9], [10,12], [15,19], [18,22] ); + +for (my $i=0; $i < scalar @numbers-1; $i++) { + if ($numbers[$i][1] >= $numbers[$i+1][0] && $numbers[$i][1] <= $numbers[$i+1][1]) { + $numbers[$i][1] = $numbers[$i+1][1]; + splice(@numbers, $i+1, 1); + $i--; #loop through to check if new merged interval should also merge with the next one + } +} + +###### Print Output ####### +say "\nMerged interval output:"; +foreach my $number_pair (@numbers) { + print "[$$number_pair[0],$$number_pair[1]]"; + print ", " unless $number_pair == $numbers[-1]; +} +print "\n\n"; diff --git a/challenge-050/ianrifkin/perl/ch-2.pl b/challenge-050/ianrifkin/perl/ch-2.pl new file mode 100644 index 0000000000..35c7f2d21a --- /dev/null +++ b/challenge-050/ianrifkin/perl/ch-2.pl @@ -0,0 +1,40 @@ +#!/usr/bin/perl +use strict; +use feature qw( say ); + + +# Perl Weekly Challenge 050 - Task 2 + +# 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. + +# 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 + +# Solution by Ian Rifkin + + +my @L = (2, 6, 1, 3); #List of number inputs + +# Initially I was going to solve with a nested loop +# Which is definitely how my mind defaults +# e.g. my $count increments whenever it finds a number greater than $i +# going to next in loop whenever it gets a count > $num +# and output $num whenever == count +# This works great but then I started brainstorming a more fun solution... + +# If the number list is sorted then instead of checking how a number is equal to another number +# we can simply count how many numbers are ahead of it in the list + +# Assumption: The same number won't be in the list more than once +# Though I suppose I could splice out dupes if that assumption isn't true + +@L = sort { $a <=> $b } @L; #Sort numerically +# If assumption was untrue, splice dupes here? +my $length = scalar @L; +for (my $pos = 0; $pos < $length; $pos++) { + #found a noble integer if it's value is equal to the amount of numbers after it + say "Noble integer found: $L[$pos]" if $L[$pos] == $length - $pos - 1; +} |
