Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #323 (“Increment Decrement” and “Tax Amount”)

For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle with two parts, with a new pair of tasks each Monday. You can find it here:

The Weekly Challenge

The Weekly Challenge for the week of 2025-05-26 through 2025-06-01 is #323

The tasks for challenge #323 are as follows:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 323-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

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

I'll use the "switch" statement from the "Switch" CPAN module to decide what operations to perform.

Robbie Hatley's Perl Solution to The Weekly Challenge 323-1

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 323-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: 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

To solve this, I first calculate "lwr" and "upr" bounds for the amount of the income which is in each bracket, then calculate the tax amount for each bracket by multiplying percentage by (upr-lwr), then sum for all brackets.

Robbie Hatley's Perl Solution to The Weekly Challenge 323-2

That's it for challenge 323; see you on challenge 324!

Comments

Popular posts from this blog

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #317 (Theme: “Friendly Acronyms”)

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #319 (“Word Count” and “Minimum Common”)

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #326 (“Day of Year” and “Decompressed List”)