diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-05-29 16:39:56 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-05-29 16:39:56 +0100 |
| commit | 26ca0d4bc6b2087576b7876832c4193ae91488e5 (patch) | |
| tree | 2f3bfb4bccaf0c3aaf48626889f0f5b07e5d1685 | |
| parent | 1f000a6d1875beabc5938005e4445127d5a1bb3d (diff) | |
| parent | 684908ce572c3a9b835c49f9cb63ea27e91665ba (diff) | |
| download | perlweeklychallenge-club-26ca0d4bc6b2087576b7876832c4193ae91488e5.tar.gz perlweeklychallenge-club-26ca0d4bc6b2087576b7876832c4193ae91488e5.tar.bz2 perlweeklychallenge-club-26ca0d4bc6b2087576b7876832c4193ae91488e5.zip | |
Merge pull request #12098 from wanderdoc/master
PWC 323 (wanderdoc)
| -rw-r--r-- | challenge-323/wanderdoc/perl/ch-1.pl | 66 | ||||
| -rw-r--r-- | challenge-323/wanderdoc/perl/ch-2.pl | 71 |
2 files changed, 137 insertions, 0 deletions
diff --git a/challenge-323/wanderdoc/perl/ch-1.pl b/challenge-323/wanderdoc/perl/ch-1.pl new file mode 100644 index 0000000000..b3e829cbb0 --- /dev/null +++ b/challenge-323/wanderdoc/perl/ch-1.pl @@ -0,0 +1,66 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +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 + + +Example 1 + +Input: @operations = ("--x", "x++", "x++") +Output: 1 + +Operation "--x" => 0 - 1 => -1 +Operation "x++" => -1 + 1 => 0 +Operation "x++" => 0 + 1 => 1 + + +Example 2 + +Input: @operations = ("x++", "++x", "x++") +Output: 3 + + +Example 3 + +Input: @operations = ("x++", "++x", "--x", "x--") +Output: 0 + +Operation "x++" => 0 + 1 => 1 +Operation "++x" => 1 + 1 => 2 +Operation "--x" => 2 - 1 => 1 +Operation "x--" => 1 - 1 => 0 + +=cut + +use Test2::V0 -no_srand => 1; + +is(task1("--x", "x++", "x++"), 1, 'Example 1'); +is(task1("x++", "++x", "x++"), 3, 'Example 2'); +is(task1("x++", "++x", "--x", "x--"), 0, 'Example 3'); +done_testing(); + +sub task1 +{ + my @arr = @_; + my $value = 0; + for my $operator ( @arr ) + { + if ($operator =~ /\+/) + { + $value++; + } + elsif ( $operator =~ /\-/ ) + { + $value--; + } + } + return $value; +} diff --git a/challenge-323/wanderdoc/perl/ch-2.pl b/challenge-323/wanderdoc/perl/ch-2.pl new file mode 100644 index 0000000000..6c604aa64a --- /dev/null +++ b/challenge-323/wanderdoc/perl/ch-2.pl @@ -0,0 +1,71 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +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: 2.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 + + +Example 2 + +Input: $income = 2, @tax = ([1, 0], [4, 25], [5,50]) +Output: 0.25 + +Total Tax => (1 * 0/100) + (1 * 25/100) + => 0 + 0.25 + => 0.25 + + +Example 3 + +Input: $income = 0, @tax = ([2, 50]) +Output: 0 +=cut + + +use Test2::V0 -no_srand => 1; +is(tax_amount(10, [[3, 50], [7, 10], [12,25]]), 2.65, 'Example 1'); +is(tax_amount(2, [[1, 0], [4, 25], [5,50]]), 0.25, 'Example 2'); +is(tax_amount(0, [[2, 50]]), 0, 'Example 3'); +done_testing(); + +sub tax_amount +{ + my ($income, $tax_aref) = @_; + my $tax_amount = 0; + my $marker = 0; + for my $slot ( @$tax_aref ) + { + my $up_to = $slot->[0]; + my $tax = $slot->[1]/100; + if ( $marker < $up_to ) + { + if ( $income >= $up_to ) + { + $tax_amount += ($up_to - $marker) * $tax; + $marker = $up_to; + } + elsif ( $income < $up_to ) + { + $tax_amount += ($income - $marker) * $tax; + last; + } + } + } + return $tax_amount; +} |
