diff options
| author | Packy Anderson <packy@cpan.org> | 2024-01-17 00:11:35 -0500 |
|---|---|---|
| committer | Packy Anderson <packy@cpan.org> | 2024-01-17 00:11:35 -0500 |
| commit | 29fc5f28a3b1b684f8dcf1dd95af51da468dd0ad (patch) | |
| tree | 2dce36121e6ec2c4e096b414dc1b2b7a13f50dc6 | |
| parent | 857d573334deaa24866cb695f1e3758a85c0b9d9 (diff) | |
| download | perlweeklychallenge-club-29fc5f28a3b1b684f8dcf1dd95af51da468dd0ad.tar.gz perlweeklychallenge-club-29fc5f28a3b1b684f8dcf1dd95af51da468dd0ad.tar.bz2 perlweeklychallenge-club-29fc5f28a3b1b684f8dcf1dd95af51da468dd0ad.zip | |
Challenge 252 solutions by Packy Anderson
* Raku
* Perl
* Python
1 Blog post
| -rw-r--r-- | challenge-252/packy-anderson/README.md | 2 | ||||
| -rw-r--r-- | challenge-252/packy-anderson/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-252/packy-anderson/perl/ch-1.pl | 72 | ||||
| -rwxr-xr-x | challenge-252/packy-anderson/perl/ch-2.pl | 34 | ||||
| -rwxr-xr-x | challenge-252/packy-anderson/python/ch-1.py | 69 | ||||
| -rwxr-xr-x | challenge-252/packy-anderson/python/ch-2.py | 33 | ||||
| -rwxr-xr-x | challenge-252/packy-anderson/raku/ch-1.raku | 68 | ||||
| -rwxr-xr-x | challenge-252/packy-anderson/raku/ch-2.raku | 35 |
8 files changed, 313 insertions, 1 deletions
diff --git a/challenge-252/packy-anderson/README.md b/challenge-252/packy-anderson/README.md index a796e42c76..336f14bee6 100644 --- a/challenge-252/packy-anderson/README.md +++ b/challenge-252/packy-anderson/README.md @@ -16,4 +16,4 @@ ## Blog Post -[Luck Concatenate Numbers Tonight!](https://packy.dardan.com/b/GL) +[Sum Enchanted Evening](https://packy.dardan.com/b/GW) diff --git a/challenge-252/packy-anderson/blog.txt b/challenge-252/packy-anderson/blog.txt new file mode 100644 index 0000000000..4865e5c0fb --- /dev/null +++ b/challenge-252/packy-anderson/blog.txt @@ -0,0 +1 @@ +https://packy.dardan.com/b/GW
\ No newline at end of file diff --git a/challenge-252/packy-anderson/perl/ch-1.pl b/challenge-252/packy-anderson/perl/ch-1.pl new file mode 100755 index 0000000000..576e39bec2 --- /dev/null +++ b/challenge-252/packy-anderson/perl/ch-1.pl @@ -0,0 +1,72 @@ +#!/usr/bin/env perl +use v5.38; + +use List::Util qw( sum ); + +sub specialElementIndices($n) { + return grep { $n % $_ == 0 } 1 .. $n; +} + +sub english_list ( @list ) { + # given a list, join it in a way that makes sense + # to english speakers + my $last = pop @list; # last element in array + if (@list == 0) { + # using an array in a scalar context returns + # the number of elements in the array + + # there was only one element in the list + return $last; + } + my $joined = join qq{,\n}, @list; + if (@list > 1) { + # if there's more than element, add an Oxford comma + $joined .= q{,}; + } + return "$joined and\n$last"; +} + +sub specialNumberSquareSum(@ints) { + # in scalar context, an array evaluates to the number + # of elements in the array + my $n = @ints; + + # find the list of indices for "special" numbers + my @specialIndices = specialElementIndices($n); + my $count = @specialIndices; + my @explain_list = map { + "\$ints[$_] since $_ divides $n" + } @specialIndices; + my $explain = "There are exactly $count special elements " + . "in the given array:\n" . english_list(@explain_list); + + # find the special numbers themselves + my @special = map { $ints[$_ - 1] } @specialIndices; + + # find the sum of the squares + my $sum = sum( map { $_ ** 2 } @special); + + $explain .= "\nHence, the sum of the squares of all special " + . "elements of given array:\n" + . join(' + ', map { "$_ * $_" } @special) + . " = " . $sum; + + return ( + $sum, + $explain + ); +} + +sub solution(@ints) { + say 'Input: @ints = (' . join(', ', @ints) . ')'; + my ($sum, $explain) = specialNumberSquareSum(@ints); + say 'Output: ' . $sum; + say ''; + say $explain; +} + +say "Example 1:"; +solution(1, 2, 3, 4); + +say "\nExample 2:"; +solution(2, 7, 1, 19, 18, 3);
\ No newline at end of file diff --git a/challenge-252/packy-anderson/perl/ch-2.pl b/challenge-252/packy-anderson/perl/ch-2.pl new file mode 100755 index 0000000000..df3757ca14 --- /dev/null +++ b/challenge-252/packy-anderson/perl/ch-2.pl @@ -0,0 +1,34 @@ +#!/usr/bin/env perl +use v5.38; + +sub uniqueSumZero($n) { + my @list; + my $x = 1; + while ($n > 0) { + if ($n % 2 == 1) { # $n is odd + push @list, 0; + $n--; + } + else { # $n is even + push @list, $x * -1, $x; + $x++; + $n -= 2; + } + } + return sort { $a <=> $b } @list; +} + +sub solution($n) { + say 'Input: $n = ' . $n; + my @list = uniqueSumZero($n); + say 'Output: (' . join(', ', @list) . ')'; +} + +say "Example 1:"; +solution(5); + +say "\nExample 2:"; +solution(3); + +say "\nExample 3:"; +solution(1);
\ No newline at end of file diff --git a/challenge-252/packy-anderson/python/ch-1.py b/challenge-252/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..16a9fd6188 --- /dev/null +++ b/challenge-252/packy-anderson/python/ch-1.py @@ -0,0 +1,69 @@ +#!/usr/bin/env python + +def comma_join(arr): + return ', '.join(map(lambda i: str(i), arr)) + +def specialElementIndices(n): + return list( filter(lambda x: n % x == 0, range(1, n+1)) ) + +def english_list (strlist): + # given a list, join it in a way that makes sense + # to english speakers + last = strlist.pop(-1) # last element in array + if (len(strlist) == 0): + # using an array in a scalar context returns + # the number of elements in the array + + # there was only one element in the list + return last + + joined = ',\n'.join(strlist) + if (len(strlist) > 1): + # if there's more than element, add an Oxford comma + joined += ',' + + return f'{joined} and\n{last}' + +def specialNumberSquareSum(ints): + n = len(ints) + + # find the list of indices for "special" numbers + specialIndices = specialElementIndices(n) + count = len(specialIndices) + explain_list = [ + f"$ints[{x}] since {x} divides {n}" + for x in specialIndices + ] + explain = ( + "There are exactly $count special elements " + + "in the given array:\n" + english_list(explain_list) + ) + + # find the special numbers themselves + special = [ ints[x - 1] for x in specialIndices ] + + # find the sum of the squares + sumval = sum([ x ** 2 for x in special ]) + + explain += '\nHence, the sum of the squares of all special ' + explain += 'elements of given array:\n' + explain += ' + '.join(map(lambda x: f'{x} * {x}', special)) + explain += f' = {sumval}' + + return ( + sumval, + explain + ) + +def solution(ints): + print(f'Input: @ints = ({comma_join(ints)})') + (sumval, explain) = specialNumberSquareSum(ints) + print(f'Output: {sumval}') + print('') + print(explain) + +print('Example 1:') +solution([1, 2, 3, 4]) + +print('\nExample 2:') +solution([2, 7, 1, 19, 18, 3])
\ No newline at end of file diff --git a/challenge-252/packy-anderson/python/ch-2.py b/challenge-252/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..b68a6502c2 --- /dev/null +++ b/challenge-252/packy-anderson/python/ch-2.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python + +def uniqueSumZero(n): + zero_sum_list = [] + x = 1 + while n > 0: + if (n % 2 == 1): # n is odd + zero_sum_list.append(0) + n -= 1 + else: # n is even + zero_sum_list.append(x * -1) + zero_sum_list.append(x) + x += 1 + n -= 2 + zero_sum_list.sort() + return zero_sum_list + +def comma_join(arr): + return ', '.join(map(lambda i: str(i), arr)) + +def solution(n): + print(f'Input: $n = {n}') + zero_sum_list = uniqueSumZero(n) + print(f'Output: ({comma_join(zero_sum_list)})') + +print('Example 1:') +solution(5) + +print('\nExample 2:') +solution(3) + +print('\nExample 3:') +solution(1)
\ No newline at end of file diff --git a/challenge-252/packy-anderson/raku/ch-1.raku b/challenge-252/packy-anderson/raku/ch-1.raku new file mode 100755 index 0000000000..8669abe7e3 --- /dev/null +++ b/challenge-252/packy-anderson/raku/ch-1.raku @@ -0,0 +1,68 @@ +#!/usr/bin/env raku +use v6; + +sub specialElementIndices($n) { + return (1 .. $n).grep({ $n % $_ == 0 }); +} + +sub english_list ( *@list ) { + # given a list, join it in a way that makes sense + # to english speakers + my $last = @list.pop(); # last element in array + if (@list == 0) { + # using an array in a scalar context returns + # the number of elements in the array + + # there was only one element in the list + return $last; + } + my $joined = @list.join(qq{,\n}); + if (@list > 1) { + # if there's more than element, add an Oxford comma + $joined ~= q{,}; + } + return "$joined and\n$last"; +} + +sub specialNumberSquareSum(@ints) { + my $n = @ints.elems; + + # find the list of indices for "special" numbers + my @specialIndices = specialElementIndices($n); + my $count = @specialIndices.elems; + my @explain_list = @specialIndices.map({ + "\$ints[$_] since $_ divides $n" + }); + my $explain = "There are exactly $count special elements " + ~ "in the given array:\n" ~ english_list(@explain_list); + + # find the special numbers themselves + my @special = @specialIndices.map({ @ints[$_ - 1] }); + + # find the sum of the squares + my $sum = @special.map({ $_ ** 2 }).sum; + + $explain ~= "\nHence, the sum of the squares of all special " + ~ "elements of given array:\n" + ~ @special.map({ "$_ * $_" }).join(' + ') + ~ " = " ~ $sum; + + return ( + $sum, + $explain + ); +} + +sub solution(@ints) { + say 'Input: @ints = (' ~ @ints.join(', ') ~ ')'; + my ($sum, $explain) = specialNumberSquareSum(@ints); + say 'Output: ' ~ $sum; + say ''; + say $explain; +} + +say "Example 1:"; +solution([1, 2, 3, 4]); + +say "\nExample 2:"; +solution([2, 7, 1, 19, 18, 3]);
\ No newline at end of file diff --git a/challenge-252/packy-anderson/raku/ch-2.raku b/challenge-252/packy-anderson/raku/ch-2.raku new file mode 100755 index 0000000000..26cb5d6709 --- /dev/null +++ b/challenge-252/packy-anderson/raku/ch-2.raku @@ -0,0 +1,35 @@ +#!/usr/bin/env raku +use v6; + +sub uniqueSumZero($input) { + my $n = $input; + my @list; + my $x = 1; + while ($n > 0) { + if ($n % 2 == 1) { # $n is odd + @list.push(0); + $n -= 1; + } + else { # $n is even + @list.append($x * -1, $x); + $x += 1; + $n -= 2; + } + } + return @list.sort; +} + +sub solution($n) { + say 'Input: $n = ' ~ $n; + my @list = uniqueSumZero($n); + say 'Output: (' ~ @list.join(', ') ~ ')'; +} + +say "Example 1:"; +solution(5); + +say "\nExample 2:"; +solution(3); + +say "\nExample 3:"; +solution(1);
\ No newline at end of file |
