diff options
| author | Luis Mochan <mochan@fis.unam.mx> | 2025-05-26 13:23:55 -0600 |
|---|---|---|
| committer | Luis Mochan <mochan@fis.unam.mx> | 2025-05-26 13:23:55 -0600 |
| commit | f4ba67a9e9a817bd5e9c597c5c4ccfc59a2dc43b (patch) | |
| tree | 48e86d885b2980ca9be2ec2fc52322885ae4faaf | |
| parent | 0729d1308bfe2e7d4fc1ea6f41b40356645d4f72 (diff) | |
| download | perlweeklychallenge-club-f4ba67a9e9a817bd5e9c597c5c4ccfc59a2dc43b.tar.gz perlweeklychallenge-club-f4ba67a9e9a817bd5e9c597c5c4ccfc59a2dc43b.tar.bz2 perlweeklychallenge-club-f4ba67a9e9a817bd5e9c597c5c4ccfc59a2dc43b.zip | |
Solve PWC323
| -rw-r--r-- | challenge-323/wlmb/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-323/wlmb/perl/ch-1.pl | 23 | ||||
| -rwxr-xr-x | challenge-323/wlmb/perl/ch-2.pl | 27 |
3 files changed, 51 insertions, 0 deletions
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"; |
