diff options
| -rw-r--r-- | challenge-285/atschneid/README.md | 10 | ||||
| -rw-r--r-- | challenge-285/atschneid/perl/ch-1.pl | 38 | ||||
| -rw-r--r-- | challenge-285/atschneid/perl/ch-2.pl | 43 |
3 files changed, 85 insertions, 6 deletions
diff --git a/challenge-285/atschneid/README.md b/challenge-285/atschneid/README.md index f2503cad2f..56bf100b0a 100644 --- a/challenge-285/atschneid/README.md +++ b/challenge-285/atschneid/README.md @@ -1,11 +1,9 @@ -# PWC 284 +# PWC 285 -**Challenge 284 solutions by Andrew Schneider** +**Challenge 285 solutions by Andrew Schneider** -[PWC 284](https://theweeklychallenge.org/blog/perl-weekly-challenge-284/) +[PWC 285](https://theweeklychallenge.org/blog/perl-weekly-challenge-285/) -I haven't had the motivation to say much here lately, but I also feel like I haven't had much to say. I suppose I've been busy with other stuff, and haven't been doing my usual programming language roulette, although, really, what's more important than the PWC?! - -I did see there's some tight competition between Ruby and Rust, hence my Rust contribution in support of my pick. I do have a soft spot in my heart for Ruby, so nothing personal of course. +Dyanmic programming for challenge 2. Whoa! Thanks for the challenges! diff --git a/challenge-285/atschneid/perl/ch-1.pl b/challenge-285/atschneid/perl/ch-1.pl new file mode 100644 index 0000000000..ca3352ab24 --- /dev/null +++ b/challenge-285/atschneid/perl/ch-1.pl @@ -0,0 +1,38 @@ +use warnings; +use strict; + +use v5.38; + +sub no_connection( @routes ) { + my %out_count; + for (@routes) { + my ( $from, $to ) = @$_; + $out_count{ $from } += 1; + $out_count{ $to } += 0; + } + + return grep { !$out_count{ $_ } } keys %out_count; +} + +my @inputs = ( + [ + ["B", "C"], + ["D", "B"], + ["C", "A"], + ], + [ + ["B", "C"], + ["D", "B"], + ["C", "A"], + ["C", "Z"], + ], + [ + ["A", "Z"], + ] + ); +for my $input (@inputs) { + for (@$input) { + printf '[ ' . ( join ', ', @$_ ) . ' ] '; + } + say ' => ' . ( join ', ', no_connection( @$input ) ); +} diff --git a/challenge-285/atschneid/perl/ch-2.pl b/challenge-285/atschneid/perl/ch-2.pl new file mode 100644 index 0000000000..bec2913e28 --- /dev/null +++ b/challenge-285/atschneid/perl/ch-2.pl @@ -0,0 +1,43 @@ +use warnings; +use strict; + +use List::Util qw( max ); + +use v5.38; + +sub make_change_v1( $amount ) { + my @dynamic_arr = (1) x ( $amount + 1 ); + my @coins = (5, 10, 25, 50); + for my $coin_amount (@coins) { + for my $idx ($coin_amount..$amount) { + $dynamic_arr[ $idx ] += $dynamic_arr[ $idx - $coin_amount ]; + } + say "coin: $coin_amount :: " . ( join ', ', @dynamic_arr ); + } + return $dynamic_arr[ -1 ]; +} + +sub make_change_v2( $amount ) { + $amount = int( $amount / 5 ); + return 1 if $amount == 0; + + my @dynamic_arr = (1) x ( $amount + 1 ); + my @coins = (1, 2, 5, 10); + for my $coin_amount (@coins) { + for my $idx ($coin_amount..$amount) { + $dynamic_arr[ $idx ] += $dynamic_arr[ $idx - $coin_amount ]; + } + say "coin: $coin_amount :: " . ( join ', ', @dynamic_arr ); + } + return $dynamic_arr[ -1 ]; +} + +my @inputs = ( + 9, 15, 100 + ); +for my $input (@inputs) { + say $input . ' => ' . make_change_v1( $input ); +} +for my $input (@inputs) { + say $input . ' => ' . make_change_v2( $input ); +} |
