diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-05-27 11:18:14 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-05-27 11:18:14 +0100 |
| commit | a98011f63be0afbc3225e6b57e1bb2885e6f48c8 (patch) | |
| tree | 9305d3e5128d58dcb4fec471cea853124127d2d2 | |
| parent | 196de4df18793aa382f31f898bd68308acafba01 (diff) | |
| parent | b6b1d2490ba0f9325e04c66ebff178273b72e9ec (diff) | |
| download | perlweeklychallenge-club-a98011f63be0afbc3225e6b57e1bb2885e6f48c8.tar.gz perlweeklychallenge-club-a98011f63be0afbc3225e6b57e1bb2885e6f48c8.tar.bz2 perlweeklychallenge-club-a98011f63be0afbc3225e6b57e1bb2885e6f48c8.zip | |
Merge pull request #12086 from pjcs00/wk323
Week 323 - Up, down, and pay your tax
| -rw-r--r-- | challenge-323/peter-campbell-smith/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-323/peter-campbell-smith/perl/ch-1.pl | 30 | ||||
| -rwxr-xr-x | challenge-323/peter-campbell-smith/perl/ch-2.pl | 45 |
3 files changed, 76 insertions, 0 deletions
diff --git a/challenge-323/peter-campbell-smith/blog.txt b/challenge-323/peter-campbell-smith/blog.txt new file mode 100644 index 0000000000..737c174b20 --- /dev/null +++ b/challenge-323/peter-campbell-smith/blog.txt @@ -0,0 +1 @@ +http://ccgi.campbellsmiths.force9.co.uk/challenge/323 diff --git a/challenge-323/peter-campbell-smith/perl/ch-1.pl b/challenge-323/peter-campbell-smith/perl/ch-1.pl new file mode 100755 index 0000000000..2132d2fc40 --- /dev/null +++ b/challenge-323/peter-campbell-smith/perl/ch-1.pl @@ -0,0 +1,30 @@ +#!/usr/bin/perl + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +use v5.26; # The Weekly Challenge - 2025-05-26 +use utf8; # Week 323 - task 1 - Increment decrement +use warnings; # Peter Campbell Smith +binmode STDOUT, ':utf8'; +use Encode; + +increment_decrement('--x', 'x++', 'x++'); +increment_decrement('x++', '++x', 'x++'); +increment_decrement('x++', '++x', '--x', 'x--'); + +sub increment_decrement { + + my (@operations, $result); + + # initialise + @operations = @_; + + # assumw they are all negative + $result = -@operations; + + # add 2 for each positive one + $result += 2 for grep(/\+/, @operations); + + say qq[\nInput: ('] . join(q[', '], @operations) . q[')]; + say qq[Output: $result]; +} diff --git a/challenge-323/peter-campbell-smith/perl/ch-2.pl b/challenge-323/peter-campbell-smith/perl/ch-2.pl new file mode 100755 index 0000000000..cf2a7072d0 --- /dev/null +++ b/challenge-323/peter-campbell-smith/perl/ch-2.pl @@ -0,0 +1,45 @@ +#!/usr/bin/perl + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +use v5.26; # The Weekly Challenge - 2025-05-26 +use utf8; # Week 323 - task 2 - Tax amount +use warnings; # Peter Campbell Smith +binmode STDOUT, ':utf8'; +use Encode; + +tax_amount(10, [[3, 50], [7, 10], [12, 25]]); +tax_amount(2, [[1, 0], [4, 25], [5,50]]); +tax_amount(0, [[2, 50]]); + +# UK 2025/6 tax rates +tax_amount(80000, [[12570, 0], [50270, 20], [125140, 40], [1e10, 45]]); +tax_amount(150000, [[12570, 0], [50270, 20], [125140, 40], [1e10, 45]]); + +sub tax_amount { + + my ($income, $tax_rates, $tax, $band_start, $band_end, $band, $rate, $rates, $taxable, $explain); + + # initialise + ($income, $tax_rates) = @_; + $tax = 0; + $explain = ''; + + # loop over tax bands + $band_start = 0; + for $band (@$tax_rates) { + ($band_end, $rate) = @$band; + $rates .= qq[up to $band_end at $rate%, ]; + next if $income <= $band_start; + + # tax payable in this band + $taxable = $income > $band_end ? ($band_end - $band_start) : ($income - $band_start); + $tax += $taxable * $rate / 100; + $explain .= qq[$taxable at $rate%, ]; + $band_start = $band_end; + } + + say qq[\nInput: \$income = $income, \@tax = (] . substr($rates, 0, -2) . q[)]; + say qq[Output: \$tax = ] . sprintf('%.2f', $tax) . + ($tax ? ' being '. substr($explain, 0, -2) : ''); +} |
