diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-05-07 00:10:22 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-05-07 00:10:22 +0100 |
| commit | c8f961a0db183e61cd2e105448ccbd90ec0e62d4 (patch) | |
| tree | e82a411c1028b5b346d2f3008af6bf02d6872576 | |
| parent | a52c85b31fcb08733a2892d6631e92e3194495a1 (diff) | |
| parent | cb308efd84d76ed1cb58546a2f897e65bcf38d7f (diff) | |
| download | perlweeklychallenge-club-c8f961a0db183e61cd2e105448ccbd90ec0e62d4.tar.gz perlweeklychallenge-club-c8f961a0db183e61cd2e105448ccbd90ec0e62d4.tar.bz2 perlweeklychallenge-club-c8f961a0db183e61cd2e105448ccbd90ec0e62d4.zip | |
Merge pull request #6063 from mattneleigh/pwc163
new file: challenge-163/mattneleigh/perl/ch-1.pl
| -rwxr-xr-x | challenge-163/mattneleigh/perl/ch-1.pl | 82 | ||||
| -rwxr-xr-x | challenge-163/mattneleigh/perl/ch-2.pl | 100 |
2 files changed, 182 insertions, 0 deletions
diff --git a/challenge-163/mattneleigh/perl/ch-1.pl b/challenge-163/mattneleigh/perl/ch-1.pl new file mode 100755 index 0000000000..36ed0ea48f --- /dev/null +++ b/challenge-163/mattneleigh/perl/ch-1.pl @@ -0,0 +1,82 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @sets = ( + [ 1, 2, 3 ], + [ 2, 3, 4 ] +); +my $set; + +print("\n"); + +foreach $set (@sets){ + my @pairs = unique_pairs_from_list(@{$set}); + my $sum = 0; + my $shown_work; + + # Do the math and document it; this will be + # shared later... + $shown_work = sprintf( + "Since %s => %s => %d", + join(" + ", map("(".$_->[0]." & ".$_->[1].")", @pairs)), + join( + " + ", + map( + # Where the actual math happens... and also + # some output generation + { my $a = ($_->[0] & $_->[1]); $sum += $a; $a; } + @pairs + ) + ), + $sum + ); + + printf("Input: \@n = (%s)\n", join(", ", @{$set})); + printf("Output: %d\n\n", $sum); + print("$shown_work\n\n\n"); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Generate a sequence of unique pairs of items from a list +# Takes one argument: +# * The list of things to group into pairs, which can contain any set of scalar +# values (e.g. (1, 2, 3) ) +# Returns on success: +# * A list of array refs, each of which refers to a pair of values from the +# original list (e.g. ( [ 1, 2 ], [ 1, 3 ], [ 2, 3 ] ) ) +# Returns on error +# * undef if the list contains fewer than two elements (no pairs are possible) +################################################################################ +sub unique_pairs_from_list{ + + return(undef) + unless(scalar(@ARG) > 1); + + my @pairs = (); + + for my $i (0 .. ($#ARG - 1)){ + for my $j (($i + 1) .. $#ARG){ + push(@pairs, [$ARG[$i], $ARG[$j]]); + } + } + + return(@pairs); + +} + + + diff --git a/challenge-163/mattneleigh/perl/ch-2.pl b/challenge-163/mattneleigh/perl/ch-2.pl new file mode 100755 index 0000000000..f59bd349dd --- /dev/null +++ b/challenge-163/mattneleigh/perl/ch-2.pl @@ -0,0 +1,100 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +use POSIX; + +################################################################################ +# Begin main execution +################################################################################ + +my @lists = ( + # Given cases + [ 1, 2, 3, 4, 5 ], + [ 1, 3, 5, 7, 9 ], + + # Additional test cases + [ 2, 4, 50, 83, 6, 5, 9 ] +); +my $list; + +print("\n"); +foreach $list (@lists){ + my $listref = []; + my $digits; + my $summation = compute_list_summation($listref, @{$list}); + + printf( + "\nInput: \@n = (%s)\nOutput: %d\n\n", + join(", ", @{$list}), + $summation + ); + + # See how many digits we'll need to handle... + $digits = ceil(log($summation) / log(10)); + + # Stick the input list at the head of the list of + # summation stages and output them in a pretty + # fashion + unshift(@{$listref}, $list); + foreach(@{$listref}){ + printf( + "%".(4 + scalar(@{$list}) * ($digits + 1))."s\n", + join(" ", map(sprintf("%".$digits."d", $_), @{$_})) + ); + } + print("\n"); + +} + +print("\n"); + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Perform a particular type of list summation on a list of numbers +# Takes two arguments: +# * An array reference for storage of lists from intermediate summation stages, +# if desired; if this argument is 'undef', no intermediate lists will be +# stored +# * The list of numbers to process +# Returns: +# * The final value that results from the summation process +################################################################################ +sub compute_list_summation{ + my $global_listref = shift(); + + my @stage_list; + + # Loop over the last N-1 numbers in the list + # and add the running total to a new list + shift(); + push(@stage_list, $ARG[0]); + for my $i (1 .. $#ARG){ + push(@stage_list, $stage_list[$#stage_list] + $ARG[$i]); + } + + # Store the list if requested to do so + push(@{$global_listref}, \@stage_list) + if(defined($global_listref)); + + # If there was just one sum generated, return + # it + return($stage_list[0]) + if(scalar(@stage_list) == 1); + + # Otherwise call ourselves on the sum list, + # return what the deeper call found + return(compute_list_summation($global_listref, @stage_list)); + +} + + + |
