diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2025-05-29 16:49:15 -0400 |
|---|---|---|
| committer | Dave Jacoby <jacoby.david@gmail.com> | 2025-05-29 16:49:15 -0400 |
| commit | c051bbb87eef23f00fd0d9d8dbabb60b3de0dcb8 (patch) | |
| tree | 6bbef7fc0f79796a7dc7698abd41754752205e2b | |
| parent | 072ce0c8580aec64bae63f3da50d19c65b3ef89d (diff) | |
| download | perlweeklychallenge-club-c051bbb87eef23f00fd0d9d8dbabb60b3de0dcb8.tar.gz perlweeklychallenge-club-c051bbb87eef23f00fd0d9d8dbabb60b3de0dcb8.tar.bz2 perlweeklychallenge-club-c051bbb87eef23f00fd0d9d8dbabb60b3de0dcb8.zip | |
DAJ 323 blogged
| -rw-r--r-- | challenge-323/dave-jacoby/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-323/dave-jacoby/perl/ch-1.pl | 32 | ||||
| -rw-r--r-- | challenge-323/dave-jacoby/perl/ch-2.pl | 44 |
3 files changed, 77 insertions, 0 deletions
diff --git a/challenge-323/dave-jacoby/blog.txt b/challenge-323/dave-jacoby/blog.txt new file mode 100644 index 0000000000..bafd6f74bd --- /dev/null +++ b/challenge-323/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby-lpwk.onrender.com/2025/05/29/orders-matter-and-order-matters-weekly-challenge-323.html diff --git a/challenge-323/dave-jacoby/perl/ch-1.pl b/challenge-323/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..2efdfbe718 --- /dev/null +++ b/challenge-323/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,32 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say state postderef signatures }; + +use List::Util qw{ sum0 }; + +my @examples = ( + + [ "--x", "x++", "x++" ], + [ "x++", "++x", "x++" ], + [ "x++", "++x", "--x", "x--" ], +); + +for my $example (@examples) { + my $operations = join ', ', map { qq{"$_"} } $example->@*; + my $output = increment_decrement($example->@*); + say <<"END"; + Input: \@operations = ($operations) + Output: $output +END +} + +sub increment_decrement (@operations) { + my $value = 0; + for my $op ( @operations ) { + $value ++ if $op =~ /\+\+/mx; + $value -- if $op =~ /\-\-/mx; + } + return $value; +} diff --git a/challenge-323/dave-jacoby/perl/ch-2.pl b/challenge-323/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..c910c5e178 --- /dev/null +++ b/challenge-323/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,44 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say state postderef signatures }; +use List::Util qw{ uniq }; + +my @examples = ( + + { income => 10, tax => [ [ 3, 50 ], [ 7, 10 ], [ 12, 25 ] ] }, + { income => 2, tax => [ [ 1, 0 ], [ 4, 25 ], [ 5, 50 ] ] }, + { income => 0, tax => [ [ 2, 50 ] ] }, +); + +for my $example (@examples) { + my $income = $example->{income}; + my @tax = $example->{tax}->@*; + my $tax = join ', ', map { qq{[ $_ ]} } + map { join ', ', $_->@* } @tax; + my $output = tax_amount($example); + say <<"END"; + Input: \@income = $income, + \@tax = ($tax) + Output: $output +END +} + +sub tax_amount($example) { + my $total = 0; + my $income = $example->{income}; + my @tax = $example->{tax}->@*; + for my $i ( 0 .. $#tax ) { + my $bracket = $tax[$i]; + my ( $upto, $rate ) = $bracket->@*; + my $prev = 0; + $prev = $i - 1 >= 0 ? $tax[ $i - 1 ][0] : 0; + my $subset = 0; + if ( $income >= $upto ) { $subset = $upto - $prev; } + elsif ( $income >= $prev ) { $subset = $income - $prev; } + my $subtax = $subset * ( $rate / 100 ); + $total += $subtax; + } + return sprintf '%.02f', $total; +} |
