From 4015e8568518715d5108979cc183a7f111669176 Mon Sep 17 00:00:00 2001 From: drbaggy Date: Mon, 19 Jul 2021 06:46:49 +0100 Subject: pushing changes --- challenge-122/james-smith/perl/ch-1.pl | 20 ++++++++++++++++++++ challenge-122/james-smith/perl/ch-2.pl | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 challenge-122/james-smith/perl/ch-1.pl create mode 100644 challenge-122/james-smith/perl/ch-2.pl (limited to 'challenge-122') diff --git a/challenge-122/james-smith/perl/ch-1.pl b/challenge-122/james-smith/perl/ch-1.pl new file mode 100644 index 0000000000..c1d25b5736 --- /dev/null +++ b/challenge-122/james-smith/perl/ch-1.pl @@ -0,0 +1,20 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say state); +use Test::More; +use Benchmark qw(cmpthese timethis); +use Data::Dumper qw(Dumper); + +my @stream = map {$_*10} 1..50; + +say stream_average($_) foreach @stream; + +sub stream_average { + state($n,$t); + $n++; $t+=shift; + return $t/$n; +} + diff --git a/challenge-122/james-smith/perl/ch-2.pl b/challenge-122/james-smith/perl/ch-2.pl new file mode 100644 index 0000000000..a89fd8e826 --- /dev/null +++ b/challenge-122/james-smith/perl/ch-2.pl @@ -0,0 +1,32 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say); +use Test::More; +use Benchmark qw(cmpthese timethis); +use Data::Dumper qw(Dumper); + +## Pre-populate cache with first 3 values... +my %cache = ( + 1 => [1], + 2 => ['1 1',2], + 3 => ['1 1 1','1 2','2 1',3], +); +say join "\n",@{pts(4)}; +say ""; +say join "\n",@{pts(5)}; + +sub pts { + my $n = shift; + return [] if $n < 1; + return $cache{$n} if exists $cache{$n}; + my @res = ( + map( {"1 $_"} @{pts($n-1)} ), + map( {"2 $_"} @{pts($n-2)} ), + map( {"3 $_"} @{pts($n-3)} ) + ); + return $cache{$n} = \@res; +} + -- cgit From 58bf5d8427ab57ee8dc095567bab5ffca7160403 Mon Sep 17 00:00:00 2001 From: Mark A Date: Sun, 18 Jul 2021 23:53:32 -0600 Subject: initial 122 --- challenge-122/mark-anderson/raku/ch-1.raku | 16 ++++++++++++++++ challenge-122/mark-anderson/raku/ch-2.raku | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 challenge-122/mark-anderson/raku/ch-1.raku create mode 100644 challenge-122/mark-anderson/raku/ch-2.raku (limited to 'challenge-122') diff --git a/challenge-122/mark-anderson/raku/ch-1.raku b/challenge-122/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..ef2f39b2a1 --- /dev/null +++ b/challenge-122/mark-anderson/raku/ch-1.raku @@ -0,0 +1,16 @@ +#!/usr/bin/env raku + +use Test; +plan 6; + +is avg-seq(1 ... ∞), (1, 1.5 ... 10.5); +is avg-seq(1, 3 ... ∞), (1 ... 20); +is avg-seq(2, 4 ... ∞), (2 ... 21); +is avg-seq(3, 6 ... ∞), (3, 4.5 ... 31.5); +is avg-seq(4, 8 ... ∞), (4, 6 ... 42); +is avg-seq(10, 20 ... ∞), (10, 15 ... 105); + +sub avg-seq($seq) +{ + $seq.head(20).map({ .narrow with (state $sum += $_) / ++$ }); +} diff --git a/challenge-122/mark-anderson/raku/ch-2.raku b/challenge-122/mark-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..1bcac2dff3 --- /dev/null +++ b/challenge-122/mark-anderson/raku/ch-2.raku @@ -0,0 +1,16 @@ +#!/usr/bin/env raku + +unit sub MAIN(UInt $S); + +.say for b-ball-points($S); + +multi b-ball-points($S where * == 1) { (1,), } +multi b-ball-points($S where * == 2) { (1, 1), (2,) } +multi b-ball-points($S where * == 3) { (1, 1, 1), (1, 2), (2, 1), (3,) } + +multi b-ball-points($S) +{ + (1..2**($S-1)-1) + .map({ (.fmt('%0' ~ $S ~ 'b') ~~ m:g/ (.) [$0+]? /).map({ .chars }) }) + .grep({ .cache.all < 4 }) +} -- cgit From 993ffb1d234c969417741a4e3a5721f66e8bf981 Mon Sep 17 00:00:00 2001 From: Mark A Date: Mon, 19 Jul 2021 00:30:43 -0600 Subject: initial 122 --- challenge-122/mark-anderson/raku/ch-1.raku | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'challenge-122') diff --git a/challenge-122/mark-anderson/raku/ch-1.raku b/challenge-122/mark-anderson/raku/ch-1.raku index ef2f39b2a1..02ffa0bb96 100644 --- a/challenge-122/mark-anderson/raku/ch-1.raku +++ b/challenge-122/mark-anderson/raku/ch-1.raku @@ -3,12 +3,12 @@ use Test; plan 6; -is avg-seq(1 ... ∞), (1, 1.5 ... 10.5); -is avg-seq(1, 3 ... ∞), (1 ... 20); -is avg-seq(2, 4 ... ∞), (2 ... 21); -is avg-seq(3, 6 ... ∞), (3, 4.5 ... 31.5); -is avg-seq(4, 8 ... ∞), (4, 6 ... 42); -is avg-seq(10, 20 ... ∞), (10, 15 ... 105); +is-deeply avg-seq(1 ... ∞), (1, 1.5 ... 10.5)>>.narrow; +is-deeply avg-seq(1, 3 ... ∞), (1 ... 20); +is-deeply avg-seq(2, 4 ... ∞), (2 ... 21); +is-deeply avg-seq(3, 6 ... ∞), (3, 4.5 ... 31.5)>>.narrow; +is-deeply avg-seq(4, 8 ... ∞), (4, 6 ... 42); +is-deeply avg-seq(10, 20 ... ∞), (10, 15 ... 105); sub avg-seq($seq) { -- cgit From 52a7ca4f612c774a2f87cab8d145b683f53c26ca Mon Sep 17 00:00:00 2001 From: Scimon Proctor Date: Mon, 19 Jul 2021 09:36:05 +0100 Subject: Challenge 1 --- challenge-122/simon-proctor/raku/ch-1.raku | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 challenge-122/simon-proctor/raku/ch-1.raku (limited to 'challenge-122') diff --git a/challenge-122/simon-proctor/raku/ch-1.raku b/challenge-122/simon-proctor/raku/ch-1.raku new file mode 100644 index 0000000000..5c685c3dd0 --- /dev/null +++ b/challenge-122/simon-proctor/raku/ch-1.raku @@ -0,0 +1,19 @@ +#!/usr/bin/env raku + +sub avg($avg,$val) { + state $count = 1; + my $sum = $avg*$count; + $sum += $val; + $count++; + $sum / $count; +} + +#| Given a list of numbers print the average of the list with each point in the list +multi sub MAIN( *@N ) { + ( [\[&avg]] @N ).join(", ").say; +} + +#| Read from STDIN and output the running average after each number +multi sub MAIN() { + .say for [\[&avg]] $*IN.lines; +} -- cgit From ecb612591c012ba4de6480192e0e049ec8d17f00 Mon Sep 17 00:00:00 2001 From: Scimon Proctor Date: Mon, 19 Jul 2021 09:56:06 +0100 Subject: Challenge 2 --- challenge-122/simon-proctor/raku/ch-2.raku | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 challenge-122/simon-proctor/raku/ch-2.raku (limited to 'challenge-122') diff --git a/challenge-122/simon-proctor/raku/ch-2.raku b/challenge-122/simon-proctor/raku/ch-2.raku new file mode 100644 index 0000000000..1c10942d42 --- /dev/null +++ b/challenge-122/simon-proctor/raku/ch-2.raku @@ -0,0 +1,11 @@ +#!/usr/bin/env raku + +#| Given a number print all the possible ways to score that in basketball +sub MAIN (Int() $N) { + .say for (|(1 xx $N), |(2 xx $N), |(3 xx $N)) + .combinations(1..$N) + .unique(:with(&[eqv])) + .grep( -> @l { ([+] @l) ~~ $N } ) + .map( -> @l { @l.permutations.unique(:with(&[eqv])).Slip } ) + .map( *.join(",") ) +} -- cgit From f0355014578390a127272346354f339b3d2a68d0 Mon Sep 17 00:00:00 2001 From: Scimon Proctor Date: Mon, 19 Jul 2021 09:59:47 +0100 Subject: Remove uneeded duplication --- challenge-122/simon-proctor/raku/ch-2.raku | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'challenge-122') diff --git a/challenge-122/simon-proctor/raku/ch-2.raku b/challenge-122/simon-proctor/raku/ch-2.raku index 1c10942d42..8487b83a62 100644 --- a/challenge-122/simon-proctor/raku/ch-2.raku +++ b/challenge-122/simon-proctor/raku/ch-2.raku @@ -2,7 +2,7 @@ #| Given a number print all the possible ways to score that in basketball sub MAIN (Int() $N) { - .say for (|(1 xx $N), |(2 xx $N), |(3 xx $N)) + .say for (|(1 xx $N), |(2 xx ($N div 2)), |(3 xx ($N div 3))) .combinations(1..$N) .unique(:with(&[eqv])) .grep( -> @l { ([+] @l) ~~ $N } ) -- cgit From 48a6bc564172521fbf96fe3b8364df84173c0485 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 19 Jul 2021 12:56:24 +0200 Subject: README for week 122 --- challenge-122/abigail/README.md | 87 ++++++++++++++++++++++------------------- 1 file changed, 46 insertions(+), 41 deletions(-) (limited to 'challenge-122') diff --git a/challenge-122/abigail/README.md b/challenge-122/abigail/README.md index 6179e4c324..15e7524445 100644 --- a/challenge-122/abigail/README.md +++ b/challenge-122/abigail/README.md @@ -1,33 +1,20 @@ # Solutions by Abigail -## [Invert Bit][task1] +## [Average of Stream][task1] -[task1]: https://perlweeklychallenge.org/blog/perl-weekly-challenge-121/#TASK1 - -> You are given integers `0 <= $m <= 255` and `1 <= $n <= 8`. +> You are given a stream of numbers, `@N`. > -> Write a script to invert `$n` bit from the end of the binary -> representation of `$m` and print the decimal representation of -> the new binary number. - -### Examples -~~~~ -Input: $m = 12, $n = 3 -Output: 8 -~~~~ - -* Binary representation of `$m = 00001100` -* Invert 3rd bit from the end `= 00001000` -* Decimal equivalent of `00001000 = 8` +> Write a script to print the average of the stream at every point. +### Example ~~~~ -Input: $m = 18, $n = 4 -Output: 26 +Input: @N = (10, 20, 30, 40, 50, 60, 70, 80, 90, ...) +Output: 10, 15, 20, 25, 30, 35, 40, 45, 50, ... ~~~~ -* Binary representation of `$m = 00010010` -* Invert 3rd bit from the end `= 00011010` -* Decimal equivalent of `00011010 = 26` - +* Average of first number is `10`. +* Average of first 2 numbers `(10+20)/2 = 15`. +* Average of first 3 numbers `(10+20+30)/3 = 20`. +* Average of first 4 numbers `(10+20+30+40)/4 = 25` and so on. ### Solutions * [AWK](awk/ch-1.awk) @@ -48,25 +35,43 @@ Output: 26 * [Tcl](tcl/ch-1.tcl) ### Blog -[Invert Bit][blog1] +[Average of Stream][blog1] -## [The Travelling Salesman][task2] +## [Basketball Points][task2] -> You are given a `NxN` matrix containing the distances between `N` cities. +> You are given a score `$S`. +> +> You can win basketball points e.g. `1` point, `2` points and `3` points. > -> Write a script to find a round trip of minimum length visiting all `N` -> cities exactly once and returning to the start. +> Write a script to find out the different ways you can score `$S`. -### Example +### Examples +~~~~ +Input: $S = 4 +Output: 1 1 1 1 + 1 1 2 + 1 2 1 + 1 3 + 2 1 1 + 2 2 + 3 1 ~~~~ -Matrix: [0, 5, 2, 7] - [5, 0, 5, 3] - [3, 1, 0, 6] - [4, 5, 4, 0] -Output: - length = 10 - tour = (0 2 1 3 0) +~~~~ +Input: $S = 5 +Output: 1 1 1 1 1 + 1 1 1 2 + 1 1 2 1 + 1 1 3 + 1 2 1 1 + 1 2 2 + 1 3 1 + 2 1 1 1 + 2 1 2 + 2 2 1 + 2 3 + 3 1 1 + 3 2 ~~~~ ### Solutions @@ -80,11 +85,11 @@ Output: * [Ruby](ruby/ch-2.rb) ### Blog -[The Travelling Salesman][blog2] +[Basketball Points][blog2] -[task1]: https://perlweeklychallenge.org/blog/perl-weekly-challenge-121/#TASK1 -[task1]: https://perlweeklychallenge.org/blog/perl-weekly-challenge-121/#TASK2 -[blog1]: https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-121-1.html -[blog2]: https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-121-2.html +[task1]: https://perlweeklychallenge.org/blog/perl-weekly-challenge-122/#TASK1 +[task1]: https://perlweeklychallenge.org/blog/perl-weekly-challenge-122/#TASK2 +[blog1]: https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-122-1.html +[blog2]: https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-122-2.html -- cgit From 4ded51d7ecb942711df0c3dc0c9fca2f3ea1186a Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 19 Jul 2021 13:00:14 +0200 Subject: Tests for week 122 --- challenge-122/abigail/t/ctest.ini | 9 +++++++++ challenge-122/abigail/t/input-1-1 | 9 +++++++++ challenge-122/abigail/t/input-2-1 | 1 + challenge-122/abigail/t/input-2-2 | 1 + challenge-122/abigail/t/output-1-1.exp | 9 +++++++++ challenge-122/abigail/t/output-2-1.exp | 7 +++++++ challenge-122/abigail/t/output-2-2.exp | 13 +++++++++++++ 7 files changed, 49 insertions(+) create mode 100644 challenge-122/abigail/t/ctest.ini create mode 100644 challenge-122/abigail/t/input-1-1 create mode 100644 challenge-122/abigail/t/input-2-1 create mode 100644 challenge-122/abigail/t/input-2-2 create mode 100644 challenge-122/abigail/t/output-1-1.exp create mode 100644 challenge-122/abigail/t/output-2-1.exp create mode 100644 challenge-122/abigail/t/output-2-2.exp (limited to 'challenge-122') diff --git a/challenge-122/abigail/t/ctest.ini b/challenge-122/abigail/t/ctest.ini new file mode 100644 index 0000000000..13d8fe84d2 --- /dev/null +++ b/challenge-122/abigail/t/ctest.ini @@ -0,0 +1,9 @@ +# +# Configuration file for running tests, using ctest. +# See https://github.com/Abigail/Misc/blob/master/ctest +# + +[names] +1-1 = Given Example +2-1 = Example 1§ +2-2 = Example 2 diff --git a/challenge-122/abigail/t/input-1-1 b/challenge-122/abigail/t/input-1-1 new file mode 100644 index 0000000000..ee26a47c52 --- /dev/null +++ b/challenge-122/abigail/t/input-1-1 @@ -0,0 +1,9 @@ +10 +20 +30 +40 +50 +60 +70 +80 +90 diff --git a/challenge-122/abigail/t/input-2-1 b/challenge-122/abigail/t/input-2-1 new file mode 100644 index 0000000000..b8626c4cff --- /dev/null +++ b/challenge-122/abigail/t/input-2-1 @@ -0,0 +1 @@ +4 diff --git a/challenge-122/abigail/t/input-2-2 b/challenge-122/abigail/t/input-2-2 new file mode 100644 index 0000000000..7ed6ff82de --- /dev/null +++ b/challenge-122/abigail/t/input-2-2 @@ -0,0 +1 @@ +5 diff --git a/challenge-122/abigail/t/output-1-1.exp b/challenge-122/abigail/t/output-1-1.exp new file mode 100644 index 0000000000..c524cd4d4c --- /dev/null +++ b/challenge-122/abigail/t/output-1-1.exp @@ -0,0 +1,9 @@ +10 +15 +20 +25 +30 +35 +40 +45 +50 diff --git a/challenge-122/abigail/t/output-2-1.exp b/challenge-122/abigail/t/output-2-1.exp new file mode 100644 index 0000000000..4bd53d531d --- /dev/null +++ b/challenge-122/abigail/t/output-2-1.exp @@ -0,0 +1,7 @@ +1 1 1 1 +1 1 2 +1 2 1 +1 3 +2 1 1 +2 2 +3 1 diff --git a/challenge-122/abigail/t/output-2-2.exp b/challenge-122/abigail/t/output-2-2.exp new file mode 100644 index 0000000000..47bad01848 --- /dev/null +++ b/challenge-122/abigail/t/output-2-2.exp @@ -0,0 +1,13 @@ +1 1 1 1 1 +1 1 1 2 +1 1 2 1 +1 1 3 +1 2 1 1 +1 2 2 +1 3 1 +2 1 1 1 +2 1 2 +2 2 1 +2 3 +3 1 1 +3 2 -- cgit From 580f84362298c4c14b3b41aa124dd3bb99d448f6 Mon Sep 17 00:00:00 2001 From: drbaggy Date: Mon, 19 Jul 2021 13:09:15 +0100 Subject: 2nd pass with comments --- challenge-122/james-smith/perl/ch-1.pl | 21 ++++++++++++++----- challenge-122/james-smith/perl/ch-2.pl | 38 ++++++++++++++++++++-------------- 2 files changed, 38 insertions(+), 21 deletions(-) (limited to 'challenge-122') diff --git a/challenge-122/james-smith/perl/ch-1.pl b/challenge-122/james-smith/perl/ch-1.pl index c1d25b5736..e85a90e949 100644 --- a/challenge-122/james-smith/perl/ch-1.pl +++ b/challenge-122/james-smith/perl/ch-1.pl @@ -8,13 +8,24 @@ use Test::More; use Benchmark qw(cmpthese timethis); use Data::Dumper qw(Dumper); -my @stream = map {$_*10} 1..50; +stream( map {$_*10} 1..50 ); ## Push values into stream... +eval {say stream_average();} until $@; ## Use eval/$@ to repeat until stream dies. -say stream_average($_) foreach @stream; +sub stream { + state(@stream); + @_ ? (push @stream,@_) ## Parameters passed - push to stream + : @stream ? shift @stream ## We have entry in stream return it + : die; ## exhausted stream die.... +} sub stream_average { + ## Use state variables for the total & count; state($n,$t); - $n++; $t+=shift; - return $t/$n; -} + ## Take next element and add to total + ## Increment the count, and return the ratio of the true values + ## Note we need to do pre-increment rather than + ## post increment so the incremement is done before use. + + return ($t+=stream)/++$n; +} diff --git a/challenge-122/james-smith/perl/ch-2.pl b/challenge-122/james-smith/perl/ch-2.pl index a89fd8e826..9fa94f6fb8 100644 --- a/challenge-122/james-smith/perl/ch-2.pl +++ b/challenge-122/james-smith/perl/ch-2.pl @@ -10,23 +10,29 @@ use Data::Dumper qw(Dumper); ## Pre-populate cache with first 3 values... my %cache = ( - 1 => [1], - 2 => ['1 1',2], - 3 => ['1 1 1','1 2','2 1',3], + 1 => [qw(1)], + 2 => [qw(11 2)], + 3 => [qw(111 12 21 3)], ); -say join "\n",@{pts(4)}; -say ""; -say join "\n",@{pts(5)}; - +say join "\n",'',"SIZE $_",'',@{pts($_)} foreach 0..20; +say ''; sub pts { - my $n = shift; - return [] if $n < 1; - return $cache{$n} if exists $cache{$n}; - my @res = ( - map( {"1 $_"} @{pts($n-1)} ), - map( {"2 $_"} @{pts($n-2)} ), - map( {"3 $_"} @{pts($n-3)} ) - ); - return $cache{$n} = \@res; + ## Let's look at the first points scored - it is either + ## 1, 2 or 3. + ## So we then look to see how remaining points are scored + ## these are 1 - followed by all combinations for n-1 + ## 2 - followed by all combinations for n-2 + ## 3 - followed by all combinations for n-3 + ## The special cases are therefore where any of these values + ## are less than 1 - so we need to have pre-populated values + ## for 1, 2 and 3 points (1; 11+2; 111+12+21+3 respectively) + ## We cache values as we know that we will see certain + ## values occuring repeatedly {e.g. the start sequences + ## 111, 12, 21 3 all then get all sequences for n-3 + return $cache{$_[0]} ||= $_[0] < 1 ? [] : [ + map( {'1'.$_} @{pts( $_[0]-1 )} ), + map( {'2'.$_} @{pts( $_[0]-2 )} ), + map( {'3'.$_} @{pts( $_[0]-3 )} ) + ]; } -- cgit From 122e5e621f85265d9df4bf2ba6c9ed8b1b186518 Mon Sep 17 00:00:00 2001 From: James Smith Date: Mon, 19 Jul 2021 13:11:19 +0100 Subject: Pass 2 with docs --- challenge-122/james-smith/README.md | 116 +++++++++++++----------------------- challenge-122/james-smith/blog.txt | 1 + 2 files changed, 42 insertions(+), 75 deletions(-) create mode 100644 challenge-122/james-smith/blog.txt (limited to 'challenge-122') diff --git a/challenge-122/james-smith/README.md b/challenge-122/james-smith/README.md index 34633d404a..14f66ec426 100644 --- a/challenge-122/james-smith/README.md +++ b/challenge-122/james-smith/README.md @@ -1,4 +1,4 @@ -# Perl Weekly Challenge #120 +# Perl Weekly Challenge #122 You can find more information about this weeks, and previous weeks challenges at: @@ -10,99 +10,65 @@ submit solutions in whichever language you feel comfortable with. You can find the solutions here on github at: -https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-121/james-smith/perl +https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-122/james-smith/perl -# Task 1 - Invert Bit +# Task 1 - Average of Stream -***You are given integers `0 <= $m <= 255` and `1 <= $n <= 8`. Write a script to invert `$n` bit from the end of the binary representation of `$m` and print the decimal representation of the new binary number.*** +***You are given a stream of numbers, `@N`. Write a script to print the average of the stream at every point.*** ## The solution -To invert a particular bit we can **xor**(`^`) `2^($n-1)` with the number in question. We can simplify the `2^($n-1)` by rewriting as `1<<$n-1` this is using bit-shift operators. So the function simply becomes. +Firstly - we create a "stream object" - we use a single function +`stream()` for this which is a get/setter - call with a sequence of data +and this pushes the values onto the stream. Call it without and it +returns the first value of the stream OR dies. + +`stream_average` just pulls the next value from the stream (or dies) +and computes the average -- updates total(`$t`) and count(`$n`) -- and +returns the `$t`/`$n` ```perl -sub flip_bit { - return $_[0] ^ 1<<$_[1]-1; +stream( map {$_*10} 1..50 ); ## Push values into stream... + +eval {say stream_average();} until $@; + +sub stream { + state(@stream); + @_ ? (push @stream,@_) + : @stream ? shift @stream + : die; } + +sub stream_average { + state($n,$t); + return ($t+=stream) / ++$n; + } + ``` -# Task 2 - The Travelling Salesman +# Task 2 - Basketball Points -***You are given a `NxN` matrix containing the distances between `N` cities. Write a script to find a round trip of minimum length visiting all `N` cities exactly once and returning to the start.*** +***You are given a score `$S`. You can win basketball points e.g. 1 point, 2 points and 3 points. Write a script to find out the different ways you can score `$S`..*** ## Solution. -This is very similar to the pre-computed distance "Adventure of Knight" solution from challenge 118, the only change we have to do is add the distance from the last node to the start node. The only change is when the list of "targets" left is of length 1. In that problem we were not expected to return to the start... +To get the combinations for a give number - we can shoot a 1, 2 or 3 point shot and then reconsider the combinations for the remaining point. -The addition is this condition: -```perl - if(@r=1) { - $len = $dist_maps->[$start][$r[0]] + $dist_maps->[$r[0]][0]; - $rt = chr(65+$r[0]).'A'; - } else { -``` - -Giving us our walking algorithm.... {the calls count is just to see how efficient the caching is} +This leads us to a simple recursive solution. ```perl -sub optimal_route { - $calls++; - my($start,@r) = @_; - my $key = "$start @{[ sort @r ]}"; - return $CACHE{$key} if exists $CACHE{$key}; - my $len = 1e99; - my $rt = ''; - if(@r==1) { - $len = $dist_maps->[$start][$r[0]] + $dist_maps->[$r[0]][0]; - $rt = chr(65+$r[0]).'A'; - } else { - foreach(0..$#r) { - my $t = shift @r; - my $x = optimal_route($t,@r); - my $l = $dist_maps->[$start][$t] + $x->[0]; - if( $l < $len ) { - $len = $l; - $rt = (chr(65+$t)).$x->[1]; - } - push @r,$t; - } - } - return $CACHE{$key} = [$len,$rt]; +sub pts { + return $cache{$_[0]} ||= $_[0] < 1 ? [] : [ + map( {'1'.$_} @{pts( $_[0] - 1 )} ), + map( {'2'.$_} @{pts( $_[0] - 2 )} ), + map( {'3'.$_} @{pts( $_[0] - 3 )} ) + ]; } ``` -Running this for size 20 give us this output... {obv a randomised distance matrix} +**Note** To reduce complexity we pre-populate the cache for the first three cases: -``` -Distance matrix: - 0 8 7 18 3 14 5 18 16 10 0 18 8 0 12 5 0 0 3 7 - 3 0 7 0 3 17 8 5 0 7 6 19 8 13 14 18 19 15 11 7 - 7 12 0 14 14 4 11 11 11 6 5 15 11 1 8 12 19 6 14 12 - 19 1 5 0 5 7 11 3 7 17 8 14 8 12 4 18 17 15 19 7 - 14 3 8 12 0 1 18 2 4 12 5 10 7 2 11 11 19 1 6 1 - 10 17 6 14 4 0 9 12 13 7 19 14 16 1 12 13 3 18 9 7 - 17 5 5 4 10 3 0 19 1 15 10 2 18 3 3 4 18 11 18 9 - 15 12 12 15 5 6 5 0 0 5 6 6 0 2 16 10 10 4 9 18 - 3 17 4 3 0 5 14 14 0 15 12 8 12 8 10 3 6 18 9 17 - 6 13 5 2 12 15 5 5 13 0 12 16 12 18 8 6 12 3 9 18 - 15 15 19 2 2 14 0 11 13 13 0 16 17 17 9 3 12 2 13 1 - 4 18 5 18 9 10 9 13 16 2 15 0 0 8 1 19 5 18 5 6 - 7 17 5 6 18 5 19 13 8 15 10 6 0 1 3 5 17 5 8 3 - 5 19 3 16 6 5 16 3 16 18 17 18 10 0 17 18 8 4 4 8 - 6 16 18 4 16 6 6 7 0 15 18 2 3 16 0 10 16 1 12 0 - 9 3 17 18 12 5 12 10 1 3 17 19 3 13 16 0 13 9 8 11 - 13 6 12 17 2 2 0 12 9 2 16 1 19 15 11 3 0 17 7 6 - 13 12 9 13 3 17 16 10 10 7 7 1 7 11 14 10 12 0 4 7 - 18 18 14 10 14 1 5 17 4 9 0 11 13 8 14 11 17 19 0 7 - 14 14 17 13 2 5 6 12 8 11 12 6 19 18 16 10 1 5 11 0 -Routes: 121,645,100,408,832,000 -Calls: 44,826,302 { 1 : 2,713,699,212 } -Cache: 4,980,718 { 1 : 24,423,205,732 } -Length: 32 -Route: A R L O T Q G I E H M C F N S K P J D B A -Time: 181.083702 seconds -``` + 1. "1" + 2. "11" & "2" + 3. "111", "12", "21" and "3" -As you can see from the output the filtering is very efficient in this case quickly reducing the number -of routes investigated from 121 quadrillion to a handful of millions. The algorithm is "limited" by -memory as the cache grows rapidly as `$N` increases. diff --git a/challenge-122/james-smith/blog.txt b/challenge-122/james-smith/blog.txt new file mode 100644 index 0000000000..dc304a8bac --- /dev/null +++ b/challenge-122/james-smith/blog.txt @@ -0,0 +1 @@ +https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-122/james-smith -- cgit From 6b112bb1931a60ebdec122eeee697b0d83a76ae2 Mon Sep 17 00:00:00 2001 From: Roger Bell_West Date: Mon, 19 Jul 2021 13:56:03 +0100 Subject: Solutions for challenge #122 --- challenge-122/roger-bell-west/perl/ch-1.pl | 21 +++++++++ challenge-122/roger-bell-west/perl/ch-2.pl | 52 +++++++++++++++++++++ challenge-122/roger-bell-west/postscript/ch-1.ps | 44 ++++++++++++++++++ challenge-122/roger-bell-west/python/ch-1.py | 21 +++++++++ challenge-122/roger-bell-west/python/ch-2.py | 53 ++++++++++++++++++++++ challenge-122/roger-bell-west/raku/ch-1.p6 | 19 ++++++++ challenge-122/roger-bell-west/ruby/ch-1.rb | 23 ++++++++++ challenge-122/roger-bell-west/ruby/ch-2.rb | 57 +++++++++++++++++++++++ challenge-122/roger-bell-west/rust/ch-1.rs | 19 ++++++++ challenge-122/roger-bell-west/rust/ch-2.rs | 58 ++++++++++++++++++++++++ 10 files changed, 367 insertions(+) create mode 100755 challenge-122/roger-bell-west/perl/ch-1.pl create mode 100755 challenge-122/roger-bell-west/perl/ch-2.pl create mode 100644 challenge-122/roger-bell-west/postscript/ch-1.ps create mode 100755 challenge-122/roger-bell-west/python/ch-1.py create mode 100755 challenge-122/roger-bell-west/python/ch-2.py create mode 100755 challenge-122/roger-bell-west/raku/ch-1.p6 create mode 100755 challenge-122/roger-bell-west/ruby/ch-1.rb create mode 100755 challenge-122/roger-bell-west/ruby/ch-2.rb create mode 100755 challenge-122/roger-bell-west/rust/ch-1.rs create mode 100755 challenge-122/roger-bell-west/rust/ch-2.rs (limited to 'challenge-122') diff --git a/challenge-122/roger-bell-west/perl/ch-1.pl b/challenge-122/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..d209bf2324 --- /dev/null +++ b/challenge-122/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,21 @@ +#! /usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 1; + +is_deeply(aos([10, 20, 30, 40, 50, 60, 70, 80, 90]),[10, 15, 20, 25, 30, 35, 40, 45, 50],'example 1'); + +sub aos { + my $m=shift; + my $n=0; + my $t=0; + my @o; + foreach my $i (@{$m}) { + $t+=$i; + $n++; + push @o,$t/$n; + } + return \@o; +} diff --git a/challenge-122/roger-bell-west/perl/ch-2.pl b/challenge-122/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..eb30ab32aa --- /dev/null +++ b/challenge-122/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,52 @@ +#! /usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 2; + +is_deeply(bp(4),[ + [1,1,1,1], + [1,1,2], + [1,2,1], + [1,3], + [2,1,1], + [2,2], + [3,1], + ],'example 1'); + +is_deeply(bp(5),[ + [1,1,1,1,1], + [1,1,1,2], + [1,1,2,1], + [1,1,3], + [1,2,1,1], + [1,2,2], + [1,3,1], + [2,1,1,1], + [2,1,2], + [2,2,1], + [2,3], + [3,1,1], + [3,2], + ],'example 2'); + +use List::Util qw(sum0 min); + +sub bp { + my $n=shift; + my @o; + my @p=([]); + while (my $s=pop @p) { + my $t=sum0(@{$s}); + if ($t==$n) { + push @o,$s; + } else { + foreach my $i (1..min(3,$n-$t)) { + push @p,[@{$s},$i]; + } + } + } + @o=reverse @o; + return \@o; +} diff --git a/challenge-122/roger-bell-west/postscript/ch-1.ps b/challenge-122/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..05ae33116c --- /dev/null +++ b/challenge-122/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,44 @@ +%! Not DSC compliant + +/aos { + /n 0 def + /t 0 def + dup length array /o exch def + { + n add /n exch def + o t + /t t 1 add def + n t div cvi put + } forall + o +} def + +/str 50 string def +/fs 12 def +/Helvetica findfont +fs scalefont setfont +20 750 translate +/line 0 def +/testnum 1 def + +/disptest { + 0 line moveto testnum str cvs show (.) show + aos + /to exch def + /ti exch def + /pass true def + 0 1 ti length 1 sub { + dup ti exch get exch to exch get ne + { /pass false def exit } if + } for + pass { (Pass) } { (FAIL) } ifelse + 50 line moveto show + /line line fs 2 mul sub def + /testnum testnum 1 add def +} def + +[ 10 15 20 25 30 35 40 45 50 ] +[ 10 20 30 40 50 60 70 80 90 ] +disptest + +showpage diff --git a/challenge-122/roger-bell-west/python/ch-1.py b/challenge-122/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..773886135d --- /dev/null +++ b/challenge-122/roger-bell-west/python/ch-1.py @@ -0,0 +1,21 @@ +#! /usr/bin/python3 + +import unittest +import re + +def aos(m): + n=0 + t=0 + o=list() + for i in m: + t+=i + n+=1 + o.append(t/n) + return o + +class TestAos(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(aos([10, 20, 30, 40, 50, 60, 70, 80, 90]),[10, 15, 20, 25, 30, 35, 40, 45, 50],'example 1') + +unittest.main() diff --git a/challenge-122/roger-bell-west/python/ch-2.py b/challenge-122/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..181032a9dc --- /dev/null +++ b/challenge-122/roger-bell-west/python/ch-2.py @@ -0,0 +1,53 @@ +#! /usr/bin/python3 + +import unittest +import copy + +def bs(n): + o=list() + p=list() + p.append(list()) + while len(p) > 0: + s=p.pop() + t=sum(s) + if t==n: + o.append(s) + else: + for i in range(1,1+min(3,n-t)): + q=copy.copy(s) + q.append(i) + p.append(q) + o.reverse() + return o + +class TestBs(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(bs(4),[ + [1,1,1,1], + [1,1,2], + [1,2,1], + [1,3], + [2,1,1], + [2,2], + [3,1] + ],'example 1') + + def test_ex2(self): + self.assertEqual(bs(5),[ + [1,1,1,1,1], + [1,1,1,2], + [1,1,2,1], + [1,1,3], + [1,2,1,1], + [1,2,2], + [1,3,1], + [2,1,1,1], + [2,1,2], + [2,2,1], + [2,3], + [3,1,1], + [3,2] + ],'example 2') + +unittest.main() diff --git a/challenge-122/roger-bell-west/raku/ch-1.p6 b/challenge-122/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..1fe1babf56 --- /dev/null +++ b/challenge-122/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,19 @@ +#! /usr/bin/perl6 + +use Test; + +plan 1; + +is-deeply(aos([10, 20, 30, 40, 50, 60, 70, 80, 90]),[10, 15, 20, 25, 30, 35, 40, 45, 50],'example 1'); + +sub aos(@m) { + my $n=0; + my $t=0; + my @o; + for @m -> $i { + $t+=$i; + $n++; + push @o,floor($t/$n); + } + return @o; +} diff --git a/challenge-122/roger-bell-west/ruby/ch-1.rb b/challenge-122/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..7ef095b1b8 --- /dev/null +++ b/challenge-122/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,23 @@ +#! /usr/bin/ruby + +def aos(m) + n=0 + t=0 + o=[] + m.each {|i| + t+=i + n+=1 + o.push(t/n) + } + return o +end + +require 'test/unit' + +class TestAos < Test::Unit::TestCase + + def test_ex1 + assert_equal([10, 15, 20, 25, 30, 35, 40, 45, 50],aos([10, 20, 30, 40, 50, 60, 70, 80, 90])) + end + +end diff --git a/challenge-122/roger-bell-west/ruby/ch-2.rb b/challenge-122/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..bee3384481 --- /dev/null +++ b/challenge-122/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,57 @@ +#! /usr/bin/ruby + +def bs(n) + o=[] + p=[[]] + while p.length() > 0 do + s=p.pop() + t=s.sum() + if t==n then + o.push(s) + else + 1.upto([3,n-t].min()) {|i| + q=s.dup + q.push(i) + p.push(q) + } + end + end + o.reverse!() + return o +end + +require 'test/unit' + +class TestBs < Test::Unit::TestCase + + def test_ex1 + assert_equal([ + [1,1,1,1], + [1,1,2], + [1,2,1], + [1,3], + [2,1,1], + [2,2], + [3,1] + ],bs(4)) + end + + def test_ex2 + assert_equal([ + [1,1,1,1,1], + [1,1,1,2], + [1,1,2,1], + [1,1,3], + [1,2,1,1], + [1,2,2], + [1,3,1], + [2,1,1,1], + [2,1,2], + [2,2,1], + [2,3], + [3,1,1], + [3,2] + ],bs(5)) + end + +end diff --git a/challenge-122/roger-bell-west/rust/ch-1.rs b/challenge-122/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..c68808dd57 --- /dev/null +++ b/challenge-122/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,19 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x; rm -f ${0}x ; exit + +#[test] +fn test_ex1() { + assert_eq!(aos(vec![10, 20, 30, 40, 50, 60, 70, 80, 90]),vec![10, 15, 20, 25, 30, 35, 40, 45, 50]); +} + +fn aos (m: Vec) -> Vec { + let mut n=0; + let mut t=0; + let mut o=vec![]; + for i in m { + t+=i; + n+=1; + o.push(t/n) + } + return o; +} diff --git a/challenge-122/roger-bell-west/rust/ch-2.rs b/challenge-122/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..5080e5002c --- /dev/null +++ b/challenge-122/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,58 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x; rm -f ${0}x ; exit + +#[test] +fn test_ex1() { + assert_eq!(bp(4),vec![ + vec![1,1,1,1], + vec![1,1,2], + vec![1,2,1], + vec![1,3], + vec![2,1,1], + vec![2,2], + vec![3,1], + ]); +} + +#[test] +fn test_ex2() { + assert_eq!(bp(5),vec![ + vec![1,1,1,1,1], + vec![1,1,1,2], + vec![1,1,2,1], + vec![1,1,3], + vec![1,2,1,1], + vec![1,2,2], + vec![1,3,1], + vec![2,1,1,1], + vec![2,1,2], + vec![2,2,1], + vec![2,3], + vec![3,1,1], + vec![3,2], + ]); +} + +fn bp (n: u32) -> Vec> { + let mut o=vec![]; + let mut p=vec![vec![]]; + while p.len() > 0 { + let s=p.pop().unwrap(); + let t: u32=s.iter().sum(); + if t==n { + o.push(s); + } else { + let mut mx=n-t; + if mx > 3 { + mx=3; + } + for i in 1..=mx { + let mut q=s.clone(); + q.push(i); + p.push(q); + } + } + } + o.reverse(); + return o; +} -- cgit From 46dff9df03baeb479313adb7038afc530c32426b Mon Sep 17 00:00:00 2001 From: chirvasitua Date: Mon, 19 Jul 2021 08:57:48 -0400 Subject: 1st commit on 122_perl --- challenge-122/stuart-little/perl/ch-1.pl | 19 +++++++++++++++++++ challenge-122/stuart-little/perl/ch-2.pl | 29 +++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100755 challenge-122/stuart-little/perl/ch-1.pl create mode 100755 challenge-122/stuart-little/perl/ch-2.pl (limited to 'challenge-122') diff --git a/challenge-122/stuart-little/perl/ch-1.pl b/challenge-122/stuart-little/perl/ch-1.pl new file mode 100755 index 0000000000..2282d8e905 --- /dev/null +++ b/challenge-122/stuart-little/perl/ch-1.pl @@ -0,0 +1,19 @@ +#!/usr/bin/env perl +use warnings; +use v5.12; + +# run