diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-01-16 20:05:43 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-01-16 20:05:43 +0000 |
| commit | 8512e57085695a3ebec76f51d65f217e0dbf0077 (patch) | |
| tree | 057725521538864b2dd78a9853269e0c72ee106e | |
| parent | fcfba54740e333884b35285e04781121e3ca20d8 (diff) | |
| parent | b1e13994b93e4d4056bfd3fe806b01ec0107558a (diff) | |
| download | perlweeklychallenge-club-8512e57085695a3ebec76f51d65f217e0dbf0077.tar.gz perlweeklychallenge-club-8512e57085695a3ebec76f51d65f217e0dbf0077.tar.bz2 perlweeklychallenge-club-8512e57085695a3ebec76f51d65f217e0dbf0077.zip | |
Merge pull request #9417 from mattneleigh/pwc252
new file: challenge-252/mattneleigh/perl/ch-1.pl
| -rwxr-xr-x | challenge-252/mattneleigh/perl/ch-1.pl | 59 | ||||
| -rwxr-xr-x | challenge-252/mattneleigh/perl/ch-2.pl | 73 |
2 files changed, 132 insertions, 0 deletions
diff --git a/challenge-252/mattneleigh/perl/ch-1.pl b/challenge-252/mattneleigh/perl/ch-1.pl new file mode 100755 index 0000000000..71e1afcac9 --- /dev/null +++ b/challenge-252/mattneleigh/perl/ch-1.pl @@ -0,0 +1,59 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @integer_lists = ( + [ 1, 2, 3, 4 ], + [ 2, 7, 1, 19, 18, 3 ] +); + +print("\n"); +foreach my $integer_list (@integer_lists){ + printf( + "Input: \@ints = (%s)\nOutput: %d\n\n", + join(", ", @{$integer_list}), + sum_squares_special_elements(@{$integer_list}) + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Given a list of integers, calculate the sum of the squares of all Special +# Elements within the array, where Special Elements are those at positions in +# which their 1-indexed, er, index evenly divides into N where N is the number +# of elements in the list (i.e. I => (1 .. N) and N % I == 0 ) +# Takes one argument: +# * The list of integers to examine (e.g. ( 2, 7, 1, 19, 18, 3 ) ) +# Returns: +# * The sum of the suqares of the special elements, as defined above (e.g. 63 ) +################################################################################ +sub sum_squares_special_elements{ + + my $sum = 0; + + # Loop from 1 to N + 1 + for my $i (1 .. $#ARG + 1){ + # Add the sum of the square of this value if + # its adjusted index is Special + $sum += $ARG[$i - 1] ** 2 + unless(scalar(@ARG) % $i); + } + + return($sum); + +} + + + diff --git a/challenge-252/mattneleigh/perl/ch-2.pl b/challenge-252/mattneleigh/perl/ch-2.pl new file mode 100755 index 0000000000..62efd1b91a --- /dev/null +++ b/challenge-252/mattneleigh/perl/ch-2.pl @@ -0,0 +1,73 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @integers = ( + # Given cases + 5, 3, 1, + + # Additional test cases + 4, 0 +); + +print("\n"); +foreach my $integer (@integers){ + printf( + "Input: \$n = %d\nOutput: (%s)\n\n", + $integer, + join(", ", create_zero_sum_array($integer)) + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Given a positive integer $n, return an array of $n unique integers that all +# add up to zero +# Takes one argument: +# * A positive integer $n (e.g. 4) +# Returns: +# * A list of $n integers that all add up to zero (e.g. ( -1, -2, 2, 1 ) ) +################################################################################ +sub create_zero_sum_array{ + my $count = shift(); + + my @ints = (); + + # See if we have an odd number to deal with, and + # if so, add a zero to the list, then decrement + # the count + if($count % 2){ + push(@ints, 0); + $count--; + } + + # Divide the count by two (it will be even now) + # then loop until it's decremented to zero + $count /= 2; + while($count){ + # Add the current count value, and its negative, + # to opposite ends of the array, and decrement + # it + push(@ints, $count); + unshift(@ints, -$count); + $count--; + } + + return(@ints); + +} + + + |
