From f4ba67a9e9a817bd5e9c597c5c4ccfc59a2dc43b Mon Sep 17 00:00:00 2001 From: Luis Mochan Date: Mon, 26 May 2025 13:23:55 -0600 Subject: Solve PWC323 --- challenge-323/wlmb/blog.txt | 1 + challenge-323/wlmb/perl/ch-1.pl | 23 +++++++++++++++++++++++ challenge-323/wlmb/perl/ch-2.pl | 27 +++++++++++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 challenge-323/wlmb/blog.txt create mode 100755 challenge-323/wlmb/perl/ch-1.pl create mode 100755 challenge-323/wlmb/perl/ch-2.pl diff --git a/challenge-323/wlmb/blog.txt b/challenge-323/wlmb/blog.txt new file mode 100644 index 0000000000..827d036ff3 --- /dev/null +++ b/challenge-323/wlmb/blog.txt @@ -0,0 +1 @@ +https://wlmb.github.io/2025/05/26/PWC323/ diff --git a/challenge-323/wlmb/perl/ch-1.pl b/challenge-323/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..c38a84cf8d --- /dev/null +++ b/challenge-323/wlmb/perl/ch-1.pl @@ -0,0 +1,23 @@ +#!/usr/bin/env perl +# Perl weekly challenge 323 +# Task 1: +# +# See https://wlmb.github.io/2025/05/26/PWC323/#task-1- +use v5.36; +die <<~"FIN" unless @ARGV; + Usage: $0 E1 E2... + to apply increment and decrement expressions ~En~ + of the form ++x, x++ --x, x-- + where x is the name of a variable + FIN +my %vars; +for(@ARGV){ + $vars{$1}//=0, $vars{$1}++, next if /^\+\+([[:alpha:]]+)$/; + $vars{$1}//=0, $vars{$1}--, next if /^--([[:alpha:]]+)$/; + $vars{$1}//=0, $vars{$1}++, next if /^([[:alpha:]]+)\+\+$/; + $vars{$1}//=0, $vars{$1}--, next if /^([[:alpha:]]+)--$/; + die "Wrong format: $_"; +} +print "@ARGV -> "; +print "$_=$vars{$_} " for keys %vars; +print "\n"; diff --git a/challenge-323/wlmb/perl/ch-2.pl b/challenge-323/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..2ee8ffd068 --- /dev/null +++ b/challenge-323/wlmb/perl/ch-2.pl @@ -0,0 +1,27 @@ +#!/usr/bin/env perl +# Perl weekly challenge 323 +# Task 2: Tax Amount +# +# See https://wlmb.github.io/2025/05/26/PWC323/#task-2-tax-amount +use v5.36; +use List::Util qw(min); +die <<~"FIN" unless @ARGV && @ARGV%2==1; + Usage: $0 I M1 P1 M2 P2... + to compute taxes corresponding to income I where Mn is the maximum + income in bracket n which is taxed with a percentage Pn + FIN +my $income = shift; +my $low = 0; # lowest bound of current bracket +my @tax; +my $result=0; +my $remaining = $income; +for my ($high, $percentage)(@ARGV){ + push @tax, [$high, $percentage]; + my $range=$high-$low; + $low=$high; # update for next iteration + next if $remaining < 0; + my $taxable = min($remaining, $range); + $result += $taxable*$percentage/100; + $remaining -= $taxable; +} +say "income=$income, tax=", join(", ", map{"[@$_]"} @tax)," -> $result"; -- cgit