diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-10-10 15:00:30 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-10-10 15:00:30 +0100 |
| commit | e873d0545682cb3304b90ca9b153c1f3e1fbd78f (patch) | |
| tree | 37b9b8c81c49014f662f97ff8f86ab2f704e692c | |
| parent | 22566e43bfb9dc2f516422abdeeb8baf55829daf (diff) | |
| parent | cf10a8bfbe66603d7efb6626d7a783662bcb6098 (diff) | |
| download | perlweeklychallenge-club-e873d0545682cb3304b90ca9b153c1f3e1fbd78f.tar.gz perlweeklychallenge-club-e873d0545682cb3304b90ca9b153c1f3e1fbd78f.tar.bz2 perlweeklychallenge-club-e873d0545682cb3304b90ca9b153c1f3e1fbd78f.zip | |
Merge pull request #8845 from mattneleigh/pwc238
Pwc238
| -rwxr-xr-x | challenge-238/mattneleigh/perl/ch-1.pl | 57 | ||||
| -rwxr-xr-x | challenge-238/mattneleigh/perl/ch-2.pl | 100 |
2 files changed, 157 insertions, 0 deletions
diff --git a/challenge-238/mattneleigh/perl/ch-1.pl b/challenge-238/mattneleigh/perl/ch-1.pl new file mode 100755 index 0000000000..cf5adb2a84 --- /dev/null +++ b/challenge-238/mattneleigh/perl/ch-1.pl @@ -0,0 +1,57 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @integer_lists = ( + [ 1, 2, 3, 4, 5 ], + [ 1, 1, 1, 1, 1 ], + [ 0, -1, 1, 2 ] +); + +print("\n"); +foreach my $integer_list (@integer_lists){ + printf( + "Input: \@int = (%s)\nOutput: (%s)\n\n", + join(", ", @{$integer_list}), + join(", ", running_sum(@{$integer_list})) + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Calculate the running sum of a sequence of integers- that is to say, produce +# a list of sums such that: $sums[$i] = $nums[0] + $nums[1] + ... + $nums[$i] +# Takes one argument: +# * A list of integers (e.g. ( 1, 1, 1, 1, 1 ) ) +# Returns: +# * A list of running sums of the supplied integers (e.g. ( 1, 2, 3, 4, 5 ) ) +################################################################################ +sub running_sum{ + + my $sum = 0; + + return( + # Add each number to the sum, with each + # sum going into the list to be returned + map( + $sum += $_, + @ARG + ) + ); + +} + + + diff --git a/challenge-238/mattneleigh/perl/ch-2.pl b/challenge-238/mattneleigh/perl/ch-2.pl new file mode 100755 index 0000000000..fc9d702c9a --- /dev/null +++ b/challenge-238/mattneleigh/perl/ch-2.pl @@ -0,0 +1,100 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @integer_lists = ( + [ 15, 99, 1, 34 ], + [ 50, 25, 33, 22 ] +); + +print("\n"); +foreach my $integer_list (@integer_lists){ + printf( + "Input: \@int = (%s)\nOutput: (%s)\n\n", + join(", ", @{$integer_list}), + join(", ", digit_product_steps_sort(@{$integer_list})) + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Sort a list of integers in ascending order according to the number of +# iterations required to reduce each to a single digit by calculating the +# product of each digit within the number, then repeating the process through +# successive products until only one digit remains; if two or more numbers +# require the same number of steps, they will be arranged in ascending order +# according to their original values +# Takes one argument: +# * A list of integers to examine (e.g. ( 50, 25, 33, 22 ) ) +# Returns: +# * The provided integers, sorted as described above (e.g. ( 22, 33, 50, 25 ) ) +################################################################################ +sub digit_product_steps_sort{ + + my @num_steps; + + # Loop over each argument + foreach my $num (@ARG){ + my $steps = 0; + my $iterated_num = $num; + + # In case there's a negative + $iterated_num *= -1 + if($iterated_num < 0); + + # Loop until there's only one digit in the + # iterated copy of the number + while(length($iterated_num) > 1){ + my $product = 1; + + # Multiply the digits + foreach my $digit (split('', $iterated_num)){ + $product *= $digit; + } + + # Update the iterated number, count this step + $iterated_num = $product; + $steps++; + } + + # Store the original number and the steps + # required to process it + push(@num_steps, [ $num, $steps ]); + } + + return( + # 2: Make a list of the original values + # from the now-sorted results + map( + $_->[0], + # 1: Sort the results: if the step counts + # are equal, sort by original value; + # otherwise sort by step count + sort( + { + $a->[1] == $b->[1] ? + $a->[0] <=> $b->[0] + : + $a->[1] <=> $b->[1]; + } + @num_steps + ) + ) + ); + +} + + + |
