diff options
| author | Luis Mochan <mochan@fis.unam.mx> | 2024-09-02 12:51:14 -0600 |
|---|---|---|
| committer | Luis Mochan <mochan@fis.unam.mx> | 2024-09-02 12:51:14 -0600 |
| commit | 889d54e9da3132ac4eb3eea9bdbb36b5fe3e423b (patch) | |
| tree | 64007da50cf9b56397e7241bfbdac2f584d5740a | |
| parent | a92117f754a875572d11900c871014386b7ade27 (diff) | |
| download | perlweeklychallenge-club-889d54e9da3132ac4eb3eea9bdbb36b5fe3e423b.tar.gz perlweeklychallenge-club-889d54e9da3132ac4eb3eea9bdbb36b5fe3e423b.tar.bz2 perlweeklychallenge-club-889d54e9da3132ac4eb3eea9bdbb36b5fe3e423b.zip | |
Solve PWC 285
| -rw-r--r-- | challenge-285/wlmb/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-285/wlmb/perl/ch-1.pl | 18 | ||||
| -rwxr-xr-x | challenge-285/wlmb/perl/ch-2.pl | 26 |
3 files changed, 45 insertions, 0 deletions
diff --git a/challenge-285/wlmb/blog.txt b/challenge-285/wlmb/blog.txt new file mode 100644 index 0000000000..951aa5de7d --- /dev/null +++ b/challenge-285/wlmb/blog.txt @@ -0,0 +1 @@ +https://wlmb.github.io/2024/09/02/PWC285/ diff --git a/challenge-285/wlmb/perl/ch-1.pl b/challenge-285/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..f1421e4b37 --- /dev/null +++ b/challenge-285/wlmb/perl/ch-1.pl @@ -0,0 +1,18 @@ +#!/usr/bin/env perl +# Perl weekly challenge 285 +# Task 1: No Connection +# +# See https://wlmb.github.io/2024/09/02/PWC285/#task-1-no-connection +use v5.36; +use experimental qw(for_list); +die <<~"FIN" unless @ARGV and @ARGV%2==0; + Usage: $0 I1 O1 I2 O2... + where Ii is the incoming and Oi the outgoing node + of the i-th edge. + FIN +my (%in, %out); +for my($in,$out)(@ARGV){ + ++$in{$in} if $in ne $out; + ++$out{$out}; +} +say "@ARGV -> ", join " ", grep{!$in{$_}}keys %out; diff --git a/challenge-285/wlmb/perl/ch-2.pl b/challenge-285/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..9e62d1d808 --- /dev/null +++ b/challenge-285/wlmb/perl/ch-2.pl @@ -0,0 +1,26 @@ +#!/usr/bin/env perl +# Perl weekly challenge 285 +# Task 2: Making Change +# +# See https://wlmb.github.io/2024/09/02/PWC285/#task-2-making-change +use v5.36; +use Memoize; +die <<~"FIN" unless @ARGV; + Usage: $0 A1 A2... + to find in how many ways can the amounts Ai can be + formed with coins (pennies, nickels, dimes, quartes + and half-dollars) + FIN +memoize qw(number_of_ways); +my @values=(1,5,10,25,50); # sorted +my $large=1+$values[-1]; # larger than largest coin +for(@ARGV){ + say "$_ -> ",number_of_ways($_+$large, $large) +} +sub number_of_ways($amount,$first_coin){ + return 0 if $first_coin>$amount; + return 1 if $first_coin==$amount; + my $total; + $total+=number_of_ways($amount-$first_coin, $_)for grep{$_<=$first_coin}@values; + return $total; +} |
