aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-10-10 14:56:39 +0100
committerGitHub <noreply@github.com>2023-10-10 14:56:39 +0100
commit28edf669f921ac6fa6940ca2bc2dfdbd096d66fa (patch)
treeffd60395d27e245975e9c7e5e2d4045ba3b3c919
parent3ce214ad99d99c1edcae374b6eac3c16448216c8 (diff)
parent6c01169c8802364daef753e39555b9ca9f220e13 (diff)
downloadperlweeklychallenge-club-28edf669f921ac6fa6940ca2bc2dfdbd096d66fa.tar.gz
perlweeklychallenge-club-28edf669f921ac6fa6940ca2bc2dfdbd096d66fa.tar.bz2
perlweeklychallenge-club-28edf669f921ac6fa6940ca2bc2dfdbd096d66fa.zip
Merge pull request #8842 from packy/master
Challenge 238 solutions by Packy Anderson
-rw-r--r--challenge-238/packy-anderson/README.md92
-rw-r--r--challenge-238/packy-anderson/blog.txt1
-rwxr-xr-xchallenge-238/packy-anderson/perl/ch-1.pl31
-rwxr-xr-xchallenge-238/packy-anderson/perl/ch-2.pl63
-rwxr-xr-xchallenge-238/packy-anderson/python/ch-1.py27
-rwxr-xr-xchallenge-238/packy-anderson/python/ch-2.py58
-rwxr-xr-xchallenge-238/packy-anderson/raku/ch-1.raku30
-rwxr-xr-xchallenge-238/packy-anderson/raku/ch-2.raku64
8 files changed, 302 insertions, 64 deletions
diff --git a/challenge-238/packy-anderson/README.md b/challenge-238/packy-anderson/README.md
index 047c0f6bd7..dc1fa1c2b2 100644
--- a/challenge-238/packy-anderson/README.md
+++ b/challenge-238/packy-anderson/README.md
@@ -8,22 +8,16 @@ Sample output
```
$ perl/ch-1.pl
Example 1:
-Input: Year = 2024, Month = 4, Weekday of month = 3, day of week = 2
-Output: 16
-
-The 3rd Tue of Apr 2024 is the 16th
+Input: @int = (1, 2, 3, 4, 5)
+Output: (1, 3, 6, 10, 15)
Example 2:
-Input: Year = 2025, Month = 10, Weekday of month = 2, day of week = 4
-Output: 9
-
-The 2nd Thu of Oct 2025 is the 9th
+Input: @int = (1, 1, 1, 1, 1)
+Output: (1, 2, 3, 4, 5)
Example 3:
-Input: Year = 2026, Month = 8, Weekday of month = 5, day of week = 3
-Output: 0
-
-There isn't a 5th Wed in Aug 2026
+Input: @int = (0, -1, 1, 2)
+Output: (0, -1, 0, 2)
```
* [Task 2](perl/ch-2.pl)
@@ -32,25 +26,22 @@ Sample output
```
$ perl/ch-2.pl
Example 1:
-Input: @ints = (1, 3, 5, 2, 1, 3, 1)
-Output: 4
+Input: @int = (15, 99, 1, 34)
+Output: (1, 15, 34, 99)
-One possible permutation: (2, 5, 1, 3, 3, 1, 1)
-which returns 4 greatness as below:
-nums[0] < perm[0]
-nums[1] < perm[1]
-nums[3] < perm[3]
-nums[4] < perm[4]
+15 => 1 x 5 => 5 (1 step)
+99 => 9 x 9 => 81 => 8 x 1 => 8 (2 steps)
+1 (0 steps)
+34 => 3 x 4 => 12 => 1 x 2 => 2 (2 steps)
Example 2:
-Input: @ints = (1, 2, 3, 4)
-Output: 3
-
-One possible permutation: (2, 3, 4, 1)
-which returns 3 greatness as below:
-nums[0] < perm[0]
-nums[1] < perm[1]
-nums[2] < perm[2]
+Input: @int = (50, 25, 33, 22)
+Output: (22, 33, 50, 25)
+
+50 => 5 x 0 => 0 (1 step)
+25 => 2 x 5 => 10 => 1 x 0 => 0 (2 steps)
+33 => 3 x 3 => 9 (1 step)
+22 => 2 x 2 => 4 (1 step)
```
## Raku
@@ -61,49 +52,22 @@ Sample output
```
$ raku/ch-1.raku
Example 1:
-Input: Year = 2024, Month = 4, Weekday of month = 3, day of week = 2
-Output: 16
-
-The 3rd Tuesday of 4 2024 is the 16th
+Input: @int = (1, 2, 3, 4, 5)
+Output: (1, 3, 6, 10, 15)
Example 2:
-Input: Year = 2025, Month = 10, Weekday of month = 2, day of week = 4
-Output: 9
-
-The 2nd Thursday of 10 2025 is the 9th
+Input: @int = (1, 1, 1, 1, 1)
+Output: (1, 2, 3, 4, 5)
Example 3:
-Input: Year = 2026, Month = 8, Weekday of month = 5, day of week = 3
-Output: 0
-
-There isn't a 5th Wednesday in 8 2026
+Input: @int = (0, -1, 1, 2)
+Output: (0, -1, 0, 2)
```
* [Task 2](raku/ch-2.raku)
Sample output
```
-$ raku/ch-2.raku
-Example 1:
-Input: @ints = (1, 3, 5, 2, 1, 3, 1)
-Output: 4
-
-One possible permutation: (2, 5, 1, 3, 3, 1, 1)
-which returns 4 greatness as below:
-nums[0] < perm[0]
-nums[1] < perm[1]
-nums[3] < perm[3]
-nums[4] < perm[4]
-
-Example 2:
-Input: @ints = (1, 2, 3, 4)
-Output: 3
-
-One possible permutation: (2, 3, 4, 1)
-which returns 3 greatness as below:
-nums[0] < perm[0]
-nums[1] < perm[1]
-nums[2] < perm[2]
```
## Guest Language: Python
@@ -111,9 +75,9 @@ nums[2] < perm[2]
* [Task 2](python/ch-2.py)
## Guest Language: Java
-* [Task 1](java/Ch1.java)
-* [Task 2](java/Ch2.java)
+
+To be completed later...
## Blog Post
-[Perl Weekly Challenge: Minute by minute, that’s how you win it!](https://packy.dardan.com/2023/10/04/perl-weekly-challenge-minute-by-minute-thats-how-you-win-it/)
+[Perl Weekly Challenge: Be Runnin' Up That Sum, Be Persisten' Up That Sort](https://packy.dardan.com/2023/10/09/be-runnin-up-that-sum-be-persisten-up-that-sort/)
diff --git a/challenge-238/packy-anderson/blog.txt b/challenge-238/packy-anderson/blog.txt
new file mode 100644
index 0000000000..a9eb809f74
--- /dev/null
+++ b/challenge-238/packy-anderson/blog.txt
@@ -0,0 +1 @@
+https://packy.dardan.com/2023/10/09/be-runnin-up-that-sum-be-persisten-up-that-sort/ \ No newline at end of file
diff --git a/challenge-238/packy-anderson/perl/ch-1.pl b/challenge-238/packy-anderson/perl/ch-1.pl
new file mode 100755
index 0000000000..0621a9e93d
--- /dev/null
+++ b/challenge-238/packy-anderson/perl/ch-1.pl
@@ -0,0 +1,31 @@
+#!/usr/bin/env perl
+use v5.38;
+
+sub runningSum {
+ my @int = @_;
+ my @sums;
+ my $running_sum = 0;
+ foreach my $num ( @int ) {
+ # add the next number to the sum of numbers before it
+ $running_sum += $num;
+ # add the current running sum to the output array
+ push @sums, $running_sum;
+ }
+ return @sums;
+}
+
+sub solution {
+ my @int = @_;
+ say 'Input: @int = (' . join(', ', @int) . ')';
+ my @sums = runningSum(@int);
+ say 'Output: (' . join(', ', @sums) . ')';
+}
+
+say "Example 1:";
+solution(1, 2, 3, 4, 5);
+
+say "\nExample 2:";
+solution(1, 1, 1, 1, 1);
+
+say "\nExample 3:";
+solution(0, -1, 1, 2); \ No newline at end of file
diff --git a/challenge-238/packy-anderson/perl/ch-2.pl b/challenge-238/packy-anderson/perl/ch-2.pl
new file mode 100755
index 0000000000..69e99cf557
--- /dev/null
+++ b/challenge-238/packy-anderson/perl/ch-2.pl
@@ -0,0 +1,63 @@
+#!/usr/bin/env perl
+use v5.38;
+
+use List::Util qw( product );
+
+sub persistenceSort {
+ my @int = @_;
+ my %step_count;
+ my $steps;
+ # first, calculates the steps for each number
+ foreach my $num ( @int ) {
+ $step_count{$num} = 0;
+
+ $steps .= "\n$num"; # our starting number
+
+ my $num_copy = $num; # copy the num so we can modify it
+
+ while ( length($num_copy) > 1 ) {
+ # split $num_copy into its individual digits
+ my @digits = split //, $num_copy;
+
+ # generate a new number by multiplying all the digits
+ $num_copy = product @digits;
+
+ # show the multiplication in the steps for this num
+ $steps .= ' => ' . join(' x ', @digits);
+ $steps .= " => $num_copy";
+
+ # add to our count of steps
+ $step_count{$num}++;
+ }
+
+ # put the step count in the steps for this num
+ $steps .=
+ sprintf " (%d step%s)", $step_count{$num},
+ $step_count{$num} == 1 ? '' : 's';
+ }
+
+ # now, sort by steps/numeric value
+ my @sorted = sort {
+ # sort by step count
+ $step_count{$a} <=> $step_count{$b}
+ ||
+ # then sort numerically
+ $a <=> $b
+ } @int;
+
+ return \@sorted, $steps;
+}
+
+sub solution {
+ my @int = @_;
+ say 'Input: @int = (' . join(', ', @int) . ')';
+ my($sorted, $steps) = persistenceSort(@int);
+ say 'Output: (' . join(', ', @$sorted) . ')';
+ say $steps;
+}
+
+say "Example 1:";
+solution(15, 99, 1, 34);
+
+say "\nExample 2:";
+solution(50, 25, 33, 22); \ No newline at end of file
diff --git a/challenge-238/packy-anderson/python/ch-1.py b/challenge-238/packy-anderson/python/ch-1.py
new file mode 100755
index 0000000000..60ee93401f
--- /dev/null
+++ b/challenge-238/packy-anderson/python/ch-1.py
@@ -0,0 +1,27 @@
+#!/usr/bin/env python
+
+def runningSum(int):
+ sums = []
+ running_sum = 0
+ for num in int:
+ # add the next number to the sum of numbers before it
+ running_sum += num
+ # add the current running sum to the output array
+ sums.append( running_sum )
+ return sums
+
+def solution(int):
+ as_list = ', '.join(map(lambda i: str(i), int))
+ print(f'Input: @int = ({as_list})')
+ sums = runningSum(int)
+ as_list = ', '.join(map(lambda i: str(i), sums))
+ print(f'Output: ({as_list})')
+
+print('Example 1:')
+solution([1, 2, 3, 4, 5])
+
+print('\nExample 2:')
+solution([1, 1, 1, 1, 1])
+
+print('\nExample 3:')
+solution([0, -1, 1, 2]) \ No newline at end of file
diff --git a/challenge-238/packy-anderson/python/ch-2.py b/challenge-238/packy-anderson/python/ch-2.py
new file mode 100755
index 0000000000..a168a3d4ce
--- /dev/null
+++ b/challenge-238/packy-anderson/python/ch-2.py
@@ -0,0 +1,58 @@
+#!/usr/bin/env python
+
+from functools import cmp_to_key, reduce
+
+def persistenceSort(int_list):
+ step_count = {}
+ steps = ""
+
+ # first, calculates the steps for each number
+ for num in int_list:
+ step_count[num] = 0
+
+ steps += f"\n{num}" # our starting number
+
+ num_copy = str(num) # copy the num so we can modify it
+
+ while len(num_copy) > 1:
+ # split num_copy into its individual digits
+ digits = list(num_copy)
+
+ # generate a new number by multiplying all the digits
+ num_copy = str(
+ reduce(
+ lambda a, b: int(a) * int(b),
+ digits
+ )
+ )
+
+ # show the multiplication in the steps for this num
+ steps += ' => ' + ' x '.join(digits)
+ steps += ' => ' + num_copy
+
+ # add to our count of steps
+ step_count[num] += 1
+
+ # put the step count in the steps for this num
+ step_word = 'step' if step_count[num] == 1 else 'steps'
+ steps += f" ({step_count[num]} {step_word})"
+
+ # now, sort by steps/numeric value
+ sorted_list = sorted(int_list,
+ key=lambda x: (step_count[x], x))
+
+ return sorted_list, steps
+
+def solution(int_list):
+ as_list = ', '.join(map(lambda i: str(i), int_list))
+ print(f'Input: @int = ({as_list})')
+ sorted_list, steps = persistenceSort(int_list)
+ as_list = ', '.join(map(lambda i: str(i), sorted_list))
+ print(f'Output: ({as_list})')
+ print(steps)
+
+print('Example 1:')
+solution([15, 99, 1, 34])
+
+print('\nExample 2:')
+solution([50, 25, 33, 22]) \ No newline at end of file
diff --git a/challenge-238/packy-anderson/raku/ch-1.raku b/challenge-238/packy-anderson/raku/ch-1.raku
new file mode 100755
index 0000000000..cdc6759ca9
--- /dev/null
+++ b/challenge-238/packy-anderson/raku/ch-1.raku
@@ -0,0 +1,30 @@
+#!/usr/bin/env raku
+use v6;
+
+sub runningSum(*@int) {
+ my @sums;
+ my $running_sum = 0;
+ for @int -> $num {
+ # add the next number to the sum of numbers before it
+ $running_sum += $num;
+ # add the current running sum to the output array
+ @sums.push( $running_sum );
+ }
+ return @sums;
+}
+
+sub solution {
+ my @int = @_;
+ say 'Input: @int = (' ~ @int.join(', ') ~ ')';
+ my @sums = runningSum(@int);
+ say 'Output: (' ~ @sums.join(', ') ~ ')';
+}
+
+say "Example 1:";
+solution(1, 2, 3, 4, 5);
+
+say "\nExample 2:";
+solution(1, 1, 1, 1, 1);
+
+say "\nExample 3:";
+solution(0, -1, 1, 2); \ No newline at end of file
diff --git a/challenge-238/packy-anderson/raku/ch-2.raku b/challenge-238/packy-anderson/raku/ch-2.raku
new file mode 100755
index 0000000000..230e32c904
--- /dev/null
+++ b/challenge-238/packy-anderson/raku/ch-2.raku
@@ -0,0 +1,64 @@
+#!/usr/bin/env raku
+use v6;
+
+sub persistenceSort(*@int) {
+ my %step_count;
+ my $steps;
+ # first, calculates the steps for each number
+ for @int -> $num {
+ %step_count{$num} = 0;
+
+ $steps ~= "\n$num"; # our starting number
+
+ my $num_copy = $num; # copy the num so we can modify it
+
+ while ( $num_copy.Str.chars > 1 ) {
+ # split $num_copy into its individual digits
+ my @digits = $num_copy.split('', :skip-empty);
+
+ # generate a new number by multiplying all the digits
+ $num_copy = [*] @digits;
+
+ # show the multiplication in the steps for this num
+ $steps ~= ' => ' ~ @digits.join(' x ');
+ $steps ~= " => $num_copy";
+
+ # add to our count of steps
+ %step_count{$num}++;
+ }
+
+ # put the step count in the steps for this num
+ $steps ~=
+ sprintf " (%d step%s)", %step_count{$num},
+ %step_count{$num} == 1 ?? '' !! 's';
+ }
+
+ # now, sort by steps/numeric value
+ my @sorted = @int.sort({
+ # sort by step count
+ %step_count{$^a} <=> %step_count{$^b}
+ ||
+ # then sort numerically
+ $^a <=> $^b
+ });
+
+ return @sorted, $steps;
+}
+
+sub solution {
+ my @int = @_;
+ say 'Input: @int = (' ~ @int.join(', ') ~ ')';
+ # if we attempt to capture the returned array in
+ # @sorted, the array becomes the first ELEMENT in
+ # @sorted (and the steps Str becomes the second
+ # element) so we capture it in $sorted
+ my ($sorted, $steps) = persistenceSort(@int);
+ say 'Output: (' ~ $sorted.join(', ') ~ ')';
+ say $steps;
+}
+
+say "Example 1:";
+solution(15, 99, 1, 34);
+
+say "\nExample 2:";
+solution(50, 25, 33, 22); \ No newline at end of file