From 3943ea3376e7c71bda8aa99c41384f76af2254b9 Mon Sep 17 00:00:00 2001 From: wanderdoc Date: Wed, 28 May 2025 17:57:55 +0200 Subject: Create ch-1.pl --- challenge-323/wanderdoc/perl/ch-1.pl | 66 ++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 challenge-323/wanderdoc/perl/ch-1.pl 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; +} -- cgit From 684908ce572c3a9b835c49f9cb63ea27e91665ba Mon Sep 17 00:00:00 2001 From: wanderdoc Date: Wed, 28 May 2025 17:58:25 +0200 Subject: Create ch-2.pl --- challenge-323/wanderdoc/perl/ch-2.pl | 71 ++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 challenge-323/wanderdoc/perl/ch-2.pl 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; +} -- cgit