From c051bbb87eef23f00fd0d9d8dbabb60b3de0dcb8 Mon Sep 17 00:00:00 2001 From: Dave Jacoby Date: Thu, 29 May 2025 16:49:15 -0400 Subject: DAJ 323 blogged --- challenge-323/dave-jacoby/blog.txt | 1 + challenge-323/dave-jacoby/perl/ch-1.pl | 32 +++++++++++++++++++++++++ challenge-323/dave-jacoby/perl/ch-2.pl | 44 ++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 challenge-323/dave-jacoby/blog.txt create mode 100644 challenge-323/dave-jacoby/perl/ch-1.pl create mode 100644 challenge-323/dave-jacoby/perl/ch-2.pl 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; +} -- cgit