From 34c6c70c5490226eb97bdbcdab3c2c5ecad6b608 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 9 Oct 2023 04:02:48 +0000 Subject: Challenge 238 Solutions (Raku) --- challenge-238/mark-anderson/raku/ch-1.raku | 6 ++++++ challenge-238/mark-anderson/raku/ch-2.raku | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 challenge-238/mark-anderson/raku/ch-1.raku create mode 100644 challenge-238/mark-anderson/raku/ch-2.raku diff --git a/challenge-238/mark-anderson/raku/ch-1.raku b/challenge-238/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..23f7e08675 --- /dev/null +++ b/challenge-238/mark-anderson/raku/ch-1.raku @@ -0,0 +1,6 @@ +#!/usr/bin/env raku +use Test; + +is-deeply ([\+] 1,2,3,4,5), (1,3,6,10,15); +is-deeply ([\+] 1,1,1,1,1), (1,2,3,4,5); +is-deeply ([\+] 0,-1,1,2), (0,-1,0,2); diff --git a/challenge-238/mark-anderson/raku/ch-2.raku b/challenge-238/mark-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..6422d0ea3b --- /dev/null +++ b/challenge-238/mark-anderson/raku/ch-2.raku @@ -0,0 +1,19 @@ +#!/usr/bin/env raku +use Test; + +is persistence(277777788888899), 11; +is-deeply persistence(39,999,4,9876), (4,9876,39,999); +is-deeply persistence(1,27,90), (1,90,27); +is-deeply persistence(868,769,976,679), (679,769,868,976); +is-deeply persistence(15,99,1,34), (1,15,34,99); +is-deeply persistence(50,25,33,22), (22,33,50,25); + +multi persistence(+@a) +{ + @a.sort: { persistence($_), $_ } +} + +multi persistence($_ is copy) +{ + sum do while .chars > 1 { $_ = [*] .comb; 1 } +} -- cgit From e163bf389e3f5a3c6f66da31083a777c6819785b Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Mon, 9 Oct 2023 09:58:59 +0200 Subject: Add solutions to 238: Running Sum & Persistence Sort by E. Choroba --- challenge-238/e-choroba/perl/ch-1.pl | 17 +++++++++++++++++ challenge-238/e-choroba/perl/ch-2.pl | 26 ++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100755 challenge-238/e-choroba/perl/ch-1.pl create mode 100755 challenge-238/e-choroba/perl/ch-2.pl diff --git a/challenge-238/e-choroba/perl/ch-1.pl b/challenge-238/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..108c79e992 --- /dev/null +++ b/challenge-238/e-choroba/perl/ch-1.pl @@ -0,0 +1,17 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub running_sum(@int) { + my @running_sum = shift @int; + push @running_sum, $running_sum[-1] + shift @int while @int; + return \@running_sum +} + +use Test2::V0; +plan 3; + +is running_sum(1, 2, 3, 4, 5), [1, 3, 6, 10, 15], 'Example 1'; +is running_sum(1, 1, 1, 1, 1), [1, 2, 3, 4, 5], 'Example 2'; +is running_sum(0, -1, 1, 2), [0, -1, 0, 2], 'Example 3'; diff --git a/challenge-238/e-choroba/perl/ch-2.pl b/challenge-238/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..9c689dc07f --- /dev/null +++ b/challenge-238/e-choroba/perl/ch-2.pl @@ -0,0 +1,26 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +use List::Util qw{ product }; + +sub persistence_sort(@int) { + return [ sort { steps($a) <=> steps($b) || $a <=> $b } @int ] +} + +sub steps($n) { + my $steps = 0; + while (length($n) > 1) { + my @digits = split //, $n; + $n = product(@digits); + ++$steps + } + return $steps +} + +use Test2::V0; +plan 2; + +is persistence_sort(15, 99, 1, 34), [1, 15, 34, 99], 'Example 1'; +is persistence_sort(50, 25, 33, 22), [22, 33, 50, 25], 'Example 2'; -- cgit From bb8463d0f0cbc381910d2703ca6493a4d7bbad64 Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 9 Oct 2023 08:32:33 +0000 Subject: w238 - Task 1 & 2 --- challenge-238/perlboy1967/perl/ch1.pl | 37 ++++++++++++++++++++++++ challenge-238/perlboy1967/perl/ch2.pl | 54 +++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100755 challenge-238/perlboy1967/perl/ch1.pl create mode 100755 challenge-238/perlboy1967/perl/ch2.pl diff --git a/challenge-238/perlboy1967/perl/ch1.pl b/challenge-238/perlboy1967/perl/ch1.pl new file mode 100755 index 0000000000..159ba18e4b --- /dev/null +++ b/challenge-238/perlboy1967/perl/ch1.pl @@ -0,0 +1,37 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 238 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-238 + +Author: Niels 'PerlBoy' van Dijke + +Task 1: Running Sum +Submitted by: 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]. + +=cut + +use v5.32; +use common::sense; +use feature 'signatures'; + +use Test::More; +use Test::Deep qw(cmp_deeply); + + +sub runningSum (@numbers) { + my $s = 0; + return map { $s += $_; } @numbers; +} + +cmp_deeply([runningSum(1,2,3,4,5)],[1,3,6,10,15]); +cmp_deeply([runningSum(1,1,1,1,1)],[1,2,3,4,5]); +cmp_deeply([runningSum(0,-1,1,2)],[0,-1,0,2]); + +done_testing; diff --git a/challenge-238/perlboy1967/perl/ch2.pl b/challenge-238/perlboy1967/perl/ch2.pl new file mode 100755 index 0000000000..c1e3b5be91 --- /dev/null +++ b/challenge-238/perlboy1967/perl/ch2.pl @@ -0,0 +1,54 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 238 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-238 + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Persistence Sort +Submitted by: 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. + +=cut + +use v5.32; +use common::sense; +use feature 'signatures'; + +use Test::More; +use Test::Deep qw(cmp_deeply); + +use List::MoreUtils qw(slide); +use Memoize; + +memoize 'cnt_steps'; + +sub cnt_steps($i) { + my $n = 0; + + while (1) { + my @d = split(//,$i); + last if (@d == 1); + $i = slide { $a * $b } @d; + $n++; + } + + return $n; +} + +sub persistenceSort (@numbers) { + sort { cnt_steps($a) <=> cnt_steps($b) || $a <=> $b} @numbers; +} + +cmp_deeply([persistenceSort(15,99,1,34)],[1,15,34,99]); +cmp_deeply([persistenceSort(50,25,33,22)],[22,33,50,25]); + +done_testing; -- cgit From 34a882b97947ad6bb1c13273e228e64a20a93d9b Mon Sep 17 00:00:00 2001 From: Steven Wilson Date: Mon, 9 Oct 2023 13:00:06 +0100 Subject: fix for week 235 task 1 --- challenge-235/steven-wilson/python/ch-01.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/challenge-235/steven-wilson/python/ch-01.py b/challenge-235/steven-wilson/python/ch-01.py index 4d5a26bae5..e128a00985 100644 --- a/challenge-235/steven-wilson/python/ch-01.py +++ b/challenge-235/steven-wilson/python/ch-01.py @@ -9,12 +9,16 @@ def remove_one(elements): False >>> remove_one( [2, 2, 3] ) True + >>> remove_one( [1, 2, 3, 1, 2, 3] ) + False """ remove = 0 for i in range(len(elements) - 1): if(elements[i] > elements[i+1]): remove += 1 - if remove == 2: + if (i > 0) and (elements[i-1] > elements[i+1]): + remove += 1 + if remove > 1: return False return True -- cgit From 5966a37da10a8d575ce14270e7c4372f8d613666 Mon Sep 17 00:00:00 2001 From: Steven Wilson Date: Mon, 9 Oct 2023 14:51:13 +0100 Subject: another edge case fix --- challenge-235/steven-wilson/python/ch-01.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/challenge-235/steven-wilson/python/ch-01.py b/challenge-235/steven-wilson/python/ch-01.py index e128a00985..b7cbbe1543 100644 --- a/challenge-235/steven-wilson/python/ch-01.py +++ b/challenge-235/steven-wilson/python/ch-01.py @@ -11,12 +11,14 @@ def remove_one(elements): True >>> remove_one( [1, 2, 3, 1, 2, 3] ) False + >>> remove_one( [1, 2, 3, 1]) + True """ remove = 0 for i in range(len(elements) - 1): if(elements[i] > elements[i+1]): remove += 1 - if (i > 0) and (elements[i-1] > elements[i+1]): + if (i > 0) and (i < len(elements) - 2) and (elements[i-1] > elements[i+1]): remove += 1 if remove > 1: return False -- cgit From 9179e64041ba6282c7a421615e78de6e83b8c1d5 Mon Sep 17 00:00:00 2001 From: Luis Mochan Date: Mon, 9 Oct 2023 10:31:07 -0600 Subject: Solve PWC238 --- challenge-238/wlmb/blog.txt | 1 + challenge-238/wlmb/perl/ch-1.pl | 13 +++++++++++++ challenge-238/wlmb/perl/ch-2.pl | 22 ++++++++++++++++++++++ 3 files changed, 36 insertions(+) create mode 100644 challenge-238/wlmb/blog.txt create mode 100755 challenge-238/wlmb/perl/ch-1.pl create mode 100755 challenge-238/wlmb/perl/ch-2.pl diff --git a/challenge-238/wlmb/blog.txt b/challenge-238/wlmb/blog.txt new file mode 100644 index 0000000000..9e75c14c50 --- /dev/null +++ b/challenge-238/wlmb/blog.txt @@ -0,0 +1 @@ +https://wlmb.github.io/2023/10/09/PWC238/ diff --git a/challenge-238/wlmb/perl/ch-1.pl b/challenge-238/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..6df93ddfcc --- /dev/null +++ b/challenge-238/wlmb/perl/ch-1.pl @@ -0,0 +1,13 @@ +#!/usr/bin/env perl +# Perl weekly challenge 238 +# Task 1: Running Sum +# +# See https://wlmb.github.io/2023/10/09/PWC238/#task-1-running-sum +use v5.36; +use List::Util qw(reductions); +die <<~"FIN" unless @ARGV; + Usage: $0 N1 [N2...] + to return the running sum N1, N1+N2, N1+N2+N3... + of the numbers N1, N2... + FIN +say "@ARGV -> ", join " ", reductions {$a+$b} @ARGV diff --git a/challenge-238/wlmb/perl/ch-2.pl b/challenge-238/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..1b18409421 --- /dev/null +++ b/challenge-238/wlmb/perl/ch-2.pl @@ -0,0 +1,22 @@ +#!/usr/bin/env perl +# Perl weekly challenge 238 +# Task 2: Persistence Sort +# +# See https://wlmb.github.io/2023/10/09/PWC238/#task-2-persistence-sort +use v5.36; +use List::Util qw(product all); +use POSIX qw(floor); +sub steps($x){ + my $result=0; + ++$result , $x=product split "", $x + while($x>=10); + $result +} +die <<~"FIN" unless @ARGV; + Usage: $0 N1 [N2...] + to sort the list N1 N2 according to the number of steps required + to bring the number to one digit and then according to value + FIN +die "Only natural numbers allowed" unless all {0<=floor($_)&&floor($_)==$_} @ARGV; +say "@ARGV -> ", join " ", + sort {steps($a)<=>steps($b)||$a<=>$b} @ARGV -- cgit From adb0140f027d3fb3fab8432b5435690612bec976 Mon Sep 17 00:00:00 2001 From: pme Date: Mon, 9 Oct 2023 20:47:11 +0200 Subject: challenge-238 --- challenge-238/peter-meszaros/perl/ch-1.pl | 55 +++++++++++++++++++++++ challenge-238/peter-meszaros/perl/ch-2.pl | 73 +++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100755 challenge-238/peter-meszaros/perl/ch-1.pl create mode 100755 challenge-238/peter-meszaros/perl/ch-2.pl diff --git a/challenge-238/peter-meszaros/perl/ch-1.pl b/challenge-238/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..b80ae4211a --- /dev/null +++ b/challenge-238/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,55 @@ +#!/usr/bin/env perl +# +# 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 strict; +use warnings; +use Test::More; +use Data::Dumper; + +my $cases = [ + [1, 2, 3, 4, 5], + [1, 1, 1, 1, 1], + [0, -1, 1, 2], +]; + +sub running_sum +{ + my $l = shift; + + my @ret; + my $sum = 0; + for my $v (@$l) { + $sum += $v; + push @ret, $sum; + } + + return \@ret; +} + +is_deeply(running_sum($cases->[0]), [1, 3, 6, 10, 15], '[1, 2, 3, 4, 5]'); +is_deeply(running_sum($cases->[1]), [1, 2, 3, 4, 5], '[1, 1, 1, 1, 1]'); +is_deeply(running_sum($cases->[2]), [0, -1, 0, 2], '[0, -1, 1, 2]'); +done_testing(); + +exit 0; + + diff --git a/challenge-238/peter-meszaros/perl/ch-2.pl b/challenge-238/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..0236251275 --- /dev/null +++ b/challenge-238/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,73 @@ +#!/usr/bin/env perl +# +# 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 => 6 (1 step) +# 22 => 2 x 2 => 4 (1 step) +# + +use strict; +use warnings; +use Test::More; +use Data::Dumper; + +my $cases = [ + [15, 99, 1, 34], + [50, 25, 33, 22], +]; + +sub count_steps +{ + my $n = shift; + + my $steps = 0; + while(length($n)> 1) { + ++$steps; + my @n = split('', $n); + $n = 1; + $n *= $_ for @n; + } + return $steps; +} + +sub persistence_sort +{ + my $l = shift; + + my @steps = map { count_steps($_) } @$l; + + my @ord = sort { if ($steps[$a] == $steps[$b]) { + return $l->[$a] <=> $l->[$b]; + } + return $steps[$a] <=> $steps[$b] + } (0 .. $#$l); + + return [$l->@[@ord]]; +} + +is_deeply(persistence_sort($cases->[0]), [1, 15, 34, 99], '[15, 99, 1, 34]'); +is_deeply(persistence_sort($cases->[1]), [22, 33, 50, 25], '[50, 25, 33, 22]'); +done_testing(); + +exit 0; -- cgit From 532210e0f18a16f0478f0e09692c1de0f3616f8f Mon Sep 17 00:00:00 2001 From: pme Date: Mon, 9 Oct 2023 20:48:39 +0200 Subject: Revert "challenge-238" This reverts commit adb0140f027d3fb3fab8432b5435690612bec976. --- challenge-238/peter-meszaros/perl/ch-1.pl | 55 ----------------------- challenge-238/peter-meszaros/perl/ch-2.pl | 73 ------------------------------- 2 files changed, 128 deletions(-) delete mode 100755 challenge-238/peter-meszaros/perl/ch-1.pl delete mode 100755 challenge-238/peter-meszaros/perl/ch-2.pl diff --git a/challenge-238/peter-meszaros/perl/ch-1.pl b/challenge-238/peter-meszaros/perl/ch-1.pl deleted file mode 100755 index b80ae4211a..0000000000 --- a/challenge-238/peter-meszaros/perl/ch-1.pl +++ /dev/null @@ -1,55 +0,0 @@ -#!/usr/bin/env perl -# -# 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 strict; -use warnings; -use Test::More; -use Data::Dumper; - -my $cases = [ - [1, 2, 3, 4, 5], - [1, 1, 1, 1, 1], - [0, -1, 1, 2], -]; - -sub running_sum -{ - my $l = shift; - - my @ret; - my $sum = 0; - for my $v (@$l) { - $sum += $v; - push @ret, $sum; - } - - return \@ret; -} - -is_deeply(running_sum($cases->[0]), [1, 3, 6, 10, 15], '[1, 2, 3, 4, 5]'); -is_deeply(running_sum($cases->[1]), [1, 2, 3, 4, 5], '[1, 1, 1, 1, 1]'); -is_deeply(running_sum($cases->[2]), [0, -1, 0, 2], '[0, -1, 1, 2]'); -done_testing(); - -exit 0; - - diff --git a/challenge-238/peter-meszaros/perl/ch-2.pl b/challenge-238/peter-meszaros/perl/ch-2.pl deleted file mode 100755 index 0236251275..0000000000 --- a/challenge-238/peter-meszaros/perl/ch-2.pl +++ /dev/null @@ -1,73 +0,0 @@ -#!/usr/bin/env perl -# -# 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 => 6 (1 step) -# 22 => 2 x 2 => 4 (1 step) -# - -use strict; -use warnings; -use Test::More; -use Data::Dumper; - -my $cases = [ - [15, 99, 1, 34], - [50, 25, 33, 22], -]; - -sub count_steps -{ - my $n = shift; - - my $steps = 0; - while(length($n)> 1) { - ++$steps; - my @n = split('', $n); - $n = 1; - $n *= $_ for @n; - } - return $steps; -} - -sub persistence_sort -{ - my $l = shift; - - my @steps = map { count_steps($_) } @$l; - - my @ord = sort { if ($steps[$a] == $steps[$b]) { - return $l->[$a] <=> $l->[$b]; - } - return $steps[$a] <=> $steps[$b] - } (0 .. $#$l); - - return [$l->@[@ord]]; -} - -is_deeply(persistence_sort($cases->[0]), [1, 15, 34, 99], '[15, 99, 1, 34]'); -is_deeply(persistence_sort($cases->[1]), [22, 33, 50, 25], '[50, 25, 33, 22]'); -done_testing(); - -exit 0; -- cgit From 048bd312eb7465ca744d4bd7e3908946357c43a6 Mon Sep 17 00:00:00 2001 From: pme Date: Mon, 9 Oct 2023 20:50:54 +0200 Subject: challenge-238 --- challenge-238/peter-meszaros/perl/ch-1.pl | 55 +++++++++++++++++++++++ challenge-238/peter-meszaros/perl/ch-2.pl | 73 +++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100755 challenge-238/peter-meszaros/perl/ch-1.pl create mode 100755 challenge-238/peter-meszaros/perl/ch-2.pl diff --git a/challenge-238/peter-meszaros/perl/ch-1.pl b/challenge-238/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..b80ae4211a --- /dev/null +++ b/challenge-238/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,55 @@ +#!/usr/bin/env perl +# +# 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 strict; +use warnings; +use Test::More; +use Data::Dumper; + +my $cases = [ + [1, 2, 3, 4, 5], + [1, 1, 1, 1, 1], + [0, -1, 1, 2], +]; + +sub running_sum +{ + my $l = shift; + + my @ret; + my $sum = 0; + for my $v (@$l) { + $sum += $v; + push @ret, $sum; + } + + return \@ret; +} + +is_deeply(running_sum($cases->[0]), [1, 3, 6, 10, 15], '[1, 2, 3, 4, 5]'); +is_deeply(running_sum($cases->[1]), [1, 2, 3, 4, 5], '[1, 1, 1, 1, 1]'); +is_deeply(running_sum($cases->[2]), [0, -1, 0, 2], '[0, -1, 1, 2]'); +done_testing(); + +exit 0; + + diff --git a/challenge-238/peter-meszaros/perl/ch-2.pl b/challenge-238/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..0236251275 --- /dev/null +++ b/challenge-238/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,73 @@ +#!/usr/bin/env perl +# +# 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 => 6 (1 step) +# 22 => 2 x 2 => 4 (1 step) +# + +use strict; +use warnings; +use Test::More; +use Data::Dumper; + +my $cases = [ + [15, 99, 1, 34], + [50, 25, 33, 22], +]; + +sub count_steps +{ + my $n = shift; + + my $steps = 0; + while(length($n)> 1) { + ++$steps; + my @n = split('', $n); + $n = 1; + $n *= $_ for @n; + } + return $steps; +} + +sub persistence_sort +{ + my $l = shift; + + my @steps = map { count_steps($_) } @$l; + + my @ord = sort { if ($steps[$a] == $steps[$b]) { + return $l->[$a] <=> $l->[$b]; + } + return $steps[$a] <=> $steps[$b] + } (0 .. $#$l); + + return [$l->@[@ord]]; +} + +is_deeply(persistence_sort($cases->[0]), [1, 15, 34, 99], '[15, 99, 1, 34]'); +is_deeply(persistence_sort($cases->[1]), [22, 33, 50, 25], '[50, 25, 33, 22]'); +done_testing(); + +exit 0; -- cgit From 5ff3e30687373515a1b6707f5762974453a2bf07 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Mon, 9 Oct 2023 20:36:01 +0100 Subject: - Added solutions by Eric Cheung. - Added solutions by Robert DiCicco. - Added solutions by Laurent Rosenfeld. - Added solutions by Ulrich Rieke. - Added solutions by Mark Anderson. - Added solutions by E. Choroba. - Added solutions by Niels van Dijke. --- challenge-238/eric-cheung/python/ch-1.py | 12 + challenge-238/eric-cheung/python/ch-2.py | 23 + challenge-238/laurent-rosenfeld/blog.txt | 1 + challenge-238/laurent-rosenfeld/perl/ch-1.pl | 17 + challenge-238/laurent-rosenfeld/raku/ch-1.raku | 9 + challenge-238/perlboy1967/perl/ch-1.pl | 37 + challenge-238/perlboy1967/perl/ch-2.pl | 54 + challenge-238/perlboy1967/perl/ch1.pl | 37 - challenge-238/perlboy1967/perl/ch2.pl | 54 - challenge-238/robert-dicicco/perl/ch-1.pl | 37 + challenge-238/robert-dicicco/raku/ch-1.raku | 40 + challenge-238/ulrich-rieke/cpp/ch-1.cpp | 38 + challenge-238/ulrich-rieke/cpp/ch-2.cpp | 73 + challenge-238/ulrich-rieke/haskell/ch-1.hs | 5 + challenge-238/ulrich-rieke/haskell/ch-2.hs | 33 + challenge-238/ulrich-rieke/perl/ch-1.pl | 17 + challenge-238/ulrich-rieke/perl/ch-2.pl | 42 + challenge-238/ulrich-rieke/raku/ch-1.raku | 13 + challenge-238/ulrich-rieke/raku/ch-2.raku | 55 + challenge-238/ulrich-rieke/rust/ch-1.rs | 18 + challenge-238/ulrich-rieke/rust/ch-2.rs | 52 + stats/pwc-challenge-237.json | 636 ++++++++ stats/pwc-current.json | 580 +------- stats/pwc-language-breakdown-summary.json | 54 +- stats/pwc-language-breakdown.json | 1569 ++++++++++---------- stats/pwc-leaders.json | 418 +++--- stats/pwc-summary-1-30.json | 34 +- stats/pwc-summary-121-150.json | 106 +- stats/pwc-summary-151-180.json | 122 +- stats/pwc-summary-181-210.json | 100 +- stats/pwc-summary-211-240.json | 122 +- stats/pwc-summary-241-270.json | 106 +- stats/pwc-summary-271-300.json | 54 +- stats/pwc-summary-31-60.json | 58 +- stats/pwc-summary-61-90.json | 116 +- stats/pwc-summary-91-120.json | 34 +- stats/pwc-summary.json | 1834 ++++++++++++------------ 37 files changed, 3636 insertions(+), 2974 deletions(-) create mode 100755 challenge-238/eric-cheung/python/ch-1.py create mode 100755 challenge-238/eric-cheung/python/ch-2.py create mode 100644 challenge-238/laurent-rosenfeld/blog.txt create mode 100644 challenge-238/laurent-rosenfeld/perl/ch-1.pl create mode 100644 challenge-238/laurent-rosenfeld/raku/ch-1.raku create mode 100755 challenge-238/perlboy1967/perl/ch-1.pl create mode 100755 challenge-238/perlboy1967/perl/ch-2.pl delete mode 100755 challenge-238/perlboy1967/perl/ch1.pl delete mode 100755 challenge-238/perlboy1967/perl/ch2.pl create mode 100644 challenge-238/robert-dicicco/perl/ch-1.pl create mode 100644 challenge-238/robert-dicicco/raku/ch-1.raku create mode 100755 challenge-238/ulrich-rieke/cpp/ch-1.cpp create mode 100755 challenge-238/ulrich-rieke/cpp/ch-2.cpp create mode 100755 challenge-238/ulrich-rieke/haskell/ch-1.hs create mode 100755 challenge-238/ulrich-rieke/haskell/ch-2.hs create mode 100755 challenge-238/ulrich-rieke/perl/ch-1.pl create mode 100755 challenge-238/ulrich-rieke/perl/ch-2.pl create mode 100755 challenge-238/ulrich-rieke/raku/ch-1.raku create mode 100755 challenge-238/ulrich-rieke/raku/ch-2.raku create mode 100755 challenge-238/ulrich-rieke/rust/ch-1.rs create mode 100755 challenge-238/ulrich-rieke/rust/ch-2.rs create mode 100644 stats/pwc-challenge-237.json diff --git a/challenge-238/eric-cheung/python/ch-1.py b/challenge-238/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..940af19099 --- /dev/null +++ b/challenge-238/eric-cheung/python/ch-1.py @@ -0,0 +1,12 @@ + +## arrInput = [1, 2, 3, 4, 5] ## Example 1 +## arrInput = [1, 1, 1, 1, 1] ## Example 2 +arrInput = [0, -1, 1, 2] ## Example 3 + +arrOutput = [] +for nLoop in arrInput: + ## print (nLoop) + arrOutput.append(nLoop if len(arrOutput) == 0 else nLoop + arrOutput[-1]) + ## print (arrOutput) + +print (arrOutput) diff --git a/challenge-238/eric-cheung/python/ch-2.py b/challenge-238/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..e215581ec3 --- /dev/null +++ b/challenge-238/eric-cheung/python/ch-2.py @@ -0,0 +1,23 @@ + +from numpy import prod + +def GetStep (nStep, nInput): + if nInput < 10: + return nStep + return GetStep (nStep + 1, prod([int(charLoop) for charLoop in str(nInput)])) + +## print (GetStep(0, 1)) + +## arrInput = [15, 99, 1, 34] ## Example 1 +arrInput = [50, 25, 33, 22] ## Example 2 + +arrOutput = arrInput[:] + +for nRow in range(0, len(arrOutput) - 1): + for nCol in range(nRow + 1, len(arrOutput)): + if GetStep (0, arrOutput[nRow]) > GetStep (0, arrOutput[nCol]) or GetStep (0, arrOutput[nRow]) == GetStep (0, arrOutput[nCol]) and arrOutput[nRow] > arrOutput[nCol]: + vTemp = arrOutput[nRow] + arrOutput[nRow] = arrOutput[nCol] + arrOutput[nCol] = vTemp + +print (arrOutput) diff --git a/challenge-238/laurent-rosenfeld/blog.txt b/challenge-238/laurent-rosenfeld/blog.txt new file mode 100644 index 0000000000..8cfb474024 --- /dev/null +++ b/challenge-238/laurent-rosenfeld/blog.txt @@ -0,0 +1 @@ +https://blogs.perl.org/users/laurent_r/2023/10/perl-weekly-challenge-238-running-sum.html diff --git a/challenge-238/laurent-rosenfeld/perl/ch-1.pl b/challenge-238/laurent-rosenfeld/perl/ch-1.pl new file mode 100644 index 0000000000..6164c90358 --- /dev/null +++ b/challenge-238/laurent-rosenfeld/perl/ch-1.pl @@ -0,0 +1,17 @@ +use strict; +use warnings; +use feature 'say'; + +sub running_sum { + my @sum = shift; + for my $item (@_) { + push @sum, $item + $sum[-1]; + } + return @sum; +} + +my @tests = ([<1 2 3 4 5>], [<1 1 1 1 1>], [<0 -1 1 2>]); +for my $test (@tests) { + printf "%-15s => ", "@$test"; + say join ", ", running_sum @$test; +} diff --git a/challenge-238/laurent-rosenfeld/raku/ch-1.raku b/challenge-238/laurent-rosenfeld/raku/ch-1.raku new file mode 100644 index 0000000000..aa519c8e8f --- /dev/null +++ b/challenge-238/laurent-rosenfeld/raku/ch-1.raku @@ -0,0 +1,9 @@ +sub running-sum (@in) { + return [\+] @in; +} + +my @tests = <1 2 3 4 5>, <1 1 1 1 1>, <0 -1 1 2>; +for @tests -> @test { + printf "%-15s => ", "@test[]"; + say join ", ", running-sum @test; +} diff --git a/challenge-238/perlboy1967/perl/ch-1.pl b/challenge-238/perlboy1967/perl/ch-1.pl new file mode 100755 index 0000000000..159ba18e4b --- /dev/null +++ b/challenge-238/perlboy1967/perl/ch-1.pl @@ -0,0 +1,37 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 238 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-238 + +Author: Niels 'PerlBoy' van Dijke + +Task 1: Running Sum +Submitted by: 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]. + +=cut + +use v5.32; +use common::sense; +use feature 'signatures'; + +use Test::More; +use Test::Deep qw(cmp_deeply); + + +sub runningSum (@numbers) { + my $s = 0; + return map { $s += $_; } @numbers; +} + +cmp_deeply([runningSum(1,2,3,4,5)],[1,3,6,10,15]); +cmp_deeply([runningSum(1,1,1,1,1)],[1,2,3,4,5]); +cmp_deeply([runningSum(0,-1,1,2)],[0,-1,0,2]); + +done_testing; diff --git a/challenge-238/perlboy1967/perl/ch-2.pl b/challenge-238/perlboy1967/perl/ch-2.pl new file mode 100755 index 0000000000..c1e3b5be91 --- /dev/null +++ b/challenge-238/perlboy1967/perl/ch-2.pl @@ -0,0 +1,54 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 238 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-238 + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Persistence Sort +Submitted by: 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. + +=cut + +use v5.32; +use common::sense; +use feature 'signatures'; + +use Test::More; +use Test::Deep qw(cmp_deeply); + +use List::MoreUtils qw(slide); +use Memoize; + +memoize 'cnt_steps'; + +sub cnt_steps($i) { + my $n = 0; + + while (1) { + my @d = split(//,$i); + last if (@d == 1); + $i = slide { $a * $b } @d; + $n++; + } + + return $n; +} + +sub persistenceSort (@numbers) { + sort { cnt_steps($a) <=> cnt_steps($b) || $a <=> $b} @numbers; +} + +cmp_deeply([persistenceSort(15,99,1,34)],[1,15,34,99]); +cmp_deeply([persistenceSort(50,25,33,22)],[22,33,50,25]); + +done_testing; diff --git a/challenge-238/perlboy1967/perl/ch1.pl b/challenge-238/perlboy1967/perl/ch1.pl deleted file mode 100755 index 159ba18e4b..0000000000 --- a/challenge-238/perlboy1967/perl/ch1.pl +++ /dev/null @@ -1,37 +0,0 @@ -#!/bin/perl - -=pod - -The Weekly Challenge - 238 -- https://theweeklychallenge.org/blog/perl-weekly-challenge-238 - -Author: Niels 'PerlBoy' van Dijke - -Task 1: Running Sum -Submitted by: 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]. - -=cut - -use v5.32; -use common::sense; -use feature 'signatures'; - -use Test::More; -use Test::Deep qw(cmp_deeply); - - -sub runningSum (@numbers) { - my $s = 0; - return map { $s += $_; } @numbers; -} - -cmp_deeply([runningSum(1,2,3,4,5)],[1,3,6,10,15]); -cmp_deeply([runningSum(1,1,1,1,1)],[1,2,3,4,5]); -cmp_deeply([runningSum(0,-1,1,2)],[0,-1,0,2]); - -done_testing; diff --git a/challenge-238/perlboy1967/perl/ch2.pl b/challenge-238/perlboy1967/perl/ch2.pl deleted file mode 100755 index c1e3b5be91..0000000000 --- a/challenge-238/perlboy1967/perl/ch2.pl +++ /dev/null @@ -1,54 +0,0 @@ -#!/bin/perl - -=pod - -The Weekly Challenge - 238 -- https://theweeklychallenge.org/blog/perl-weekly-challenge-238 - -Author: Niels 'PerlBoy' van Dijke - -Task 2: Persistence Sort -Submitted by: 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. - -=cut - -use v5.32; -use common::sense; -use feature 'signatures'; - -use Test::More; -use Test::Deep qw(cmp_deeply); - -use List::MoreUtils qw(slide); -use Memoize; - -memoize 'cnt_steps'; - -sub cnt_steps($i) { - my $n = 0; - - while (1) { - my @d = split(//,$i); - last if (@d == 1); - $i = slide { $a * $b } @d; - $n++; - } - - return $n; -} - -sub persistenceSort (@numbers) { - sort { cnt_steps($a) <=> cnt_steps($b) || $a <=> $b} @numbers; -} - -cmp_deeply([persistenceSort(15,99,1,34)],[1,15,34,99]); -cmp_deeply([persistenceSort(50,25,33,22)],[22,33,50,25]); - -done_testing; diff --git a/challenge-238/robert-dicicco/perl/ch-1.pl b/challenge-238/robert-dicicco/perl/ch-1.pl new file mode 100644 index 0000000000..a5729284dc --- /dev/null +++ b/challenge-238/robert-dicicco/perl/ch-1.pl @@ -0,0 +1,37 @@ +#!/usr/bin/env perl +=begin comment +AUTHOR: Robert DiCicco +DATE : 2023-10-09 +Challenge 238 Task 01 Running Sum ( Perl ) +=cut + +use v5.38; + +my @myints = ([1, 2, 3, 4, 5],[1, 1, 1, 1, 1],[0, -1, 1, 2]); + +for my $mints (@myints) { + my $sum = 0; + my @out = (); + say "Input: \@int = [@$mints]"; + for my $i (@$mints) { + push(@out, $i + $sum); + $sum += $i; + } + say "Output: [@out]\n"; +} + +=begin comment +SAMPLE OUTPUT +perl .\RunningSum.pl + +Input: @int = [1 2 3 4 5] +Output: [1 3 6 10 15] + +Input: @int = [1 1 1 1 1] +Output: [1 2 3 4 5] + +Input: @int = [0 -1 1 2] +Output: [0 -1 0 2] +=cut + + diff --git a/challenge-238/robert-dicicco/raku/ch-1.raku b/challenge-238/robert-dicicco/raku/ch-1.raku new file mode 100644 index 0000000000..4e9944e28d --- /dev/null +++ b/challenge-238/robert-dicicco/raku/ch-1.raku @@ -0,0 +1,40 @@ +#!/usr/bin/env raku + +=begin comment +--------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-10-09 +Challenge 238 Task 01 Running Sum ( Raku ) +--------------------------------- +=end comment + +my @myints = ([1, 2, 3, 4, 5],[1, 1, 1, 1, 1],[0, -1, 1, 2]); + +for (@myints) -> @mints { + my $sum = 0; + my @out = (); + say "Input: \@int = ", @mints; + for (@mints) -> $i { + @out.push($i + $sum); + $sum += $i; + } + say "Output: ",@out,"\n"; +} + +=begin comment +--------------------------------- +SAMPLE OUTPUT +raku .\RunningSum.rk + +Input: @int = [1 2 3 4 5] +Output: [1 3 6 10 15] + +Input: @int = [1 1 1 1 1] +Output: [1 2 3 4 5] + +Input: @int = [0 -1 1 2] +Output: [0 -1 0 2] +--------------------------------- +=end comment + + diff --git a/challenge-238/ulrich-rieke/cpp/ch-1.cpp b/challenge-238/ulrich-rieke/cpp/ch-1.cpp new file mode 100755 index 0000000000..255523fb1f --- /dev/null +++ b/challenge-238/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,38 @@ +#include +#include +#include + +std::vector split( const std::string & startline , + const std::string & sep ) { + std::vector separated ; + std::string::size_type start { 0 } ; + std::string::size_type pos ; + do { + pos = startline.find_first_of( sep , start ) ; + separated.push_back( startline.substr(start , pos - start )) ; + start = pos + 1 ; + } while ( pos != std::string::npos ) ; + return separated ; +} + +int main( ) { + std::cout << "Enter some integers, separated by blanks!\n" ; + std::string line ; + std::getline( std::cin , line ) ; + std::vector numberstrings( split( line , " " ) ) ; + std::vector numbers ; + for ( auto s : numberstrings ) + numbers.push_back( std::stoi( s ) ) ; + std::vector result ; + int current_sum = 0 ; + int size = numbers.size( ) ; + for ( int i = 0 ; i < size ; i++ ) { + current_sum += numbers[ i ] ; + result.push_back( current_sum ) ; + } + std::cout << "( " ; + for ( int i : result ) + std::cout << i << " " ; + std::cout << ")\n" ; + return 0 ; +} diff --git a/challenge-238/ulrich-rieke/cpp/ch-2.cpp b/challenge-238/ulrich-rieke/cpp/ch-2.cpp new file mode 100755 index 0000000000..695c38c7c1 --- /dev/null +++ b/challenge-238/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,73 @@ +#include +#include +#include +#include +#include + +std::vector split( const std::string & startline , + const std::string & sep ) { + std::vector separated ; + std::string::size_type start { 0 } ; + std::string::size_type pos ; + do { + pos = startline.find_first_of( sep , start ) ; + separated.push_back( startline.substr(start , pos - start )) ; + start = pos + 1 ; + } while ( pos != std::string::npos ) ; + return separated ; +} + +std::vector decompose( int number ) { + std::vector digits ; + while ( number != 0 ) { + int remainder = number % 10 ; + digits.push_back( remainder ) ; + number /= 10 ; + } + return digits ; +} + +int findSteps( int number ) { + if ( number < 10 ) + return 0 ; + else { + int steps = 1 ; + std::vector digits ( decompose( number ) ) ; + int product = std::accumulate( digits.begin( ) , digits.end( ) , + 1 , std::multiplies( ) ) ; + while ( product > 9 ) { + steps++ ; + digits = decompose( product ) ; + product = std::accumulate( digits.begin( ) , digits.end( ) , + 1 , std::multiplies( ) ) ; + } + return steps ; + } +} + +bool mySorter( int a , int b ) { + int steps1 = findSteps( a ) ; + int steps2 = findSteps( b ) ; + if ( steps1 != steps2 ) + return steps1 < steps2 ; + else + return a < b ; +} + +int main( ) { + std::cout << "Enter some positive integers, separated by blanks!\n" ; + std::string line ; + std::getline( std::cin , line ) ; + std::vector numberstrings( split( line , " " ) ) ; + std::vector numbers ; + for ( auto s : numberstrings ) + numbers.push_back( std::stoi( s ) ) ; + std::sort( numbers.begin( ) , numbers.end( ) , []( int a , int b ) { + return mySorter( a , b ) ; } ) ; + std::cout << "(" ; + for ( int i : numbers ) { + std::cout << i << " " ; + } + std::cout << ")\n" ; + return 0 ; +} diff --git a/challenge-238/ulrich-rieke/haskell/ch-1.hs b/challenge-238/ulrich-rieke/haskell/ch-1.hs new file mode 100755 index 0000000000..35d2766786 --- /dev/null +++ b/challenge-238/ulrich-rieke/haskell/ch-1.hs @@ -0,0 +1,5 @@ +module Challenge238 + where + +solution :: [Int] -> [Int] +solution list = map (\n -> sum $ take n list ) [1..length list] diff --git a/challenge-238/ulrich-rieke/haskell/ch-2.hs b/challenge-238/ulrich-rieke/haskell/ch-2.hs new file mode 100755 index 0000000000..7c9b38a720 --- /dev/null +++ b/challenge-238/ulrich-rieke/haskell/ch-2.hs @@ -0,0 +1,33 @@ +module Challenge238_2 + where +import Data.Char ( digitToInt ) +import Data.List ( sortBy ) + +decompose :: Int -> [Int] +decompose = map digitToInt . show + +findSteps :: Int -> Int +findSteps n + |n < 10 = 0 + |otherwise = fst $ until ( ( < 10 ) . snd ) action ( 0 , n ) + where + action :: (Int , Int ) -> (Int , Int ) + action ( count , d ) = ( count + 1 , product $ decompose d ) + +mySorter :: Int -> Int -> Ordering +mySorter d1 d2 + |st1 /= st2 = compare st1 st2 + |otherwise = compare d1 d2 + where + st1 = findSteps d1 + st2 = findSteps d2 + +solution :: [Int] -> [Int] +solution = sortBy mySorter + +main :: IO ( ) +main = do + putStrLn "Enter some positive integers , separated by blanks!" + numberstrings <- getLine + let numbers = map read $ words numberstrings + print $ solution numbers diff --git a/challenge-238/ulrich-rieke/perl/ch-1.pl b/challenge-238/ulrich-rieke/perl/ch-1.pl new file mode 100755 index 0000000000..e53dec077d --- /dev/null +++ b/challenge-238/ulrich-rieke/perl/ch-1.pl @@ -0,0 +1,17 @@ +#!/usr/bin/perl ; +use strict ; +use warnings ; +use feature 'say' ; + +say "Enter some integers, separated by blanks!" ; +my $line = ; +chomp $line ; +my @numbers = split( /\s/ , $line ) ; +my $len = scalar( @numbers ) ; +my $current_sum = 0 ; +my @result ; +for my $i (0..$len - 1 ) { + $current_sum += $numbers[ $i ] ; + push @result , $current_sum ; +} +say "(" . join( ',' , @result ) . ")" ; diff --git a/challenge-238/ulrich-rieke/perl/ch-2.pl b/challenge-238/ulrich-rieke/perl/ch-2.pl new file mode 100755 index 0000000000..2e11c42b1d --- /dev/null +++ b/challenge-238/ulrich-rieke/perl/ch-2.pl @@ -0,0 +1,42 @@ +#!/usr/bin/perl ; +use strict ; +use warnings ; +use feature 'say' ; +use List::Util qw ( product ) ; + +sub decompose { + my $number = shift ; + my @digits ; + while ( $number != 0 ) { + my $remainder = $number % 10 ; + push @digits , $remainder ; + $number = int( $number / 10 ) ; + } + return @digits ; +} + +sub findSteps { + my $number = shift ; + if ( $number < 10 ) { + return 0 ; + } + else { + my @digits = decompose( $number ) ; + my $steps = 1 ; + my $prod = product( @digits ) ; + while ( $prod > 9 ) { + $steps++ ; + @digits = decompose( $prod ) ; + $prod = product( @digits ) ; + } + return $steps ; + } +} + +say "Enter some positive integers, separated by blanks!" ; +my $line = ; +chomp $line ; +my @numbers = split( /\s/ , $line ) ; +my @sorted = sort { findSteps( $a ) <=> findSteps( $b ) || $a <=> $b } + @numbers ; +say "(" . join( ',' , @sorted ) . ")" ; diff --git a/challenge-238/ulrich-rieke/raku/ch-1.raku b/challenge-238/ulrich-rieke/raku/ch-1.raku new file mode 100755 index 0000000000..f120033055 --- /dev/null +++ b/challenge-238/ulrich-rieke/raku/ch-1.raku @@ -0,0 +1,13 @@ +use v6 ; + +say "Enter some integers, separated by blanks!" ; +my $line = $*IN.get ; +my @numbers = $line.words.map( {.Int} ) ; +my $len = @numbers.elems ; +my @result ; +my $current_sum = 0 ; +for (0..$len - 1 ) -> $pos { + $current_sum += @numbers[ $pos ] ; + @result.push( $current_sum ) ; +} +say "(" ~ @result.join( ',' ) ~ ")" ; diff --git a/challenge-238/ulrich-rieke/raku/ch-2.raku b/challenge-238/ulrich-rieke/raku/ch-2.raku new file mode 100755 index 0000000000..b033d029d0 --- /dev/null +++ b/challenge-238/ulrich-rieke/raku/ch-2.raku @@ -0,0 +1,55 @@ +use v6 ; + +sub decompose( $number is copy ) { + my @digits ; + while ( $number != 0 ) { + my $remainder = $number % 10 ; + @digits.push( $remainder ) ; + $number div= 10 ; + } + return @digits ; +} + +sub find_steps( $number is copy ) { + if ( $number < 10 ) { + return 0 ; + } + else { + my $steps = 1 ; + my @digits = decompose( $number ) ; + my $product = [*] @digits ; + while ( $product > 9 ) { + $steps++ ; + @digits = decompose( $product ) ; + $product = [*] @digits ; + } + return $steps ; + } +} + +sub my_sorter( $firstNum , $secondNum ) { + my $steps1 = find_steps( $firstNum ) ; + my $steps2 = find_steps( $secondNum ) ; + if ( $steps1 != $steps2 ) { + if ( $steps1 < $steps2 ) { + return Order::Less ; + } + else { + return Order::More ; + } + } + else { + if ( $firstNum < $secondNum ) { + return Order::Less ; + } + else { + return Order::More ; + } + } +} + +say "Enter some positive integers, separated by blanks!" ; +my $line = $*IN.get ; +my @numbers = $line.words.map( {.Int} ) ; +my $sorted = @numbers.sort: &my_sorter ; +say "(" ~ $sorted.join( ',' ) ~ ")" ; diff --git a/challenge-238/ulrich-rieke/rust/ch-1.rs b/challenge-238/ulrich-rieke/rust/ch-1.rs new file mode 100755 index 0000000000..17daff00de --- /dev/null +++ b/challenge-238/ulrich-rieke/rust/ch-1.rs @@ -0,0 +1,18 @@ +use std::io ; + +fn main() { + println!("Enter some integers, separated by blanks!"); + let mut inline : String = String::new( ) ; + io::stdin( ).read_line( &mut inline ).unwrap( ) ; + let entered_line : &str = &*inline ; + let numbers : Vec = entered_line.split_whitespace( ).map( | s | + s.trim( ).parse::( ).unwrap( ) ).collect( ) ; + let mut result : Vec = Vec::new( ) ; + let mut current_sum : i32 = 0 ; + let len = numbers.len( ) ; + for i in 0..len { + current_sum += numbers[ i ] ; + result.push( current_sum ) ; + } + println!("{:?}" , result ) ; +} diff --git a/challenge-238/ulrich-rieke/rust/ch-2.rs b/challenge-238/ulrich-rieke/rust/ch-2.rs new file mode 100755 index 0000000000..491503d5fc --- /dev/null +++ b/challenge-238/ulrich-rieke/rust/ch-2.rs @@ -0,0 +1,52 @@ +use std::io ; +use std::cmp::Ordering ; + +fn decompose( number : u32 ) -> Vec { + let mut digits : Vec = Vec::new( ) ; + let mut num : u32 = number ; + while num != 0 { + let remainder = num % 10 ; + digits.push( remainder ) ; + num /= 10 ; + } + digits +} + +fn find_steps( number : u32 ) -> usize { + if number < 10 { + 0 + } + else { + let mut digits : Vec = decompose( number ) ; + let mut steps : usize = 1 ; + let mut prod : u32 = digits.iter( ).product::( ) ; + while prod > 9 { + steps += 1 ; + digits = decompose( prod ) ; + prod = digits.iter( ).product::() ; + } + steps + } +} + +fn main() { + println!("Enter some positive integers , separated by blanks!"); + let mut inline : String = String::new( ) ; + io::stdin( ).read_line( &mut inline ).unwrap( ) ; + let entered_line : &str = &*inline ; + let mut numbers : Vec = entered_line.split_whitespace( ).map( | s | + s.trim( ).parse::().unwrap( )).collect( ) ; + let nums = numbers.as_mut_slice( ) ; + nums.sort_by( |a , b| { + let s1 : usize = find_steps( *a ) ; + let s2 : usize = find_steps( *b ) ; + let my_ord : Ordering = s1.cmp(&s2) ; + if my_ord != Ordering::Equal { + my_ord + } + else { + a.cmp( b ) + } + }) ; + println!("{:?}" , nums ) ; +} diff --git a/stats/pwc-challenge-237.json b/stats/pwc-challenge-237.json new file mode 100644 index 0000000000..7056539f52 --- /dev/null +++ b/stats/pwc-challenge-237.json @@ -0,0 +1,636 @@ +{ + "title" : { + "text" : "The Weekly Challenge - 237" + }, + "xAxis" : { + "type" : "category" + }, + "drilldown" : { + "series" : [ + { + "name" : "Ali Moradi", + "id" : "Ali Moradi", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ] + }, + { + "data" : [ + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Arne Sommer", + "name" : "Arne Sommer" + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ] + ], + "id" : "Athanasius", + "name" : "Athanasius" + }, + { + "id" : "Avery Adams", + "data" : [ + [ + "Perl", + 1 + ], + [ + "Blog", + 2 + ] + ], + "name" : "Avery Adams" + }, + { + "name" : "BarrOff", + "id" : "BarrOff", + "data" : [ + [ + "Raku", + 2 + ] + ] + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Bob Lied", + "name" : "Bob Lied" + }, + { + "name" : "Bruce Gray", + "id" : "Bruce Gray", + "data" : [ + [ + "Raku", + 2 + ] + ] + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Dave Jacoby", + "name" : "Dave Jacoby" + }, + { + "name" : "David Ferrone", + "data" : [ + [ + "Perl", + 2 + ] + ], + "id" : "David Ferrone" + }, + { + "name" : "E. Choroba", + "id" : "E. Choroba", + "data" : [ + [ + "Perl", + 2 + ] + ] + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 2 + ] + ], + "id" : "Flavio Poletti", + "name" : "Flavio Poletti" + }, + { + "name" : "Jaldhar H. Vyas", + "id" : "Jaldhar H. Vyas", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ] + }, + { + "name" : "Jan Krnavek", + "data" : [ + [ + "Raku", + 2 + ] + ], + "id" : "Jan Krnavek" + }, + { + "data" : [ + [ + "Perl", + 2 + ] + ], + "id" : "Jorg Sommrey", + "name" : "Jorg Sommrey" + }, + { + "id" : "Lubos Kolouch", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ], + "name" : "Lubos Kolouch" + }, + { + "name" : "Luca Ferrari", + "data" : [ + [ + "Raku", + 2 + ], + [ + "Blog", + 6 + ] + ], + "id" : "Luca Ferrari" + }, + { + "name" : "Mark Anderson", + "id" : "Mark Anderson", + "data" : [ + [ + "Raku", + 2 + ] + ] + }, + { + "name" : "Matthew Neleigh", + "data" : [ + [ + "Perl", + 1 + ] + ], + "id" : "Matthew Neleigh" + }, + { + "id" : "Matthias Muth", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ], + "name" : "Matthias Muth" + }, + { + "name" : "Packy Anderson", + "id" : "Packy Anderson", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ] + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Peter Campbell Smith", + "name" : "Peter Campbell Smith" + }, + { + "name" : "Peter Meszaros", + "id" : "Peter Meszaros", + "data" : [ + [ + "Perl", + 2 + ] + ] + }, + { + "name" : "rcmlz", + "data" : [ + [ + "Raku", + 2 + ] + ], + "id" : "rcmlz" + }, + { + "name" : "Robbie Hatley", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Robbie Hatley" + }, + { + "id" : "Robert DiCicco", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ] + ], + "name" : "Robert DiCicco" + }, + { + "id" : "Robert Ransbottom", + "data" : [ + [ + "Raku", + 2 + ] + ], + "name" : "Robert Ransbottom" + }, + { + "name" : "Roger Bell_West", + "id" : "Roger Bell_West", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ] + }, + { + "name" : "Simon Green", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Simon Green" + }, + { + "name" : "Thomas Kohler", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 2 + ] + ], + "id" : "Thomas Kohler" + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ] + ], + "id" : "Ulrich Rieke", + "name" : "Ulrich Rieke" + }, + { + "name" : "W. Luis Mochan", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "W. Luis Mochan" + }, + { + "name" : "Yves Orton", + "id" : "Yves Orton", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ] + } + ] + }, + "plotOptions" : { + "series" : { + "borderWidth" : 0, + "dataLabels" : { + "enabled" : 1, + "format" : "{point.y}" + } + } + }, + "yAxis" : { + "title" : { + "text" : "Total Solutions" + } + }, + "legend" : { + "enabled" : 0 + }, + "chart" : { + "type" : "column" + }, + "tooltip" : { + "headerFormat" : "{series.name}
", + "pointFormat" : "{point.name}: {point.y:f}
", + "followPointer" : 1 + }, + "subtitle" : { + "text" : "[Champions: 32] Last updated at 2023-10-09 19:29:15 GMT" + }, + "series" : [ + { + "name" : "The Weekly Challenge - 237", + "colorByPoint" : 1, + "data" : [ + { + "y" : 5, + "name" : "Ali Moradi", + "drilldown" : "Ali Moradi" + }, + { + "y" : 3, + "drilldown" : "Arne Sommer", + "name" : "Arne Sommer" + }, + { + "y" : 4, + "drilldown" : "Athanasius", + "name" : "Athanasius" + }, + { + "y" : 3, + "drilldown" : "Avery Adams", + "name" : "Avery Adams" + }, + { + "y" : 2, + "drilldown" : "BarrOff", + "name" : "BarrOff" + }, + { + "name" : "Bob Lied", + "drilldown" : "Bob Lied", + "y" : 3 + }, + { + "y" : 2, + "name" : "Bruce Gray", + "drilldown" : "Bruce Gray" + }, + { + "y" : 3, + "name" : "Dave Jacoby", + "drilldown" : "Dave Jacoby" + }, + { + "y" : 2, + "name" : "David Ferrone", + "drilldown" : "David Ferrone" + }, + { + "y" : 2, + "name" : "E. Choroba", + "drilldown" : "E. Choroba" + }, + { + "drilldown" : "Flavio Poletti", + "name" : "Flavio Poletti", + "y" : 6 + }, + { + "drilldown" : "Jaldhar H. Vyas", + "name" : "Jaldhar H. Vyas", + "y" : 5 + }, + { + "drilldown" : "Jan Krnavek", + "name" : "Jan Krnavek", + "y" : 2 + }, + { + "y" : 2, + "name" : "Jorg Sommrey", + "drilldown" : "Jorg Sommrey" + }, + { + "drilldown" : "Lubos Kolouch", + "name" : "Lubos Kolouch", + "y" : 5 + }, + { + "y" : 8, + "name" : "Luca Ferrari", + "drilldown" : "Luca Ferrari" + }, + { + "y" : 2, + "drilldown" : "Mark Anderson", + "name" : "Mark Anderson" + }, + { + "name" : "Matthew Neleigh", + "drilldown" : "Matthew Neleigh", + "y" : 1 + }, + { + "y" : 3, + "name" : "Matthias Muth", + "drilldown" : "Matthias Muth" + }, + { + "drilldown" : "Packy Anderson", + "name" : "Packy Anderson", + "y" : 5 + }, + { + "name" : "Peter Campbell Smith", + "drilldown" : "Peter Campbell Smith", + "y" : 3 + }, + { + "name" : "Peter Meszaros", + "drilldown" : "Peter Meszaros", + "y" : 2 + }, + { + "y" : 2, + "name" : "rcmlz", + "drilldown" : "rcmlz" + }, + { + "y" : 3, + "name" : "Robbie Hatley", + "drilldown" : "Robbie Hatley" + }, + { + "drilldown" : "Robert DiCicco", + "name" : "Robert DiCicco", + "y" : 4 + }, + { + "drilldown" : "Robert Ransbottom", + "name" : "Robert Ransbottom", + "y" : 2 + }, + { + "y" : 5, + "drilldown" : "Roger Bell_West", + "name" : "Roger Bell_West" + }, + { + "y" : 3, + "drilldown" : "Simon Green", + "name" : "Simon Green" + }, + { + "y" : 4, + "name" : "Thomas Kohler", + "drilldown" : "Thomas Kohler" + }, + { + "name" : "Ulrich Rieke", + "drilldown" : "Ulrich Rieke", + "y" : 4 + }, + { + "drilldown" : "W. Luis Mochan", + "name" : "W. Luis Mochan", + "y" : 3 + }, + { + "name" : "Yves Orton", + "drilldown" : "Yves Orton", + "y" : 3 + } + ] + } + ] +} diff --git a/stats/pwc-current.json b/stats/pwc-current.json index 2d57a02796..2aa454e141 100644 --- a/stats/pwc-current.json +++ b/stats/pwc-current.json @@ -1,209 +1,24 @@ { - "subtitle" : { - "text" : "[Champions: 32] Last updated at 2023-10-08 22:56:50 GMT" - }, - "tooltip" : { - "headerFormat" : "{series.name}
", - "followPointer" : 1, - "pointFormat" : "{point.name}: {point.y:f}
" - }, - "title" : { - "text" : "The Weekly Challenge - 237" - }, "legend" : { "enabled" : 0 }, - "chart" : { - "type" : "column" - }, "plotOptions" : { "series" : { + "borderWidth" : 0, "dataLabels" : { - "format" : "{point.y}", - "enabled" : 1 - }, - "borderWidth" : 0 + "enabled" : 1, + "format" : "{point.y}" + } } }, + "xAxis" : { + "type" : "category" + }, "drilldown" : { "series" : [ { - "id" : "Ali Moradi", - "data" : [ - [ - "Perl", - 2 - ], - [ - "Raku", - 2 - ], - [ - "Blog", - 1 - ] - ], - "name" : "Ali Moradi" - }, - { - "name" : "Arne Sommer", - "data" : [ - [ - "Raku", - 2 - ], - [ - "Blog", - 1 - ] - ], - "id" : "Arne Sommer" - }, - { - "name" : "Athanasius", - "id" : "Athanasius", - "data" : [ - [ - "Perl", - 2 - ], - [ - "Raku", - 2 - ] - ] - }, - { - "data" : [ - [ - "Perl", - 1 - ], - [ - "Blog", - 2 - ] - ], - "id" : "Avery Adams", - "name" : "Avery Adams" - }, - { - "id" : "BarrOff", - "data" : [ - [ - "Raku", - 2 - ] - ], - "name" : "BarrOff" - }, - { - "name" : "Bob Lied", - "id" : "Bob Lied", - "data" : [ - [ - "Perl", - 2 - ], - [ - "Blog", - 1 - ] - ] - }, - { - "name" : "Bruce Gray", - "id" : "Bruce Gray", - "data" : [ - [ - "Raku", - 2 - ] - ] - }, - { - "data" : [ - [ - "Perl", - 2 - ], - [ - "Blog", - 1 - ] - ], -