diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-05-27 11:31:05 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-05-27 11:31:05 +0100 |
| commit | bfc7812f96ad626d6ae7e797844ce8217d94c765 (patch) | |
| tree | 76de80e0fe9c30dc97089789707d867ff582608c | |
| parent | e57457f86b7bcedfe36c1b96be4c5c8e59e6e67e (diff) | |
| parent | f4ba67a9e9a817bd5e9c597c5c4ccfc59a2dc43b (diff) | |
| download | perlweeklychallenge-club-bfc7812f96ad626d6ae7e797844ce8217d94c765.tar.gz perlweeklychallenge-club-bfc7812f96ad626d6ae7e797844ce8217d94c765.tar.bz2 perlweeklychallenge-club-bfc7812f96ad626d6ae7e797844ce8217d94c765.zip | |
Merge pull request #12093 from wlmb/challenges
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"; |
