diff options
| author | E. Choroba <choroba@matfyz.cz> | 2024-09-02 10:49:25 +0200 |
|---|---|---|
| committer | E. Choroba <choroba@matfyz.cz> | 2024-09-02 10:49:25 +0200 |
| commit | 5413258a14f8c6e4cf05c4bed70ee058e3a7bc11 (patch) | |
| tree | c0d72e707bfd83eed62bc00dae3b7c025cef36d4 | |
| parent | 0512711fccf91c731495f1dd901349cc900ec299 (diff) | |
| download | perlweeklychallenge-club-5413258a14f8c6e4cf05c4bed70ee058e3a7bc11.tar.gz perlweeklychallenge-club-5413258a14f8c6e4cf05c4bed70ee058e3a7bc11.tar.bz2 perlweeklychallenge-club-5413258a14f8c6e4cf05c4bed70ee058e3a7bc11.zip | |
Add solutions to 285: No Connection & Making Change by E. Choroba
| -rwxr-xr-x | challenge-285/e-choroba/perl/ch-1.pl | 16 | ||||
| -rwxr-xr-x | challenge-285/e-choroba/perl/ch-2.pl | 32 |
2 files changed, 48 insertions, 0 deletions
diff --git a/challenge-285/e-choroba/perl/ch-1.pl b/challenge-285/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..37cca18494 --- /dev/null +++ b/challenge-285/e-choroba/perl/ch-1.pl @@ -0,0 +1,16 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub no_connection(@routes) { + my %nodes; + @nodes{ map @$_, @routes } = (); + delete @nodes{ map $_->[0], @routes }; + return (keys %nodes)[0] +} + +use Test::More tests => 2; + +is no_connection(['B','C'], ['D','B'], ['C','A']), 'A', 'Example 1'; +is no_connection(['A','Z']), 'Z', 'Example 2'; diff --git a/challenge-285/e-choroba/perl/ch-2.pl b/challenge-285/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..2ddb9473ef --- /dev/null +++ b/challenge-285/e-choroba/perl/ch-2.pl @@ -0,0 +1,32 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +use Memoize; +memoize qw{ making_change_rec }; + +my @coins = (1, 5, 10, 25, 50); + +sub making_change($amount) { + making_change_rec($amount, 1); +} + +sub making_change_rec($amount, $min) { + return 1 if 0 == $amount; + my $ways = 0; + { no warnings 'recursion'; + $ways += making_change_rec($amount - $_, $_) + for grep $_ >= $min && $amount >= $_, @coins; + } + return $ways +} + +use Test::More tests => 3 + 1; + +is making_change(9), 2, 'Example 1'; +is making_change(15), 6, 'Example 2'; +is making_change(100), 292, 'Example 3'; + +# Memoize needed. +is making_change(1000), 801451, 'Large'; |
