diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-09-02 13:01:02 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-09-02 13:01:02 +0100 |
| commit | 9ed7b68c35504643a459bdf688ef3268b42a6edd (patch) | |
| tree | 863b4f516fec3e534a27becb25138006d613ffb6 | |
| parent | 0a433e2376093c2284457d3507c1d37e37c4973c (diff) | |
| parent | 5413258a14f8c6e4cf05c4bed70ee058e3a7bc11 (diff) | |
| download | perlweeklychallenge-club-9ed7b68c35504643a459bdf688ef3268b42a6edd.tar.gz perlweeklychallenge-club-9ed7b68c35504643a459bdf688ef3268b42a6edd.tar.bz2 perlweeklychallenge-club-9ed7b68c35504643a459bdf688ef3268b42a6edd.zip | |
Merge pull request #10755 from choroba/ech285
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'; |
