From 09bbd9d0b152fe780fcfa4f12543d4d11df4141e Mon Sep 17 00:00:00 2001 From: Bob Lied Date: Mon, 9 Oct 2023 12:56:40 -0500 Subject: Update README --- challenge-238/bob-lied/README | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/challenge-238/bob-lied/README b/challenge-238/bob-lied/README index ca9b4d5e22..beac1734fc 100644 --- a/challenge-238/bob-lied/README +++ b/challenge-238/bob-lied/README @@ -1,4 +1,4 @@ -Solutions to weekly challenge 237 by Bob Lied +Solutions to weekly challenge 238 by Bob Lied -https://perlweeklychallenge.org/blog/perl-weekly-challenge-237/ -https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-237/bob-lied +https://perlweeklychallenge.org/blog/perl-weekly-challenge-238/ +https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-238/bob-lied -- cgit From 6aa3eacc50edcdef100509986db3032d1dd03593 Mon Sep 17 00:00:00 2001 From: Bob Lied Date: Mon, 9 Oct 2023 13:08:43 -0500 Subject: Task 1 solution --- challenge-172/bob-lied/README | 4 +-- challenge-172/bob-lied/perl/ch-1.pl | 47 +++++++++++++++++++++++++++++++++ challenge-172/bob-lied/perl/ch-2.pl | 48 ++++++++++++++++++++++++++++++++++ challenge-238/bob-lied/perl/ch-1.pl | 52 +++++++++++++++++++++++++++++++++++++ 4 files changed, 149 insertions(+), 2 deletions(-) create mode 100644 challenge-172/bob-lied/perl/ch-1.pl create mode 100644 challenge-172/bob-lied/perl/ch-2.pl create mode 100644 challenge-238/bob-lied/perl/ch-1.pl diff --git a/challenge-172/bob-lied/README b/challenge-172/bob-lied/README index c231e3a589..fe223977ff 100644 --- a/challenge-172/bob-lied/README +++ b/challenge-172/bob-lied/README @@ -1,3 +1,3 @@ -Solutions to weekly challenge 138 by Bob Lied +Solutions to weekly challenge 172 by Bob Lied -https://perlweeklychallenge.org/blog/perl-weekly-challenge-138/ +https://perlweeklychallenge.org/blog/perl-weekly-challenge-172/ diff --git a/challenge-172/bob-lied/perl/ch-1.pl b/challenge-172/bob-lied/perl/ch-1.pl new file mode 100644 index 0000000000..ae612614de --- /dev/null +++ b/challenge-172/bob-lied/perl/ch-1.pl @@ -0,0 +1,47 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# ch-1.pl Perl Weekly Challenge 172 Task 1 Prime Partition +#============================================================================= +# Copyright (c) 2023, Bob Lied +#============================================================================= +# You are given two positive integers, $m and $n. +# Write a script to find out the Prime Partition of the given number. +# No duplicates allowed. +# https://en.wikipedia.org/wiki/Partition_(number_theory) +# Example 1: Input: $m = 18, $n = 2 +# Output: 5, 13 or 7, 11 +# Example 2: Input: $m = 19, $n = 3 +# Output: 3, 5, 11 +#============================================================================= + +use v5.38; +use builtin qw/true false/; no warnings "experimental::builtin"; + +use Math::Prime::Util qw/forpart partitions/; + +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +exit(!runTest()) if $DoTest; + +sub primePartition($m, $n) +{ + my @part; + forpart { push @part, [ @_ ] } $m, { n => $n, prime => 1 }; + + return @part; +} + +sub runTest +{ + use Test2::V0; + + is( primePartition(19, 3), [ [3,5,11] ], "Example 2"); + is( primePartition(18, 2), [ [5,13], [7,11] ], "Example 1"); + + done_testing; +} + diff --git a/challenge-172/bob-lied/perl/ch-2.pl b/challenge-172/bob-lied/perl/ch-2.pl new file mode 100644 index 0000000000..10542c894e --- /dev/null +++ b/challenge-172/bob-lied/perl/ch-2.pl @@ -0,0 +1,48 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# ch-2.pl Perl Weekly Challenge 172 Task 2 Five-number Summary +#============================================================================= +# Copyright (c) 2023, Bob Lied +#============================================================================= +# You are given an array of integers. +# Write a script to compute the five-number summary of the given set +# of integers. You can find the definition and example in the wikipedia page. +# https://en.wikipedia.org/wiki/Five-number_summary +# +# The five-number summary is a set of descriptive statistics that provides +# information about a dataset. It consists of the five most important +# sample percentiles: +# the sample minimum (smallest observation) +# the lower quartile or first quartile +# the median (the middle value) +# the upper quartile or third quartile +# the sample maximum (largest observation) +# +# Example 1: Input: @int = (0, 0, 1, 2, 63, 61, 27, 13) +# Output: 0.00 0.75 7.50 20.88 35.50 63.00 +#============================================================================= + +use v5.38; +use builtin qw/true false/; no warnings "experimental::builtin"; + +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +exit(!runTest()) if $DoTest; + +sub fiveNumSum(@int) +{ +} + +sub runTest +{ + use Test2::V0; + + is(0, 1, "FAIL"); + + done_testing; +} + diff --git a/challenge-238/bob-lied/perl/ch-1.pl b/challenge-238/bob-lied/perl/ch-1.pl new file mode 100644 index 0000000000..80630659a6 --- /dev/null +++ b/challenge-238/bob-lied/perl/ch-1.pl @@ -0,0 +1,52 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# ch-1.pl Perl Weekly Challenge 238 Task 1 Running Sum +#============================================================================= +# Copyright (c) 2023, Bob Lied +#============================================================================= +# You are given an array of integers. +# Write a script to return the running sum of the given array. The running +# sum can be calculated as sum[i] = num[0] + num[1] + …. + num[i]. +# Example 1 Input: @int = (1, 2, 3, 4, 5) +# Output: (1, 3, 6, 10, 15) +# Example 2 Input: @int = (1, 1, 1, 1, 1) +# Output: (1, 2, 3, 4, 5) +# Example 3 Input: @int = (0, -1, 1, 2) +# Output: (0, -1, 0, 2) +#============================================================================= + +use v5.38; +use builtin qw/true false/; no warnings "experimental::builtin"; + +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +exit(!runTest()) if $DoTest; + +say "(", join(", ", runningSum(@ARGV)->@*), ")"; + +sub runningSum(@int) +{ + my @running; + my $sum = 0; + push @running, $sum += shift @int while @int; + return \@running; +} + +sub runTest +{ + use Test2::V0; + + is( runningSum(1,2,3,4,5), [1,3,6,10,15], "Example 1"); + is( runningSum(1,1,1,1,1), [1,2,3,4,5 ], "Example 2"); + is( runningSum(0,-1,1,2 ), [0,-1,0,2 ], "Example 3"); + + is( runningSum( ), [ ], "Empty list"); + is( runningSum(9234 ), [ 9234 ], "Singleton"); + + done_testing; +} + -- cgit From 0241c1778efa9d9ac165c97d00f3428d758b0983 Mon Sep 17 00:00:00 2001 From: Packy Anderson Date: Mon, 9 Oct 2023 15:45:33 -0400 Subject: Challenge 238 solutions by Packy Anderson Solutions in: * Perl * Raku * Python One blog post! --- challenge-238/packy-anderson/README.md | 92 +++++++++-------------------- challenge-238/packy-anderson/blog.txt | 1 + challenge-238/packy-anderson/perl/ch-1.pl | 31 ++++++++++ challenge-238/packy-anderson/perl/ch-2.pl | 63 ++++++++++++++++++++ challenge-238/packy-anderson/python/ch-1.py | 27 +++++++++ challenge-238/packy-anderson/python/ch-2.py | 68 +++++++++++++++++++++ challenge-238/packy-anderson/raku/ch-1.raku | 30 ++++++++++ challenge-238/packy-anderson/raku/ch-2.raku | 64 ++++++++++++++++++++ 8 files changed, 312 insertions(+), 64 deletions(-) create mode 100644 challenge-238/packy-anderson/blog.txt create mode 100755 challenge-238/packy-anderson/perl/ch-1.pl create mode 100755 challenge-238/packy-anderson/perl/ch-2.pl create mode 100755 challenge-238/packy-anderson/python/ch-1.py create mode 100755 challenge-238/packy-anderson/python/ch-2.py create mode 100755 challenge-238/packy-anderson/raku/ch-1.raku create mode 100755 challenge-238/packy-anderson/raku/ch-2.raku 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..99c9057857 --- /dev/null +++ b/challenge-238/packy-anderson/python/ch-2.py @@ -0,0 +1,68 @@ +#!/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})" + + def compare(a, b): + if step_count[a] != step_count[b]: + # sort by step count + return -1 if step_count[a] < step_count[b] else 1 + elif a != b: + # then sort numerically + return -1 if a < b else 1 + else: + # both items have same step count and value + return 0 + + # now, sort by steps/numeric value + sorted_list = sorted(int_list, key=cmp_to_key(compare)) + + 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 -- cgit From 6c01169c8802364daef753e39555b9ca9f220e13 Mon Sep 17 00:00:00 2001 From: Packy Anderson Date: Mon, 9 Oct 2023 16:11:30 -0400 Subject: Streamline second Python task solution --- challenge-238/packy-anderson/python/ch-2.py | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/challenge-238/packy-anderson/python/ch-2.py b/challenge-238/packy-anderson/python/ch-2.py index 99c9057857..a168a3d4ce 100755 --- a/challenge-238/packy-anderson/python/ch-2.py +++ b/challenge-238/packy-anderson/python/ch-2.py @@ -37,19 +37,9 @@ def persistenceSort(int_list): step_word = 'step' if step_count[num] == 1 else 'steps' steps += f" ({step_count[num]} {step_word})" - def compare(a, b): - if step_count[a] != step_count[b]: - # sort by step count - return -1 if step_count[a] < step_count[b] else 1 - elif a != b: - # then sort numerically - return -1 if a < b else 1 - else: - # both items have same step count and value - return 0 - # now, sort by steps/numeric value - sorted_list = sorted(int_list, key=cmp_to_key(compare)) + sorted_list = sorted(int_list, + key=lambda x: (step_count[x], x)) return sorted_list, steps -- cgit From 16950adfbc568c7d9a85f921b79c0e10355fba57 Mon Sep 17 00:00:00 2001 From: lancew Date: Mon, 9 Oct 2023 22:30:08 +0100 Subject: Lance challenge one --- challenge-238/lance-wicks/perl/ch-1.pl | 19 +++++++++++++++++ challenge-238/lance-wicks/perl/lib/RunningSum.pm | 19 +++++++++++++++++ challenge-238/lance-wicks/perl/t/runningsum.t | 26 ++++++++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 challenge-238/lance-wicks/perl/ch-1.pl create mode 100644 challenge-238/lance-wicks/perl/lib/RunningSum.pm create mode 100644 challenge-238/lance-wicks/perl/t/runningsum.t diff --git a/challenge-238/lance-wicks/perl/ch-1.pl b/challenge-238/lance-wicks/perl/ch-1.pl new file mode 100644 index 0000000000..6ebf5dd7e7 --- /dev/null +++ b/challenge-238/lance-wicks/perl/ch-1.pl @@ -0,0 +1,19 @@ +use strict; +use warnings; + +use lib './lib'; +use RunningSum; + +use Data::Dumper qw(Dumper); + + +my $out = RunningSum->run(@ARGV); + + +print "Input: "; +print join(", ", @ARGV); +print "\n"; + +print "Output: "; +print join(", ", @{$out}); +print "\n"; \ No newline at end of file diff --git a/challenge-238/lance-wicks/perl/lib/RunningSum.pm b/challenge-238/lance-wicks/perl/lib/RunningSum.pm new file mode 100644 index 0000000000..c23528c309 --- /dev/null +++ b/challenge-238/lance-wicks/perl/lib/RunningSum.pm @@ -0,0 +1,19 @@ +package RunningSum; + +use strict; +use warnings; + +sub run { + my $self = shift; + my @input = @_; + + my @out = ( $input[0] ); + + for my $i ( 1 .. $#input ) { + push @out, ( $out[ $i - 1 ] + $input[$i] ); + } + + return \@out; +} + +1; diff --git a/challenge-238/lance-wicks/perl/t/runningsum.t b/challenge-238/lance-wicks/perl/t/runningsum.t new file mode 100644 index 0000000000..34c9e87bf2 --- /dev/null +++ b/challenge-238/lance-wicks/perl/t/runningsum.t @@ -0,0 +1,26 @@ +use Test2::V0 -target => 'RunningSum'; + +subtest "Example 1" => sub { + my @in = ( 1, 2, 3, 4, 5 ); + my @out = ( 1, 3, 6, 10, 15 ); + + my $res = $CLASS->run(@in); + is $res, \@out; +}; + +subtest "Example 2" => sub { + my @in = ( 1, 1, 1, 1, 1 ); + my @out = ( 1, 2, 3, 4, 5 ); + + my $res = $CLASS->run(@in); + is $res, \@out; +}; + +subtest "Example 3" => sub { + my @in = ( 0, -1, 1, 2 ); + my @out = ( 0, -1, 0, 2 ); + + my $res = $CLASS->run(@in); + is $res, \@out; +}; +done_testing; -- cgit From 7cccf2f51d1a1ad5027dcdf2fb9058cd7804808f Mon Sep 17 00:00:00 2001 From: Dave Jacoby Date: Mon, 9 Oct 2023 17:41:11 -0400 Subject: DAJ 238 --- challenge-238/dave-jacoby/blog.txt | 1 + challenge-238/dave-jacoby/perl/ch-1.pl | 28 ++++++++++++++++++++++ challenge-238/dave-jacoby/perl/ch-2.pl | 43 ++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 challenge-238/dave-jacoby/blog.txt create mode 100644 challenge-238/dave-jacoby/perl/ch-1.pl create mode 100644 challenge-238/dave-jacoby/perl/ch-2.pl diff --git a/challenge-238/dave-jacoby/blog.txt b/challenge-238/dave-jacoby/blog.txt new file mode 100644 index 0000000000..3c53d53a96 --- /dev/null +++ b/challenge-238/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby.github.io/2023/10/09/you-cant.html diff --git a/challenge-238/dave-jacoby/perl/ch-1.pl b/challenge-238/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..fb07484a53 --- /dev/null +++ b/challenge-238/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,28 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +my @examples = ( + + [ 1, 2, 3, 4, 5 ], + [ 1, 1, 1, 1, 1 ], + [ 0, -1, 1, 2 ], +); + +for my $e (@examples) { + my @output = running_sum( $e->@* ); + my $input = join ', ', $e->@*; + my $output = join ', ', @output; + say <<~"END"; + Input: \@int = ($input) + Output: ($output) + END +} + +sub running_sum (@int) { + my $c = 0; + my @output = map { $c += $_; $c } @int; + return @output; +} diff --git a/challenge-238/dave-jacoby/perl/ch-2.pl b/challenge-238/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..8b47668fa9 --- /dev/null +++ b/challenge-238/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,43 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +use Algorithm::Permute; +use List::Util qw{ product }; + +my @examples = ( + + [ 15, 99, 1, 34 ], + [ 50, 25, 33, 22 ], +); + +for my $e (@examples) { + my @int = $e->@*; + my $int = join ', ', @int; + my @output = persistence_sort(@int); + my $output = join ', ', @output; + say <<~"END"; + Input: \@int = ($int) + Output: ($output) + END +} + +sub persistence_sort (@nums) { + my @output = + map { $_->[0] } + sort { $a->[1] <=> $b->[1] } + map { [ $_, munge($_) ] } sort @nums; + return @output; +} + +sub munge ($i) { + my $c = 0; + while (1) { + return $c if $i < 10; + $i = product split //, $i; + $c++; + } + return -1; # just in case +} -- cgit From 5be0e6716f9cb08cf3676be8f5e83424f405a729 Mon Sep 17 00:00:00 2001 From: Matthew Neleigh Date: Mon, 9 Oct 2023 20:38:45 -0400 Subject: new file: challenge-238/mattneleigh/perl/ch-1.pl new file: challenge-238/mattneleigh/perl/ch-2.pl --- challenge-238/mattneleigh/perl/ch-1.pl | 57 +++++++++++++++++++ challenge-238/mattneleigh/perl/ch-2.pl | 100 +++++++++++++++++++++++++++++++++ 2 files changed, 157 insertions(+) create mode 100755 challenge-238/mattneleigh/perl/ch-1.pl create mode 100755 challenge-238/mattneleigh/perl/ch-2.pl diff --git a/challenge-238/mattneleigh/perl/ch-1.pl b/challenge-238/mattneleigh/perl/ch-1.pl new file mode 100755 index 0000000000..74a67197fb --- /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( + map( + # Add each number to the sum, with each + # sum going into the list to be returned + $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 + ) + ) + ); + +} + + + -- cgit From cf10a8bfbe66603d7efb6626d7a783662bcb6098 Mon Sep 17 00:00:00 2001 From: Matthew Neleigh Date: Mon, 9 Oct 2023 20:40:56 -0400 Subject: modified: challenge-238/mattneleigh/perl/ch-1.pl --- challenge-238/mattneleigh/perl/ch-1.pl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/challenge-238/mattneleigh/perl/ch-1.pl b/challenge-238/mattneleigh/perl/ch-1.pl index 74a67197fb..cf5adb2a84 100755 --- a/challenge-238/mattneleigh/perl/ch-1.pl +++ b/challenge-238/mattneleigh/perl/ch-1.pl @@ -43,9 +43,9 @@ sub running_sum{ my $sum = 0; return( + # Add each number to the sum, with each + # sum going into the list to be returned map( - # Add each number to the sum, with each - # sum going into the list to be returned $sum += $_, @ARG ) -- cgit From 02a00facfe5f75e8064e792c6c26f8f74b2a2685 Mon Sep 17 00:00:00 2001 From: robbie-hatley Date: Mon, 9 Oct 2023 17:41:55 -0700 Subject: Robbie Hatley's Perl solutions for The Weekly Challenge #238. --- challenge-238/robbie-hatley/blog.txt | 1 + challenge-238/robbie-hatley/perl/ch-1.pl | 113 +++++++++++++++++++++++++++++ challenge-238/robbie-hatley/perl/ch-2.pl | 118 +++++++++++++++++++++++++++++++ 3 files changed, 232 insertions(+) create mode 100644 challenge-238/robbie-hatley/blog.txt create mode 100755 challenge-238/robbie-hatley/perl/ch-1.pl create mode 100755 challenge-238/robbie-hatley/perl/ch-2.pl diff --git a/challenge-238/robbie-hatley/blog.txt b/challenge-238/robbie-hatley/blog.txt new file mode 100644 index 0000000000..a7988dc40c --- /dev/null +++ b/challenge-238/robbie-hatley/blog.txt @@ -0,0 +1 @@ +https://hatley-software.blogspot.com/2023/10/robbie-hatleys-solutions-to-weekly_9.html \ No newline at end of file diff --git a/challenge-238/robbie-hatley/perl/ch-1.pl b/challenge-238/robbie-hatley/perl/ch-1.pl new file mode 100755 index 0000000000..26cafb1ddf --- /dev/null +++ b/challenge-238/robbie-hatley/perl/ch-1.pl @@ -0,0 +1,113 @@ +#!/usr/bin/perl -CSDA + +=pod + +-------------------------------------------------------------------------------------------------------------- +COLOPHON: +This is a 110-character-wide Unicode UTF-8 Perl-source-code text file with hard Unix line breaks ("\x0A"). +¡Hablo Español! Говорю Русский. Björt skjöldur. ॐ नमो भगवते वासुदेवाय. 看的星星,知道你是爱。麦藁雪、富士川町、山梨県。 + +-------------------------------------------------------------------------------------------------------------- +TITLE BLOCK: +Solutions in Perl for The Weekly Challenge 238-1. +Written by Robbie Hatley on Mon Oct 09, 2023. + +-------------------------------------------------------------------------------------------------------------- +PROBLEM DESCRIPTION: +Task 1: Running Sum +Submitted by: Mohammad S Anwar +Given an array of integers*, write a script to return the +running sum of the array. The running sum can be calculated as: +sum[i] = num[0] + num[1] + … + num[i]. + +*[RH Note: this can be done for ANY kind of addable numbers: +integer, real, complex, etc. For for the purpose of this script, +I'll assume all numbers are real (non necessarily integers).] + +Example 1: +Input: (1, 2, 3, 4, 5) +Output: (1, 3, 6, 10, 15) + +Example 2: +Input: (1, 1, 1, 1, 1) +Output: (1, 2, 3, 4, 5) + +Example 3: +Input: (0, -1, 1, 2) +Output: (0, -1, 0, 2) + +-------------------------------------------------------------------------------------------------------------- +PROBLEM NOTES: +This is what's known in mathematics as "the sequence of partial sums of a sequence of numbers", also known as +a "series". A series can be formed for any sequence of addable numbers (integer, real, complex, etc), +both finite and infinite. Some infinite serieses converge to an exact "limit" value; others diverge and +have no limit. As for computation details, there are many ways to do it, all equivalent. I think I'll start +by making an array "@series", the push $$aref[0] to it, then for each element of @$aref from index $idx = 1 +onward, do "$series[$idx] = $series[$idx-1] + $$aref[$idx]". + +-------------------------------------------------------------------------------------------------------------- +IO NOTES: +Input is via either built-in variables or via @ARGV. If using @ARGV, provide one argument which must be a +single-quoted array of arrays of real numbers, in proper Perl syntax, like so: +./ch-1.pl '([1, 3, 5, 7], [4.3, -2.7, 6.8, -5.1, 8.7])' + +Output is to STDOUT and will be each input array followed by the corresponding output. + +=cut + +# ------------------------------------------------------------------------------------------------------------ +# PRAGMAS AND MODULES USED: + +use v5.38; +use strict; +use warnings; +use utf8; +use warnings FATAL => 'utf8'; +use Sys::Binmode; +use Time::HiRes 'time'; + +# ------------------------------------------------------------------------------------------------------------ +# START TIMER: +our $t0; BEGIN {$t0 = time} + +# ------------------------------------------------------------------------------------------------------------ +# SUBROUTINES: + +sub series ($aref) { + my @series; + $series[0] = $$aref[0]; + for ( my $idx = 1 ; $idx <= $#$aref ; ++$idx ) { + $series[$idx] = $series[$idx-1]+$$aref[$idx]; + } + return @series; +} + +# ------------------------------------------------------------------------------------------------------------ +# MAIN BODY OF PROGRAM: + +# Inputs: +my @arrays = @ARGV ? eval($ARGV[0]) : +( + # Example 1 input: + [1, 2, 3, 4, 5], + + # Example 2 input: + [1, 1, 1, 1, 1], + + # Example 3 input: + [0, -1, 1, 2], +); + +# Main loop: +for my $aref (@arrays) { + my @series = series($aref); + say ''; + say 'sequence = (', join(', ',@$aref ), ')'; + say 'series = (', join(', ',@series), ')'; +} +exit; + +# ------------------------------------------------------------------------------------------------------------ +# DETERMINE AND PRINT EXECUTION TIME: +END {my $µs = 1000000 * (time - $t0);printf("\nExecution time was %.0fµs.\n", $µs)} +__END__ diff --git a/challenge-238/robbie-hatley/perl/ch-2.pl b/challenge-238/robbie-hatley/perl/ch-2.pl new file mode 100755 index 0000000000..0fb180b7f0 --- /dev/null +++ b/challenge-238/robbie-hatley/perl/ch-2.pl @@ -0,0 +1,118 @@ +#!/usr/bin/perl -CSDA + +=pod + +-------------------------------------------------------------------------------------------------------------- +COLOPHON: +This is a 110-character-wide Unicode UTF-8 Perl-source-code text file with hard Unix line breaks ("\x0A"). +¡Hablo Español! Говорю Русский. Björt skjöldur. ॐ नमो भगवते वासुदेवाय. 看的星星,知道你是爱。麦藁雪、富士川町、山梨県。 + +-------------------------------------------------------------------------------------------------------------- +TITLE BLOCK: +Solutions in Perl for The Weekly Challenge 238-2. +Written by Robbie Hatley on Mon Oct 09, 2023. + +-------------------------------------------------------------------------------------------------------------- +PROBLEM DESCRIPTION: +Task 2: Persistence Sort +Submitted by: Mohammad S Anwar +Given an array of positive integers, write a script to sort the +array in increasing order with respect to the count of steps +required to obtain a single-digit number by multiplying its digits +recursively for each array element. If any two numbers have the +same count of steps, then print the smaller number first. + +Example 1: +Input: @int = (15, 99, 1, 34) +Output: (1, 15, 34, 99) +15 => 1 x 5 => 5 (1 step) +99 => 9 x 9 => 81 => 8 x 1 => 8 (2 steps) +1 => 0 step +34 => 3 x 4 => 12 => 1 x 2 => 2 (2 steps) + +Example 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) + +-------------------------------------------------------------------------------------------------------------- +PROBLEM NOTES: +This just cries-out for the "sort compare @$aref" form of "sort". I'll combine both the "persistence" +and "value" criteria in a single function called "by_persistence", then do this: +my @sorted = sort by_persistence @$aref; + +-------------------------------------------------------------------------------------------------------------- +IO NOTES: +Input is via either built-in variables or via @ARGV. If using @ARGV, provide one argument which must be a +single-quoted array of arrays of positive integers, in proper Perl syntax, like so: +./ch-2.pl '([37, 54, 82, 112], [234, 345, 456, 567])' + +Output is to STDOUT and will be each input array followed by the corresponding output. + +=cut + +# ------------------------------------------------------------------------------------------------------------ +# PRAGMAS AND MODULES USED: + +use v5.38; +use strict; +use warnings; +use utf8; +use warnings FATAL => 'utf8'; +use Sys::Binmode; +use Time::HiRes 'time'; +use List::Util 'product'; + +# ------------------------------------------------------------------------------------------------------------ +# START TIMER: +our $t0; BEGIN {$t0 = time} + +# ------------------------------------------------------------------------------------------------------------ +# SUBROUTINES: + +sub persistence ($x) { + my $persistence = 0 ; + my @digits = () ; + my $digits = 0 ; + while ( ($digits = scalar(@digits = split(//,$x))) > 1 ) { + $x = product @digits; + ++$persistence; + } + return $persistence; +} + +sub by_persistence { + my $cmp = persistence($a) <=> persistence($b); + if ( 0 == $cmp ) {$cmp = ($a <=> $b)} + return $cmp; +} + +# ------------------------------------------------------------------------------------------------------------ +# MAIN BODY OF PROGRAM: + +# Inputs: +my @arrays = @ARGV ? eval($ARGV[0]) : +( + # Example 1 input: + [15, 99, 1, 34], + + # Example 2 input: + [50, 25, 33, 22], +); + +# Main loop: +for my $aref (@arrays) { + my @sorted = sort by_persistence @$aref; + say ''; + say 'original unsorted array = (', join(', ',@$aref ), ')'; + say 'persistence-sorted array = (', join(', ',@sorted), ')'; +} +exit; + +# ------------------------------------------------------------------------------------------------------------ +# DETERMINE AND PRINT EXECUTION TIME: +END {my $µs = 1000000 * (time - $t0);printf("\nExecution time was %.0fµs.\n", $µs)} +__END__ -- cgit From de215720df537a15de474f0125f2994651be09c5 Mon Sep 17 00:00:00 2001 From: Michael Firkins Date: Tue, 10 Oct 2023 12:48:49 +1100 Subject: pwc238 solution in python --- challenge-238/pokgopun/python/ch-1.py | 45 ++++++++++++++++++++++ challenge-238/pokgopun/python/ch-2.py | 70 +++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 challenge-238/pokgopun/python/ch-1.py create mode 100644 challenge-238/pokgopun/python/ch-2.py diff --git a/challenge-238/pokgopun/python/ch-1.py b/challenge-238/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..9ac57e1aa2 --- /dev/null +++ b/challenge-238/pokgopun/python/ch-1.py @@ -0,0 +1,45 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-238/ +""" + +Task 1: Running Sum + +Submitted by: [43]Mohammad S Anwar + __________________________________________________________________ + + You are given an array of integers. + + Write a script to return the running sum of the given array. The + running sum can be calculated as sum[i] = num[0] + num[1] + …. + + num[i]. + +Example 1 + +Input: @int = (1, 2, 3, 4, 5) +Output: (1, 3, 6, 10, 15) + +Example 2 + +Input: @int = (1, 1, 1, 1, 1) +Output: (1, 2, 3, 4, 5) + +Example 3 + +Input: @int = (0, -1, 1, 2) +Output: (0, -1, 0, 2) + +Task 2: Persistence Sort +""" +### solution by pokgopun@gmail.com + +def runningSum(tup): + lst = list(tup) + for i in range(1,len(lst)): + lst[i] += lst[i-1] + return tuple(lst) + +for inpt, otpt in { + (1, 2, 3, 4, 5):(1, 3, 6, 10, 15), + (1, 1, 1, 1, 1):(1, 2, 3, 4, 5), + (0, -1, 1, 2):(0, -1, 0, 2), + }.items(): + print(runningSum(inpt)==otpt) diff --git a/challenge-238/pokgopun/python/ch-2.py b/challenge-238/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..086194f65a --- /dev/null +++ b/challenge-238/pokgopun/python/ch-2.py @@ -0,0 +1,70 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-238/ +""" + +Task 2: Persistence Sort + +Submitted by: [44]Mohammad S Anwar + __________________________________________________________________ + + You are given an array of positive integers. + + Write a script to sort the given array in increasing order with respect + to the count of steps required to obtain a single-digit number by + multiplying its digits recursively for each array element. If any two + numbers have the same count of steps, then print the smaller number + first. + +Example 1 + +Input: @int = (15, 99, 1, 34) +Output: (1, 15, 34, 99) + +15 => 1 x 5 => 5 (1 step) +99 => 9 x 9 => 81 => 8 x 1 => 8 (2 steps) +1 => 0 step +34 => 3 x 4 => 12 => 1 x 2 => 2 (2 steps) + +Example 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) + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 15th October + 2023. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +def mk1dgt(num): + i = 0 + while num > 9: + i += 1 + num = eval( + "*".join( + map( + lambda x: chr(x), bytes(str(num),"ascii") + ) + ) + ) + return i + +def pSort(tup): + lst = sorted(list(tup)) + lst.sort(key=mk1dgt) + return tuple(lst) + +for inpt, otpt in { + (15, 99, 1, 34):(1, 15, 34, 99), + (50, 25, 33, 22):(22, 33, 50, 25), + }.items(): + print(pSort(inpt)==otpt) + + -- cgit From 962c2aa4bebf9d52f59e1384e804fc620afa7cd6 Mon Sep 17 00:00:00 2001 From: Michael Firkins Date: Tue, 10 Oct 2023 13:45:39 +1100 Subject: pwc238 solution in go --- challenge-238/pokgopun/go/ch-1.go | 59 ++++++++++++++++++++++++++++ challenge-238/pokgopun/go/ch-2.go | 82 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+) create mode 100644 challenge-238/pokgopun/go/ch-1.go create mode 100644 challenge-238/pokgopun/go/ch-2.go diff --git a/challenge-238/pokgopun/go/ch-1.go b/challenge-238/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..e429633daf --- /dev/null +++ b/challenge-238/pokgopun/go/ch-1.go @@ -0,0 +1,59 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-238/ +/*# + +Task 1: Running Sum + +Submitted by: [43]Mohammad S Anwar + __________________________________________________________________ + + You are given an array of integers. + + Write a script to return the running sum of the given array. The + running sum can be calculated as sum[i] = num[0] + num[1] + …. + + num[i]. + +Example 1 + +Input: @int = (1, 2, 3, 4, 5) +Output: (1, 3, 6, 10, 15) + +Example 2 + +Input: @int = (1, 1, 1, 1, 1) +Output: (1, 2, 3, 4, 5) + +Example 3 + +Input: @int = (0, -1, 1, 2) +Output: (0, -1, 0, 2) + +Task 2: Persistence Sort +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "fmt" + "slices" +) + +func main() { + for _, data := range []struct { + input, output []int + }{ + {[]int{1, 2, 3, 4, 5}, []int{1, 3, 6, 10, 15}}, + {[]int{1, 1, 1, 1, 1}, []int{1, 2, 3, 4, 5}}, + {[]int{0, -1, 1, 2}, []int{0, -1, 0, 2}}, + } { + fmt.Println(slices.Equal(runningSum(data.input), data.output)) + } +} + +func runningSum(s []int) []int { + l := len(s) + for i := 1; i < l; i++ { + s[i] += s[i-1] + } + return s +} diff --git a/challenge-238/pokgopun/go/ch-2.go b/challenge-238/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..b0cff2d66e --- /dev/null +++ b/challenge-238/pokgopun/go/ch-2.go @@ -0,0 +1,82 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-238/ +/*# + +Task 2: Persistence Sort + +Submitted by: [44]Mohammad S Anwar + __________________________________________________________________ + + You are given an array of positive integers. + + Write a script to sort the given array in increasing order with respect + to the count of steps required to obtain a single-digit number by + multiplying its digits recursively for each array element. If any two + numbers have the same count of steps, then print the smaller number + first. + +Example 1 + +Input: @int = (15, 99, 1, 34) +Output: (1, 15, 34, 99) + +15 => 1 x 5 => 5 (1 step) +99 => 9 x 9 => 81 => 8 x 1 => 8 (2 steps) +1 => 0 step +34 => 3 x 4 => 12 => 1 x 2 => 2 (2 steps) + +Example 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) + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 15th October + 2023. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "fmt" + "slices" + "sort" + "strconv" +) + +func main() { + for _, data := range []struct { + input, output []int + }{ + {[]int{15, 99, 1, 34}, []int{1, 15, 34, 99}}, + {[]int{50, 25, 33, 22}, []int{22, 33, 50, 25}}, + } { + sort.SliceStable(data.input, func(i, j int) bool { + return true + }) + sort.SliceStable(data.input, func(i, j int) bool { + return mk1dgt(data.input[i]) < mk1dgt(data.input[j]) + }) + fmt.Println(slices.Equal(data.input, data.output)) + } +} + +func mk1dgt(n int) (i int) { + for n > 9 { + i++ + r := 1 + for _, v := range strconv.Itoa(n) { + r *= int(v) - 48 + } + n = r + } + return i +} -- cgit From 3d85f4292ea069c71eeb945228aeffff18d713c1 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 9 Oct 2023 19:00:18 +0200 Subject: PWC 238 Task 1 Raku done Task 2 Raku done Task 1 PL/Perl done Task 2 PL/Perl done Task 1 SQL done Task 2 PL/PgSQL done Another possible implementation of the second task in PL/PgSQL Add python solutions. --- challenge-238/luca-ferrari/blog-1.txt | 1 + challenge-238/luca-ferrari/blog-2.txt | 1 + challenge-238/luca-ferrari/blog-3.txt | 1 + challenge-238/luca-ferrari/blog-4.txt | 1 + challenge-238/luca-ferrari/blog-5.txt | 1 + challenge-238/luca-ferrari/blog-6.txt | 1 + challenge-238/luca-ferrari/blog-7.txt | 1 + challenge-238/luca-ferrari/postgresql/ch-1.plperl | 24 ++++++++++ challenge-238/luca-ferrari/postgresql/ch-1.sql | 18 +++++++ challenge-238/luca-ferrari/postgresql/ch-2.2.sql | 53 +++++++++++++++++++++ challenge-238/luca-ferrari/postgresql/ch-2.plperl | 50 ++++++++++++++++++++ challenge-238/luca-ferrari/postgresql/ch-2.sql | 57 +++++++++++++++++++++++ challenge-238/luca-ferrari/python/ch-1.py | 22 +++++++++ challenge-238/luca-ferrari/python/ch-2.py | 42 +++++++++++++++++ challenge-238/luca-ferrari/raku/ch-1.p6 | 17 +++++++ challenge-238/luca-ferrari/raku/ch-2.p6 | 26 +++++++++++ 16 files changed, 316 insertions(+) create mode 100644 challenge-238/luca-ferrari/blog-1.txt create mode 100644 challenge-238/luca-ferrari/blog-2.txt create mode 100644 challenge-238/luca-ferrari/blog-3.txt create mode 100644 challenge-238/luca-ferrari/blog-4.txt create mode 100644 challenge-238/luca-ferrari/blog-5.txt create mode 100644 challenge-238/luca-ferrari/blog-6.txt create mode 100644 challenge-238/luca-ferrari/blog-7.txt create mode 100644 challenge-238/luca-ferrari/postgresql/ch-1.plperl create mode 100644 challenge-238/luca-ferrari/postgresql/ch-1.sql create mode 100644 challenge-238/luca-ferrari/postgresql/ch-2.2.sql create mode 100644 challenge-238/luca-ferrari/postgresql/ch-2.plperl create mode 100644 challenge-238/luca-ferrari/postgresql/ch-2.sql create mode 100644 challenge-238/luca-ferrari/python/ch-1.py create mode 100644 challenge-238/luca-ferrari/python/ch-2.py create mode 100644 challenge-238/luca-ferrari/raku/ch-1.p6 create mode 100644 challenge-238/luca-ferrari/raku/ch-2.p6 diff --git a/challenge-238/luca-ferrari/blog-1.txt b/challenge-238/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..934bfbed25 --- /dev/null +++ b/challenge-238/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/10/09/PerlWeeklyChallenge238.html#task1 diff --git a/challenge-238/luca-ferrari/blog-2.txt b/challenge-238/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..b99739a572 --- /dev/null +++ b/challenge-238/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/10/09/PerlWeeklyChallenge238.html#task2 diff --git a/challenge-238/luca-ferrari/blog-3.txt b/challenge-238/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..4dfe404108 --- /dev/null +++ b/challenge-238/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/10/09/PerlWeeklyChallenge238.html#task1plperl diff --git a/challenge-238/luca-ferrari/blog-4.txt b/challenge-238/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..2f0f8a781f --- /dev/null +++ b/challenge-238/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/10/09/PerlWeeklyChallenge238.html#task2plperl diff --git a/challenge-238/luca-ferrari/blog-5.txt b/challenge-238/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..6e864012bf --- /dev/null +++ b/challenge-238/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/10/09/PerlWeeklyChallenge238.html#task1plpgsql diff --git a/challenge-238/luca-ferrari/blog-6.txt b/challenge-238/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..7893f25aa6 --- /dev/null +++ b/challenge-238/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/10/09/PerlWeeklyChallenge238.html#task2plpgsql diff --git a/challenge-238/luca-ferrari/blog-7.txt b/challenge-238/luca-ferrari/blog-7.txt new file mode 100644 index 0000000000..281cdbbfda --- /dev/null +++ b/challenge-238/luca-ferrari/blog-7.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/10/09/PerlWeeklyChallenge238.html#task2plpgsql_b diff --git a/challenge-238/luca-ferrari/postgresql/ch-1.plperl b/challenge-238/luca-ferrari/postgresql/ch-1.plperl new file mode 100644 index 0000000000..d3c64a1cf1 --- /dev/null +++ b/challenge-238/luca-ferrari/postgresql/ch-1.plperl @@ -0,0 +1,24 @@ +-- +-- Perl Weekly Challenge 238 +-- Task 1 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc238; + +CREATE OR REPLACE FUNCTION +pwc238.task1_plperl( int[] ) +RETURNS SETOF int +AS $CODE$ + my ( $nums ) = @_; + + for my $index ( 0 .. $nums->@* - 1 ) { + my $sum = 0; + + $sum += $_ for ( $nums->@[ 0 .. $index ] ); + return_next( $sum ); + } + + return undef; +$CODE$ +LANGUAGE plperl; diff --git a/challenge-238/luca-ferrari/postgresql/ch-1.sql b/challenge-238/luca-ferrari/postgresql/ch-1.sql new file mode 100644 index 0000000000..3663306f9f --- /dev/null +++ b/challenge-238/luca-ferrari/postgresql/ch-1.sql @@ -0,0 +1,18 @@ +-- +-- Perl Weekly Challenge 238 +-- Task 1 +-- +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc238; + +CREATE OR REPLACE FUNCTION +pwc238.task1_plpgsql( nums int[] ) +RETURNS TABLE( v int, s int ) +AS $CODE$ + + SELECT v, sum( v ) OVER ( ORDER BY v ) + FROM unnest( nums ) v; +$CODE$ +LANGUAGE sql; diff --git a/challenge-238/luca-ferrari/postgresql/ch-2.2.sql b/challenge-238/luca-ferrari/postgresql/ch-2.2.sql new file mode 100644 index 0000000000..d1456ab3ee --- /dev/null +++ b/challenge-238/luca-ferrari/postgresql/ch-2.2.sql @@ -0,0 +1,53 @@ +CREATE OR REPLACE FUNCTION pwc238.reduce( n int ) +RETURNS int +AS $CODE$ +DECLARE + current_value int; + step_counter int; + digit text; + multiplication int; + +BEGIN + current_value := n; + step_counter := 0; + + WHILE current_value > 9 LOOP + multiplication := 1; + step_counter := step_counter + 1; + + FOREACH digit IN ARRAY regexp_split_to_array( current_value::text, '' ) LOOP + multiplication := multiplication * digit::int; + END LOOP; + + current_value := multiplication; + END LOOP; + + RETURN step_counter; +END +$CODE$ +LANGUAGE plpgsql; + + + +-- +-- Function task2_plpgsql +-- Schema pwc238 +-- +-- Description: +-- +-- +-- Return Type: SETOF INT +-- +CREATE OR REPLACE FUNCTION +pwc238.task2_plpgsql( nums int[] ) +RETURNS SETOF INT +AS $CODE$ + +SELECT v +FROM unnest( nums ) v +ORDER BY pwc238.reduce( v ), v; + +$CODE$ +LANGUAGE sql +VOLATILE +; diff --git a/challenge-238/luca-ferrari/postgresql/ch-2.plperl b/challenge-238/luca-ferrari/postgresql/ch-2.plperl new file mode 100644 index 0000000000..9a2cbcccbd --- /dev/null +++ b/challenge-238/luca-ferrari/postgresql/ch-2.plperl @@ -0,0 +1,50 @@ +-- +-- Perl Weekly Challenge 238 +-- Task 2 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc238; + +CREATE OR REPLACE FUNCTION +pwc238.task2_plperl( int[] ) +RETURNS SETOF int +AS $CODE$ + my ( $nums ) = @_; + + my $steps = {}; + + # utility function to reduce a number + # does only one pass so that I can counter + # how many passes are required + my $reduce = sub { + my ( $number ) = @_; + return $number if ( $number <= 9 ); + + my $value = 1; + for my $digit ( split( '', $number ) ) { + $value *= $digit; + } + + return $value; + }; + + for ( $nums->@* ) { + my $step_counter = 0; + my $value = $_; + + while ( $value > 9 ) { + $value = $reduce->( $value ); + $step_counter++; + } + + push $steps->{ $step_counter }->@*, $_; + } + + for my $key ( sort keys $steps->%* ) { + return_next( $_ ) for ( sort $steps->{ $key }->@* ) + } + + return undef; +$CODE$ +LANGUAGE plperl; diff --git a/challenge-238/luca-ferrari/postgresql/ch-2.sql b/challenge-238/luca-ferrari/postgresql/ch-2.sql new file mode 100644 index 0000000000..95d6555633 --- /dev/null +++ b/challenge-238/luca-ferrari/postgresql/ch-2.sql @@ -0,0 +1,57 @@ +-- +-- Perl Weekly Challenge 238 +-- Task 2 +-- +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc238; + +CREATE OR REPLACE FUNCTION +pwc238.task2_plpgsql( nums int[] ) +RETURNS SETOF int +AS $CODE$ +DECLARE + current_value int; + digit text; + multiplication int; + step_counter int; + value_to_insert int; +BEGIN + CREATE TEMPORARY TABLE IF NOT EXISTS mul( v int, mul int, steps int DEFAULT 0 ); + TRUNCATE mul; + + FOREACH current_value IN ARRAY nums LOOP + IF current_value < 9 THEN + INSERT INTO mul( v, mul ) + VALUES( current_value, current_value ); + CONTINUE; + END IF; + + -- if here the number is at least two digits long + step_counter := 0; + value_to_insert := current_value; + WHILE current_value > 9 LOOP + multiplication := 1; + step_counter := step_counter + 1; + + FOREACH digit IN ARRAY regexp_split_to_array( current_value::text, '' ) LOOP + multiplication := multiplication * digit::int; + END LOOP; + + current_value := multiplication; + END LOOP; + + INSERT INTO mul( v, mul, steps ) + VALUES( value_to_insert, multiplication, step_counter ); + + END LOOP; + + + RETURN QUERY SELECT v + FROM mul + ORDER BY steps ASC, v ASC; +END + +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-238/luca-ferrari/python/ch-1.py b/challenge-238/luca-ferrari/python/ch-1.py new file mode 100644 index 0000000000..67507eb558 --- /dev/null +++ b/challenge-238/luca-ferrari/python/ch-1.py @@ -0,0 +1,22 @@ +#!python + +import sys + +def main( argv ): + running_sum = [] + current_index = 0 + while current_index < len( argv ): + running_sum.insert( current_index, 0 ) + + for n in argv[ 0 : current_index ]: + running_sum[ current_index ] += int( n ) + + current_index += 1 + + print( ", ".join( map( str, running_sum ) ) ) + + + + +if __name__ == '__main__': + main( sys.argv[ 1: ] ) diff --git a/challenge-238/luca-ferrari/python/ch-2.py b/challenge-238/luca-ferrari/python/ch-2.py new file mode 100644 index 0000000000..183d67e65d --- /dev/null +++ b/challenge-238/luca-ferrari/python/ch-2.py @@ -0,0 +1,42 @@ +#!python + +import sys +import collections + +def reduce( n ): + if n <= 9: + return n + + multiplication = 1 + for digit in map( int, str( n ) ): + multiplication *= digit + + return multiplication + + +def main( argv ): + steps = {} + for n in map( int, argv ): + current_step = 0 + if n > 9: + # need reduction + current_value = n + while current_value > 9: + current_value = reduce( current_value ) + current_step += 1 + + # if the key is not here, create a list + if not str( current_step ) in steps: + steps[ str( current_step ) ] = [] + + steps[ str(current_step) ].append( n ) + + # now traverse the dictionary and sort the array + # and print it + for k, v in collections.OrderedDict(sorted(steps.items())).items(): + print( ", ".join( map( str, v ) ) ) + + + +if __name__ == '__main__': + main( sys.argv[ 1: ] ) diff --git a/challenge-238/luca-ferrari/raku/ch-1.p6 b/challenge-238/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..2d6d317076 --- /dev/null +++ b/challenge-238/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,17 @@ +#!raku + +# +# Perl Weekly Challenge 238 +# Task 1 +# +# See +# + +sub MAIN( *@nums where { @nums.grep( * ~~ Int ).elems == @nums.elems } ) { + my @running-sum; + for 0 ..^ @nums.elems -> $index { + @running-sum[ $index ] = [+] @nums[ 0 .. $index ]; + } + + @running-sum.join( ', ' ).say; +} diff --git a/challenge-238/luca-ferrari/raku/ch-2.p6 b/challenge-238/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..fe8a4438a4 --- /dev/null +++ b/challenge-238/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,26 @@ +#!raku + +# +# Perl Weekly Challenge 238 +# Task 2 +# +# See +# + +sub MAIN( *@nums where { @nums.grep( { $_ ~~ Int && $_ > 0 } ).elems == @nums.elems } ) { + my %steps; + for @nums { + my $step-counter = 0; + my $value = $_; + while ( $value > 9 ) { + $value = [*] $value.comb; + $step-counter++; + } + + %steps{ $step-counter }.push: $_; + } + + my @running-sort.push: | %steps{ $_ }.sort for %steps.keys.sort; + @running-sort.join( ', ' ).say; + +} -- cgit From a46ff5fc2efcb44bbb4e4d40a842c84522bbc81e Mon Sep 17 00:00:00 2001 From: Humberto Massa Date: Tue, 10 Oct 2023 11:00:50 -0300 Subject: one-liner raku solutions --- challenge-238/massa/raku/ch-1.raku | 63 ++++++++++++++++++++++++++++++++ challenge-238/massa/raku/ch-2.raku | 74 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+) create mode 100644 challenge-238/massa/raku/ch-1.raku create mode 100644 challenge-238/massa/raku/ch-2.raku diff --git a/challenge-238/massa/raku/ch-1.raku b/cha