aboutsummaryrefslogtreecommitdiff
path: root/challenge-323/sgreen/python
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-06-01 22:54:44 +0100
committerGitHub <noreply@github.com>2025-06-01 22:54:44 +0100
commit72a1c60daab582b75fc1439c852a7c868adc0172 (patch)
tree11330f178a2108df26455ca98592d415c28a54c9 /challenge-323/sgreen/python
parent3517294cda226112a6cddb4ab8c76f285694ad83 (diff)
parent08dd8bb4e98654546426dfa427943a5670912745 (diff)
downloadperlweeklychallenge-club-72a1c60daab582b75fc1439c852a7c868adc0172.tar.gz
perlweeklychallenge-club-72a1c60daab582b75fc1439c852a7c868adc0172.tar.bz2
perlweeklychallenge-club-72a1c60daab582b75fc1439c852a7c868adc0172.zip
Merge pull request #12111 from simongreen-net/master
sgreen solutions to challenge 323
Diffstat (limited to 'challenge-323/sgreen/python')
-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
3 files changed, 105 insertions, 0 deletions
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()