diff options
| -rwxr-xr-x | challenge-284/mattneleigh/perl/ch-1.pl | 76 | ||||
| -rwxr-xr-x | challenge-284/mattneleigh/perl/ch-2.pl | 113 |
2 files changed, 189 insertions, 0 deletions
diff --git a/challenge-284/mattneleigh/perl/ch-1.pl b/challenge-284/mattneleigh/perl/ch-1.pl new file mode 100755 index 0000000000..fc49e486d9 --- /dev/null +++ b/challenge-284/mattneleigh/perl/ch-1.pl @@ -0,0 +1,76 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @integer_lists = ( + [ 2, 2, 3, 4 ], + [ 1, 2, 2, 3, 3, 3 ], + [ 1, 1, 1, 3 ] +); + +print("\n"); +foreach my $integer_list (@integer_lists){ + my $rval = find_lucky_integer(@{$integer_list}); + + printf( + "Input: \@ints = (%s)\nOutput: %d\n\n", + join(", ", @{$integer_list}), + defined($rval) ? + $rval + : + -1 + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Given an array of integers, find a Lucky Number within, where a Lucky Number +# is defined as a number that appears in the array a number of times equal to +# its own value. If more than one exists, the largest will be returned; if +# none exist, undef will be returned. +# Takes one argument: +# * An array of integers to examine (e.g. ( 1, 2, 2, 3, 3, 3 ) ) +# Returns on success: +# * The largest Lucky Number found within the list (e.g. in the case above, 3 ) +# Returns on error: +# * undef if no Lucky Number could be found within the array +################################################################################ +sub find_lucky_integer{ + + my $int; + my %frequency_table; + + # Count how often each integer appears in the + # array + foreach $int (@ARG){ + $frequency_table{$int}++; + } + + # Examine each integer that was seen, after + # sorting them in descending order + foreach $int (sort({ $b <=> $a } keys(%frequency_table))){ + # Return this integer if its frequency is equal + # to its own value, making it a Lucky Number + return($int) + if($frequency_table{$int} == $int); + } + + # If we got here, no lucky number was found + return(undef); + +} + + + diff --git a/challenge-284/mattneleigh/perl/ch-2.pl b/challenge-284/mattneleigh/perl/ch-2.pl new file mode 100755 index 0000000000..826139e361 --- /dev/null +++ b/challenge-284/mattneleigh/perl/ch-2.pl @@ -0,0 +1,113 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @integer_list_sets = ( + [ + [ 2, 3, 9, 3, 1, 4, 6, 7, 2, 8, 5 ], + [ 2, 1, 4, 3, 5, 6 ] + ], + [ + [ 3, 3, 4, 6, 2, 4, 2, 1, 3 ], + [ 1, 3, 2 ] + ], + [ + [ 3, 0, 5, 0, 2, 1, 4, 1, 1 ], + [ 1, 0, 3, 2 ] + ] +); + +print("\n"); +foreach my $integer_list_set (@integer_list_sets){ + printf( + "Input: \@list1 = (%s)\n \@list2 = (%s)\nOuput: (%s)\n\n", + join(", ", @{$integer_list_set->[0]}), + join(", ", @{$integer_list_set->[1]}), + join(", ", relative_order_sort($integer_list_set)) + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Given two lists of integers, a primary and a secondary- whose members must be +# distinct and also present in the primary- sort the elements in the primary +# such that their relative order is the same as their order in the secondary, +# with elements in the primary NOT present in the secondary being sorted in +# ascending order but placed after all elements that ARE present in the +# secondary +# Takes one argument: +# * A ref to an array of two arrays of integers, comprising the primary and +# secondary integer lists; all elements in the secondary list must be +# distinct and present in the primary list (e.g. +# [ +# # Primary list +# [ 2, 3, 9, 3, 1, 4, 6, 7, 2, 8, 5 ], +# # Secondary list +# [ 2, 1, 4, 3, 5, 6 ] +# ] +# ) +# Returns: +# * A list of integers sorted according to the procedure described above (e.g. +# ( 2, 2, 1, 4, 3, 3, 5, 6, 7, 8, 9 ) ) +################################################################################ +sub relative_order_sort{ + my $integer_lists = shift(); + + my $int; + my %present; + my %bucket; + my @junk_drawer; + + # Take note of where each element of the secondary + # list appears- temporarily using $int to hold + # index values + $int = 0; + %present = map( + { $_ => $int++ } + @{$integer_lists->[1]} + ); + + # Examine every integer in the primary list + foreach $int (@{$integer_lists->[0]}){ + if(defined($present{$int})){ + # This integer was in the secondary list- store + # it in the correct bucket for the matching index + # from the secondary list + push( + @{$bucket{$present{$int}}}, + $int + ); + } else{ + # This integer was NOT in the secondary list- + # store it in the junk drawer list + push(@junk_drawer, $int); + } + } + + # Combine each bucket's contents, in the correct + # order, with the sorted contents of the junk drawer + # list + return( + map( + @{$bucket{$_}}, + sort({ $a <=> $b } keys(%bucket)) + ), + sort({ $a <=> $b } @junk_drawer) + ); + +} + + + |
