diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-09-07 15:36:07 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-09-07 15:36:07 +0100 |
| commit | daa266700d79b91fa61a1376c0fc03904b0384a1 (patch) | |
| tree | be8765c915a308d13f064d2956e651d48ec3204f /challenge-285 | |
| parent | e6e1e8e4ed687ceef89bdca83ac5c53b2885116e (diff) | |
| parent | f5488b2073669c6a06bac1555efbf4a79009bae5 (diff) | |
| download | perlweeklychallenge-club-daa266700d79b91fa61a1376c0fc03904b0384a1.tar.gz perlweeklychallenge-club-daa266700d79b91fa61a1376c0fc03904b0384a1.tar.bz2 perlweeklychallenge-club-daa266700d79b91fa61a1376c0fc03904b0384a1.zip | |
Merge pull request #10778 from jo-37/contrib
Solutions to challenge 285
Diffstat (limited to 'challenge-285')
| -rw-r--r-- | challenge-285/jo-37/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-285/jo-37/perl/ch-1.pl | 62 | ||||
| -rwxr-xr-x | challenge-285/jo-37/perl/ch-2.pl | 105 |
3 files changed, 168 insertions, 0 deletions
diff --git a/challenge-285/jo-37/blog.txt b/challenge-285/jo-37/blog.txt new file mode 100644 index 0000000000..23dc577f74 --- /dev/null +++ b/challenge-285/jo-37/blog.txt @@ -0,0 +1 @@ +https://github.sommrey.de/the-bears-den/2024/09/06/ch-285.html diff --git a/challenge-285/jo-37/perl/ch-1.pl b/challenge-285/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..9a7d0620f4 --- /dev/null +++ b/challenge-285/jo-37/perl/ch-1.pl @@ -0,0 +1,62 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0; +use Graph::Directed; + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless @ARGV; +usage: $0 [-examples] [-tests] [ROUTE...] + +-examples + run the examples from the challenge + +-tests + run some tests + +ROUTE... + routes given as origin-destination, e.g A-B + +EOS + + +### Input and Output + +say for no_connection(map [split qr/-/, $_], @ARGV); + + +### Implementation +# +# For details see: +# https://github.sommrey.de/the-bears-den/2024/09/06/ch-285.html#task-1 + + +sub no_connection { + Graph::Directed->new(edges => \@_)->sink_vertices; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is [no_connection(["B","C"], ["D","B"], ["C","A"])], ["A"], + "example 1"; + is [no_connection(["A","Z"])], ["Z"], + "example 2"; + } + + SKIP: { + skip "tests" unless $tests; + + is [no_connection(["A", "B"], ["B", "C"], ["C", "A"])], [], 'cycle'; + } + + done_testing; + exit; +} diff --git a/challenge-285/jo-37/perl/ch-2.pl b/challenge-285/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..f6020c5079 --- /dev/null +++ b/challenge-285/jo-37/perl/ch-2.pl @@ -0,0 +1,105 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0; +use experimental 'signatures'; + +our ($tests, $examples, $verbose); + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless @ARGV; +usage: $0 [-examples] [-tests] [-verbose] [AMOUNT] + +-examples + run the examples from the challenge + +-tests + run some tests + +-verbose + print all individual combinations + +AMOUNT + amount + +EOS + + +### Input and Output + +if ($verbose) { + my $n; + say "${\(++$n)}: ", fmt(@$_) for make_change(shift); +} else { + say scalar make_change(shift); +} + + + +### Implementation +# +# For details see: +# https://github.sommrey.de/the-bears-den/2024/09/06/ch-285.html#task-2 + +sub make_change ($amount, $coin = 4) { + state $coins = [1, 5, 10, 25, 50]; + if ($coin == 0) { + if ($amount % $coins->[0] == 0) { + return [$amount / $coins->[0]]; + } else { + return (); + } + } + my @comb; + for (my ($cnt, $val) = (0, 0); + $val <= $amount; + $cnt++, $val += $coins->[$coin]) { + if ($val == $amount) { + push @comb, [(0) x $coin, $cnt]; + } else { + push @comb, [@$_, $cnt] + for make_change($amount - $val, $coin - 1); + } + } + + @comb; +} + +sub fmt { + state $name = [qw(P N D Q HD)]; + my @coins; + for (my $i = $#_; $i >= 0; $i--) { + my $c = $_[$i]; + push @coins, $c x ($c != 1) . $name->[$i] if $c; + } + + join ' + ', @coins; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is [make_change(9)], [[9, 0, 0, 0, 0], [4, 1, 0, 0, 0]], 'example 1'; + is [make_change(15)], [ + [15, 0, 0, 0, 0], + [10, 1, 0, 0, 0], + [5, 2, 0, 0, 0], + [0, 3, 0, 0, 0], + [5, 0, 1, 0, 0], + [0, 1, 1, 0, 0], + ], 'example 2'; + is make_change(100), 292, 'example 3'; + } + + SKIP: { + skip "tests" unless $tests; + } + + done_testing; + exit; +} |
