aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-03-20 19:35:25 +0000
committerGitHub <noreply@github.com>2024-03-20 19:35:25 +0000
commitb24affed570eccfad6c65c7456afeae2a65d01fc (patch)
treeda30679cc9ccbeaf506299ebca0289a8e2f73e7d
parent65d6c1abbc1203f16b9c9da5812095a2165e5d70 (diff)
parentee16cba97d38ad7e846d845c4072741387d6186c (diff)
downloadperlweeklychallenge-club-b24affed570eccfad6c65c7456afeae2a65d01fc.tar.gz
perlweeklychallenge-club-b24affed570eccfad6c65c7456afeae2a65d01fc.tar.bz2
perlweeklychallenge-club-b24affed570eccfad6c65c7456afeae2a65d01fc.zip
Merge pull request #9783 from packy/master
Challenge 261 solutions by Packy Anderson
-rw-r--r--challenge-261/packy-anderson/README.md2
-rw-r--r--challenge-261/packy-anderson/blog.txt1
-rwxr-xr-xchallenge-261/packy-anderson/perl/ch-1.pl49
-rwxr-xr-xchallenge-261/packy-anderson/perl/ch-2.pl35
-rwxr-xr-xchallenge-261/packy-anderson/python/ch-1.py47
-rwxr-xr-xchallenge-261/packy-anderson/python/ch-2.py37
-rwxr-xr-xchallenge-261/packy-anderson/raku/ch-1.raku49
-rwxr-xr-xchallenge-261/packy-anderson/raku/ch-2.raku38
8 files changed, 257 insertions, 1 deletions
diff --git a/challenge-261/packy-anderson/README.md b/challenge-261/packy-anderson/README.md
index e7ef61a748..72d867b319 100644
--- a/challenge-261/packy-anderson/README.md
+++ b/challenge-261/packy-anderson/README.md
@@ -16,4 +16,4 @@
## Blog Post
-[Unique Dictionary Occurrences are Rank](https://packy.dardan.com/2024/03/11/perl-weekly-challenge-260J_)
+[Two Elements, Multiplied by Digit Sum](https://packy.dardan.com/b/Jj)
diff --git a/challenge-261/packy-anderson/blog.txt b/challenge-261/packy-anderson/blog.txt
new file mode 100644
index 0000000000..1f5fa62e9a
--- /dev/null
+++ b/challenge-261/packy-anderson/blog.txt
@@ -0,0 +1 @@
+https://packy.dardan.com/b/Jj
diff --git a/challenge-261/packy-anderson/perl/ch-1.pl b/challenge-261/packy-anderson/perl/ch-1.pl
new file mode 100755
index 0000000000..413fab645a
--- /dev/null
+++ b/challenge-261/packy-anderson/perl/ch-1.pl
@@ -0,0 +1,49 @@
+#!/usr/bin/env perl
+use v5.38;
+
+use List::Util qw( sum );
+
+sub elementDigitSum(@ints) {
+ my $elementSum = sum @ints;
+
+ my $explain = 'Element Sum: '
+ . join(' + ', @ints)
+ . ' = ' . $elementSum;
+
+ # use join() to concatenate all the integers together
+ # into a single string, then use split() to get the
+ # individual digits
+ my @digits = split //, join('', @ints);
+ my $digitSum = sum @digits;
+
+ $explain .= "\nDigit Sum: "
+ . join(' + ', @digits)
+ . ' = ' . $digitSum;
+
+ my $abs = abs($elementSum - $digitSum);
+
+ $explain .= "\nAbsolute Difference: "
+ . "| $elementSum - $digitSum | = $abs";
+
+ return ($abs, $explain);
+}
+
+sub solution(@ints) {
+ say 'Input: @ints = (' . join(', ', @ints) . ')';
+ my ($sum, $explain) = elementDigitSum(@ints);
+ say 'Output: ' . $sum;
+ say '';
+ say $explain;
+}
+
+say "Example 1:";
+solution(1,2,3,45);
+
+say "\nExample 2:";
+solution(1,12,3);
+
+say "\nExample 3:";
+solution(1,2,3,4);
+
+say "\nExample 4:";
+solution(236, 416, 336, 350); \ No newline at end of file
diff --git a/challenge-261/packy-anderson/perl/ch-2.pl b/challenge-261/packy-anderson/perl/ch-2.pl
new file mode 100755
index 0000000000..33e8eac9fd
--- /dev/null
+++ b/challenge-261/packy-anderson/perl/ch-2.pl
@@ -0,0 +1,35 @@
+#!/usr/bin/env perl
+use v5.38;
+
+sub multiplyByTwo($start, @ints) {
+ my %ints = map { $_ => 1 } @ints;
+ my @explain;
+ my $step = 0;
+
+ while ( exists $ints{$start} ) {
+ $step++;
+ my $old = $start;
+ $start *= 2;
+ push @explain,
+ "Step $step: $old is in the array so $old x 2 = $start";
+ }
+ push @explain,
+ "$start is not in the array so return $start.";
+ return ($start, join("\n", @explain));
+}
+
+sub solution($start, $ints) {
+ print 'Input: @ints = (' . join(', ', @$ints);
+ say ") and \$start = $start";
+ my ($output, $explain) = multiplyByTwo($start, @$ints);
+ say "Output: $output\n\n$explain";
+}
+
+say "Example 1:";
+solution(3, [5,3,6,1,12]);
+
+say "\nExample 2:";
+solution(1, [1,2,4,3]);
+
+say "\nExample 3:";
+solution(2, [5,6,7]); \ No newline at end of file
diff --git a/challenge-261/packy-anderson/python/ch-1.py b/challenge-261/packy-anderson/python/ch-1.py
new file mode 100755
index 0000000000..727b9e253e
--- /dev/null
+++ b/challenge-261/packy-anderson/python/ch-1.py
@@ -0,0 +1,47 @@
+#!/usr/bin/env python
+
+def plus_join(arr):
+ return ' + '.join(map(lambda i: str(i), arr))
+
+def elementDigitSum(ints):
+ elementSum = sum(ints)
+
+ explain = f'Element Sum: {plus_join(ints)} = {elementSum}'
+
+ # concatenate all the integers together into a single
+ # string
+ digitStr = ''.join([ str(i) for i in ints ])
+ # loop over the individual digits
+ digits = [ int(d) for d in digitStr ]
+ digitSum = sum(digits)
+
+ explain += "\n"
+ explain += f'Digit Sum: {plus_join(digits)} = {digitSum}'
+
+ absVal = abs(elementSum - digitSum)
+
+ explain += "\n"
+ explain += 'Absolute Difference: '
+ explain += f'| {elementSum} - {digitSum} | = {absVal}'
+
+ return (absVal, explain)
+
+def comma_join(arr):
+ return ', '.join(map(lambda i: str(i), arr))
+
+def solution(ints):
+ print(f'Input: @ints = ({comma_join(ints)})')
+ (sum, explain) = elementDigitSum(ints)
+ print(f'Output: {sum}\n\n{explain}')
+
+print('Example 1:')
+solution([1,2,3,45])
+
+print('\nExample 2:')
+solution([1,12,3])
+
+print('\nExample 3:')
+solution([1,2,3,4])
+
+print('\nExample 4:')
+solution([236, 416, 336, 350])
diff --git a/challenge-261/packy-anderson/python/ch-2.py b/challenge-261/packy-anderson/python/ch-2.py
new file mode 100755
index 0000000000..fbbf4f535e
--- /dev/null
+++ b/challenge-261/packy-anderson/python/ch-2.py
@@ -0,0 +1,37 @@
+#!/usr/bin/env python
+
+def multiplyByTwo(ints, start):
+ intSet = frozenset(ints)
+ explain = []
+ step = 0
+
+ while start in intSet:
+ step += 1
+ old = start
+ start *= 2
+ explain.append(
+ f"Step {step}: {old} is in the array " +
+ f"so {old} x 2 = {start}"
+ )
+ explain.append(
+ f"{start} is not in the array so return {start}."
+ )
+ return (start, "\n".join(explain))
+
+def comma_join(arr):
+ return ', '.join(map(lambda i: str(i), arr))
+
+def solution(ints, start):
+ print(f'Input: @ints = ({comma_join(ints)})', end=" ")
+ print(f'and $start = {start}')
+ (output, explain) = multiplyByTwo(ints, start)
+ print(f'Output: {output}\n\n{explain}')
+
+print('Example 1:')
+solution([5,3,6,1,12], 3)
+
+print('\nExample 2:')
+solution([1,2,4,3], 1)
+
+print('\nExample 3:')
+solution([5,6,7], 2) \ No newline at end of file
diff --git a/challenge-261/packy-anderson/raku/ch-1.raku b/challenge-261/packy-anderson/raku/ch-1.raku
new file mode 100755
index 0000000000..c782854341
--- /dev/null
+++ b/challenge-261/packy-anderson/raku/ch-1.raku
@@ -0,0 +1,49 @@
+#!/usr/bin/env raku
+use v6;
+
+sub elementDigitSum(@ints) {
+ # [+] sums all the elements of @ints
+ my $elementSum = [+] @ints;
+
+ my $explain = 'Element Sum: '
+ ~ @ints.join(' + ')
+ ~ ' = ' ~ $elementSum;
+
+ # use [~] to concatenate all the integers together
+ # into a single string, then use split() to get the
+ # individual digits
+ my @digits = ([~] @ints).split('', :skip-empty);
+ # [+] sums all the elements of @digits
+ my $digitSum = [+] @digits;
+
+ $explain ~= "\n" ~ 'Digit Sum: '
+ ~ @digits.join(' + ')
+ ~ ' = ' ~ $digitSum;
+
+ my $abs = ($elementSum - $digitSum).abs;
+
+ $explain ~= "\nAbsolute Difference: "
+ ~ "| $elementSum - $digitSum | = $abs";
+
+ return ($abs, $explain);
+}
+
+sub solution(@ints) {
+ say 'Input: @ints = (' ~ @ints.join(', ') ~ ')';
+ my ($sum, $explain) = elementDigitSum(@ints);
+ say 'Output: ' ~ $sum;
+ say '';
+ say $explain;
+}
+
+say "Example 1:";
+solution([1,2,3,45]);
+
+say "\nExample 2:";
+solution([1,12,3]);
+
+say "\nExample 3:";
+solution([1,2,3,4]);
+
+say "\nExample 4:";
+solution([236, 416, 336, 350]); \ No newline at end of file
diff --git a/challenge-261/packy-anderson/raku/ch-2.raku b/challenge-261/packy-anderson/raku/ch-2.raku
new file mode 100755
index 0000000000..44d388d3a1
--- /dev/null
+++ b/challenge-261/packy-anderson/raku/ch-2.raku
@@ -0,0 +1,38 @@
+#!/usr/bin/env raku
+use v6;
+
+sub multiplyByTwo(@ints, $s) {
+ my $start = $s; # so we can modify the value
+ my $ints = Set(@ints);
+ my @explain;
+ my $step = 0;
+
+ while ($start ∈ $ints) {
+ $step++;
+ my $old = $start;
+ $start *= 2;
+ @explain.push(
+ "Step $step: $old is in the array so $old x 2 = $start"
+ );
+ }
+ @explain.push(
+ "$start is not in the array so return $start."
+ );
+ return ($start, @explain.join("\n"));
+}
+
+sub solution(@ints, $start) {
+ print 'Input: @ints = (' ~ @ints.join(', ');
+ say ") and \$start = $start";
+ my ($output, $explain) = multiplyByTwo(@ints, $start);
+ say "Output: $output\n\n$explain";
+}
+
+say "Example 1:";
+solution([5,3,6,1,12], 3);
+
+say "\nExample 2:";
+solution([1,2,4,3], 1);
+
+say "\nExample 3:";
+solution([5,6,7], 2); \ No newline at end of file