diff options
| author | Matthew Neleigh <matthew.neleigh@gmail.com> | 2024-05-07 00:47:54 -0400 |
|---|---|---|
| committer | Matthew Neleigh <matthew.neleigh@gmail.com> | 2024-05-07 00:47:54 -0400 |
| commit | c2d2e15610b9834dd74bc99076a14710f32610a4 (patch) | |
| tree | 0cd608cfd05ecab6db61f9149407b2d34529ba55 | |
| parent | c1756b0e7aed0ad70fa63feb2565c69215c9d426 (diff) | |
| download | perlweeklychallenge-club-c2d2e15610b9834dd74bc99076a14710f32610a4.tar.gz perlweeklychallenge-club-c2d2e15610b9834dd74bc99076a14710f32610a4.tar.bz2 perlweeklychallenge-club-c2d2e15610b9834dd74bc99076a14710f32610a4.zip | |
new file: challenge-268/mattneleigh/perl/ch-1.pl
new file: challenge-268/mattneleigh/perl/ch-2.pl
| -rwxr-xr-x | challenge-268/mattneleigh/perl/ch-1.pl | 76 | ||||
| -rwxr-xr-x | challenge-268/mattneleigh/perl/ch-2.pl | 59 |
2 files changed, 135 insertions, 0 deletions
diff --git a/challenge-268/mattneleigh/perl/ch-1.pl b/challenge-268/mattneleigh/perl/ch-1.pl new file mode 100755 index 0000000000..4ae4be481b --- /dev/null +++ b/challenge-268/mattneleigh/perl/ch-1.pl @@ -0,0 +1,76 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @data_sets = ( + [ + [ 3, 7, 5 ], + [ 9, 5, 7 ] + ], + [ + [ 1, 2, 1 ], + [ 5, 4, 4 ] + ], + [ + [ 2 ], + [ 5 ] + ] +); + +print("\n"); +foreach my $data_set (@data_sets){ + printf( + "Input: \@x = (%s)\n \@y = (%s)\nOutput: %d\n\n", + join(", ", @{$data_set->[0]}), + join(", ", @{$data_set->[1]}), + find_magic_number($data_set) + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Determine the magic number that separates two arrays of numbers, each of +# which must differ from some element in the other by the same amount, though +# the particular element with which it differs by that amount is not required +# to have the same index within the other array +# Takes one argument: +# * A ref to an array of two arrays of integers, each of which differs from +# some element in the other by the same value (e.g. +# [ +# [ 3, 7, 5 ], +# [ 9, 5, 7 ] +# ] +# ) +# Returns: +# * The value by which each element of each array differs from some element in +# the other (e.g. 2 ) +################################################################################ +sub find_magic_number{ + + # Sort the two arrays, grab the first element of + # each, and return the absolute value of the + # difference between them + return( + abs( + (sort({ $a <=> $b } @{$ARG[0][0]}))[0] + - + (sort({ $a <=> $b } @{$ARG[0][1]}))[0] + ) + ); + +} + + + diff --git a/challenge-268/mattneleigh/perl/ch-2.pl b/challenge-268/mattneleigh/perl/ch-2.pl new file mode 100755 index 0000000000..b9173990bc --- /dev/null +++ b/challenge-268/mattneleigh/perl/ch-2.pl @@ -0,0 +1,59 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @integer_lists = ( + [ 2, 5, 3, 4 ], + [ 9, 4, 1, 3, 6, 4, 6, 1 ], + [ 1, 2, 2, 3 ] +); + +print("\n"); +foreach my $integer_list (@integer_lists){ + printf( + "Input: \@ints = (%s)\nOutput: (%s)\n\n", + join(", ", @{$integer_list}), + join(", ", transposed_sort(@{$integer_list})) + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Rearrange a list of integers with an even number of elements such that it is +# sorted in ascending order, except that each apair within the list is +# transposed +# Takes one argument: +# * A list of integers with an even number of elements (e.g. ( 2, 5, 3, 4 ) ) +# Returns: +# * A rearranged copy of the list (e.g. ( 3, 2, 5, 4 ) ) +################################################################################ +sub transposed_sort{ + my @ints = sort({ $a <=> $b } @ARG); + + my @rearranged; + + while(@ints){ + # Grab a the first two remaining elements from the + # sorted argument list, and add them to the + # rearranged list in reversed order + push(@rearranged, (splice(@ints, 0, 2))[1, 0]); + } + + return(@rearranged); + +} + + + |
