aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNiels van Dijke <perlboy@cpan.org>2025-05-26 12:28:47 +0000
committerNiels van Dijke <perlboy@cpan.org>2025-05-26 12:28:47 +0000
commit3142e2468e552cd1efa2511150a8a739bd41e7ed (patch)
tree351cfd565fbfbd56720b940b2dd872c6ccfdf5b6
parent0729d1308bfe2e7d4fc1ea6f41b40356645d4f72 (diff)
downloadperlweeklychallenge-club-3142e2468e552cd1efa2511150a8a739bd41e7ed.tar.gz
perlweeklychallenge-club-3142e2468e552cd1efa2511150a8a739bd41e7ed.tar.bz2
perlweeklychallenge-club-3142e2468e552cd1efa2511150a8a739bd41e7ed.zip
w323 - Task 1 & 2
-rwxr-xr-xchallenge-323/perlboy1967/perl/ch1.pl45
-rwxr-xr-xchallenge-323/perlboy1967/perl/ch2.pl79
2 files changed, 124 insertions, 0 deletions
diff --git a/challenge-323/perlboy1967/perl/ch1.pl b/challenge-323/perlboy1967/perl/ch1.pl
new file mode 100755
index 0000000000..a9097270d9
--- /dev/null
+++ b/challenge-323/perlboy1967/perl/ch1.pl
@@ -0,0 +1,45 @@
+#!/bin/perl
+
+=pod
+
+The Weekly Challenge - 323
+L<https://theweeklychallenge.org/blog/perl-weekly-challenge-323#TASK1>
+
+Author: Niels 'PerlBoy' van Dijke
+
+Task 1: Increment Decrement
+Submitted by: Mohammad Sajid Anwar
+
+You are given a list of operations.
+
+Write a script to return the final value after performing the given
+operations in order. The initial value is always 0.
+
+|| Possible Operations:
+|| ++x or x++: increment by 1
+|| --x or x--: decrement by 1
+
+=cut
+
+use v5.32;
+use common::sense;
+use feature qw(signatures);
+use Test2::V0 qw(-no_srand);
+no warnings qw(experimental::signatures);
+
+use List::Util qw(sum);
+
+sub incDec (@str) {
+ state $op = {
+ 'x++' => 1, 'x--' => -1,
+ '++x' => 1, '--x' => -1,
+ };
+ sum( map { $op->{$_} // 0 } @str);
+}
+
+is(incDec(qw{--x x++ x++}),1,'Example 1');
+is(incDec(qw{x++ ++x x++}),3,'Example 2');
+is(incDec(qw{x++ ++x --x x--}),0,'Example 2');
+is(incDec(qw{x++ bogus op}),1,'Own example');
+
+done_testing;
diff --git a/challenge-323/perlboy1967/perl/ch2.pl b/challenge-323/perlboy1967/perl/ch2.pl
new file mode 100755
index 0000000000..25fa9b9d2f
--- /dev/null
+++ b/challenge-323/perlboy1967/perl/ch2.pl
@@ -0,0 +1,79 @@
+#!/bin/perl
+
+=pod
+
+The Weekly Challenge - 323
+L<https://theweeklychallenge.org/blog/perl-weekly-challenge-323#TASK2>
+
+Author: Niels 'PerlBoy' van Dijke
+
+Task 2: Tax Amount
+Submitted by: Mohammad Sajid Anwar
+
+You are given an income amount and tax brackets.
+
+Write a script to calculate the total tax amount.
+
+|| Example 1
+||
+|| Input: $income = 10, @tax = ([3, 50], [7, 10], [12,25])
+|| Output: 1.65
+||
+|| 1st tax bracket upto 3, tax is 50%.
+|| 2nd tax bracket upto 7, tax is 10%.
+|| 3rd tax bracket upto 12, tax is 25%.
+||
+|| Total Tax => (3 * 50/100) + (4 * 10/100) + (3 * 25/100)
+|| => 1.50 + 0.40 + 0.75
+|| => 2.65
+
+=cut
+
+use v5.32;
+use common::sense;
+use feature qw(signatures);
+use Test2::V0 qw(-no_srand);
+no warnings qw(experimental::signatures);
+
+sub taxAmount ($income,@taxBrackets) {
+ @taxBrackets = map { [$$_[0],$$_[1]/100] }
+ sort { $$a[0] <=> $$b[0] } @taxBrackets;
+
+ # Get last bracket via [0,x]
+ my @lastBracket = $taxBrackets[0][0] == 0 ? @{shift @taxBrackets} : (0,0);
+ $lastBracket[0] = $taxBrackets[-1][0] if $lastBracket[1];
+
+ my ($tax,$lo) = (0,0);
+ while (@taxBrackets) {
+ my ($hi,$bracket) = @{shift @taxBrackets};
+ $hi = $income if $income < $hi;
+ $tax += ($hi - $lo) * $bracket;
+ last if $hi > $income;
+ $lo = $hi;
+ }
+
+ # Process last bracket (if needed)
+ $tax += ($income - $lastBracket[0]) * $lastBracket[1]
+ if ($lastBracket[1] and $income > $lastBracket[0]);
+
+ return int($tax * 100 + 0.5)/100;
+}
+
+is(taxAmount(10,@{[[3,50],[7,10],[12,25]]}),2.65,'Example 1');
+is(taxAmount(2,@{[[1,0],[4,25],[5,50]]}),0.25,'Example 2');
+is(taxAmount(0,@{[[2,50]]}),0,'Example 3');
+
+is(taxAmount(50,@{[[100,10],[0,50]]}),5,'Own example with high tax bracket');
+
+is(taxAmount(200_000.00,@{[[15820,25],[27920,40],[48320,45],[0,50]]}),
+ 93_815.00,'United States (USD)');
+is(taxAmount(200_000.00,@{[[38441,35.82],[76817,37.48],[0,49.50]]}),
+ 89_128.48,'The Netherlands (Euro)');
+is(taxAmount(200_000.00,@{[[12570,0],[50270,20],[125140,40],[0,45]]}),
+ 71_175.00,'United Kingdom (GBP)');
+is(taxAmount(200_000.00,@{[[11497,0],[29315,11],[83823,30],[180294,41],[0,45]]}),
+ 66_733.19,'France (Euro)');
+is(taxAmount(200_000.00,@{[[11600,10],[47150,12],[100525,22],[191950,24]]}),
+ 39_110.50,'United States (USD)');
+
+done_testing;