aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Green <mail@simon.green>2025-06-01 22:54:52 +1000
committerSimon Green <mail@simon.green>2025-06-01 22:54:52 +1000
commit08dd8bb4e98654546426dfa427943a5670912745 (patch)
tree51b7bba76a8d4afc12863ac8600f7b3389ea16ef
parent0729d1308bfe2e7d4fc1ea6f41b40356645d4f72 (diff)
downloadperlweeklychallenge-club-08dd8bb4e98654546426dfa427943a5670912745.tar.gz
perlweeklychallenge-club-08dd8bb4e98654546426dfa427943a5670912745.tar.bz2
perlweeklychallenge-club-08dd8bb4e98654546426dfa427943a5670912745.zip
sgreen solutions to challenge 323
-rw-r--r--challenge-323/sgreen/README.md4
-rw-r--r--challenge-323/sgreen/blog.txt1
-rwxr-xr-xchallenge-323/sgreen/perl/ch-1.pl25
-rwxr-xr-xchallenge-323/sgreen/perl/ch-2.pl48
-rwxr-xr-xchallenge-323/sgreen/python/ch-1.py33
-rwxr-xr-xchallenge-323/sgreen/python/ch-2.py50
-rwxr-xr-xchallenge-323/sgreen/python/test.py22
7 files changed, 181 insertions, 2 deletions
diff --git a/challenge-323/sgreen/README.md b/challenge-323/sgreen/README.md
index 34165c0406..2e2ab9bc80 100644
--- a/challenge-323/sgreen/README.md
+++ b/challenge-323/sgreen/README.md
@@ -1,3 +1,3 @@
-# The Weekly Challenge 322
+# The Weekly Challenge 323
-Blog: [Strings and Arrays](https://dev.to/simongreennet/weekly-challenge-strings-and-arrays-4a8h)
+Blog: [Counting the tax](https://dev.to/simongreennet/weekly-challenge-counting-the-tax-53fh)
diff --git a/challenge-323/sgreen/blog.txt b/challenge-323/sgreen/blog.txt
new file mode 100644
index 0000000000..8f8d54a7af
--- /dev/null
+++ b/challenge-323/sgreen/blog.txt
@@ -0,0 +1 @@
+https://dev.to/simongreennet/weekly-challenge-counting-the-tax-53fh \ No newline at end of file
diff --git a/challenge-323/sgreen/perl/ch-1.pl b/challenge-323/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..e6fe905195
--- /dev/null
+++ b/challenge-323/sgreen/perl/ch-1.pl
@@ -0,0 +1,25 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+sub main (@operations) {
+ my $counter = 0;
+ foreach my $operation (@operations) {
+ if ($operation eq "x++" or $operation eq "++x") {
+ $counter++;
+ }
+ elsif ($operation eq "x--" or $operation eq "--x") {
+ $counter--;
+ }
+ else {
+ die "Unknown operation: $operation}";
+ }
+ };
+
+ say $counter;
+}
+
+main(@ARGV); \ No newline at end of file
diff --git a/challenge-323/sgreen/perl/ch-2.pl b/challenge-323/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..484e5b4168
--- /dev/null
+++ b/challenge-323/sgreen/perl/ch-2.pl
@@ -0,0 +1,48 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+use List::Util 'min';
+
+sub main (@numbers) {
+ # Check we have an odd number of arguments
+ die "Please provide an odd number of arguments" if @numbers % 2 == 0;
+
+ my $income = shift(@numbers);
+ my @tax_brackets = ();
+ while ( scalar(@numbers) ) {
+ push @tax_brackets, [ shift(@numbers), shift(@numbers) ];
+ }
+
+ # Check income does not exceed the last tax bracket
+ if ( $income > $tax_brackets[-1][0] ) {
+ die "Income exceeds the last tax bracket limit.\n";
+ }
+
+ # Add a zero tax bracket
+ unshift( @tax_brackets, [ 0, 0 ] );
+
+ # Initialize total tax amount
+ my $total_tax = 0;
+
+ # Iterate through tax brackets
+ foreach my $idx ( 1 .. $#tax_brackets ) {
+ my ( $tax_threshold, $tax_rate ) = @{ $tax_brackets[$idx] };
+ $total_tax +=
+ ( min( $income, $tax_threshold ) - $tax_brackets[ $idx - 1 ][0] ) *
+ ( $tax_rate / 100 );
+
+ # We don't need to look at further brackets if income is less than
+ # or equal to the current threshold
+ if ( $income <= $tax_threshold ) {
+ last;
+ }
+ }
+
+ say sprintf '%0.2f', $total_tax;
+}
+
+main(@ARGV);
diff --git a/challenge-323/sgreen/python/ch-1.py b/challenge-323/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..1932147849
--- /dev/null
+++ b/challenge-323/sgreen/python/ch-1.py
@@ -0,0 +1,33 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def increment_decrement(operations: list) -> int:
+ """
+ This function takes a list of strings representing increment and decrement operations
+ on a variable `x` initialized to 0. It returns the final value of `x` after applying
+ all operations in the list.
+ :param operations: List of strings, each representing an operation on `x`
+ :return: Final value of `x` after all operations
+ """
+
+ counter = 0
+ for operation in operations:
+ if operation == "x++" or operation == "++x":
+ counter += 1
+ elif operation == "x--" or operation == "--x":
+ counter -= 1
+ else:
+ raise ValueError(f"Unknown operation: {operation}")
+
+ return counter
+
+
+def main():
+ result = increment_decrement(sys.argv[1:])
+ print(result)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-323/sgreen/python/ch-2.py b/challenge-323/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..1947d2dfe3
--- /dev/null
+++ b/challenge-323/sgreen/python/ch-2.py
@@ -0,0 +1,50 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def tax_amount(income, tax_brackets: list) -> float:
+ """
+ Calculate the total tax amount based on income and tax brackets.
+ :param income: The income for which tax is to be calculated
+ :param tax_brackets: A list of tax brackets, where each bracket is a list containing
+ the upper limit of the bracket and the tax rate for that bracket.
+ :return: The total tax amount as a Decimal
+ """
+ # Check income does not exceed the last tax bracket
+ if income > tax_brackets[-1][0]:
+ raise ValueError("Income exceeds the last tax bracket limit.")
+
+ # Add a zero tax bracket
+ tax_brackets.insert(0, [0, 0])
+
+ # Initialize total tax amount
+ total_tax = 0
+
+ # Iterate through tax brackets
+ for idx in range(1, len(tax_brackets)):
+ tax_threshold, tax_rate = tax_brackets[idx]
+ total_tax += (min(income, tax_threshold) - tax_brackets[idx - 1][0]) * (tax_rate / 100)
+
+ # We don't need to look at further brackets if income is less than
+ # or equal to the current threshold
+ if income <= tax_threshold:
+ break
+
+ return round(total_tax, 2)
+
+def main():
+ # Convert input into floating point numbers
+ array = [float(n) for n in sys.argv[1:]]
+
+ if len(array) < 3 or len(array) % 2 == 0:
+ raise ValueError("Input must contain at least one income and at least one tax bracket.")
+ income = array[0]
+ tax_brackets = [array[i:i + 2] for i in range(1, len(array), 2)]
+
+ result = tax_amount(income, tax_brackets)
+ print(result)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-323/sgreen/python/test.py b/challenge-323/sgreen/python/test.py
new file mode 100755
index 0000000000..ac48d28290
--- /dev/null
+++ b/challenge-323/sgreen/python/test.py
@@ -0,0 +1,22 @@
+#!/usr/bin/env python3
+
+import unittest
+
+ch_1 = __import__('ch-1')
+ch_2 = __import__('ch-2')
+
+
+class TestClass(unittest.TestCase):
+ def test_ch_1(self):
+ self.assertEqual(ch_1.increment_decrement(["--x", "x++", "x++"]), 1)
+ self.assertEqual(ch_1.increment_decrement(["x++", "++x", "x++"]), 3)
+ self.assertEqual(ch_1.increment_decrement(["x++", "++x", "--x", "x--"]), 0)
+
+ def test_ch_2(self):
+ self.assertEqual(ch_2.tax_amount(10, [[3, 50], [7, 10], [12,25]]), 2.65)
+ self.assertEqual(ch_2.tax_amount(2, [[1, 0], [4, 25], [5,50]]), 0.25)
+ self.assertEqual(ch_2.tax_amount(0, [[2, 50]]), 0)
+
+
+if __name__ == '__main__':
+ unittest.main()