aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPok <pok@goyangi>2025-05-27 01:03:18 +1000
committerPok <pok@goyangi>2025-05-27 01:03:18 +1000
commit71d41e3840ba847a0efdde2a79c7e8a064bd2230 (patch)
tree06c82e76f5b198cc110faeaf2c78dd9a97305a10
parent0729d1308bfe2e7d4fc1ea6f41b40356645d4f72 (diff)
downloadperlweeklychallenge-club-71d41e3840ba847a0efdde2a79c7e8a064bd2230.tar.gz
perlweeklychallenge-club-71d41e3840ba847a0efdde2a79c7e8a064bd2230.tar.bz2
perlweeklychallenge-club-71d41e3840ba847a0efdde2a79c7e8a064bd2230.zip
pwc323 solution in python
-rw-r--r--challenge-323/pokgopun/python/ch-1.py71
-rw-r--r--challenge-323/pokgopun/python/ch-2.py81
2 files changed, 152 insertions, 0 deletions
diff --git a/challenge-323/pokgopun/python/ch-1.py b/challenge-323/pokgopun/python/ch-1.py
new file mode 100644
index 0000000000..e10b31a4c5
--- /dev/null
+++ b/challenge-323/pokgopun/python/ch-1.py
@@ -0,0 +1,71 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-323/
+"""
+
+Task 1: Increment Decrement
+
+Submitted by: [47]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
+
+Task 2: Tax Amount
+"""
+### solution by pokgopun@gmail.com
+
+def id(ops: tuple[str]) -> int:
+ v = 0
+ for op in ops:
+ for sig in (op[0],op[-1]):
+ if sig == "-":
+ v -= 1
+ elif sig == "+":
+ v += 1
+ else:
+ continue
+ break
+ return v
+
+import unittest
+
+class TestId(unittest.TestCase):
+ def test(self):
+ for inpt, otpt in {
+ ("--x", "x++", "x++"): 1,
+ ("x++", "++x", "x++"): 3,
+ ("x++", "++x", "--x", "x--"): 0,
+ }.items():
+ self.assertEqual(id(inpt),otpt)
+
+unittest.main()
+
+
diff --git a/challenge-323/pokgopun/python/ch-2.py b/challenge-323/pokgopun/python/ch-2.py
new file mode 100644
index 0000000000..2ca9af4423
--- /dev/null
+++ b/challenge-323/pokgopun/python/ch-2.py
@@ -0,0 +1,81 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-323/
+"""
+
+Task 2: Tax Amount
+
+Submitted by: [48]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
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 1st June 2025.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+"""
+### solution by pokgopun@gmail.com
+
+from dataclasses import dataclass
+
+@dataclass
+class TaxB:
+ ceil: float
+ perc: float
+
+def ta(income: float, tax: list[TaxB]) -> float:
+ floor = .0
+ ttax = 0
+ taxt = 0
+ for tb in tax:
+ if income >= tb.ceil:
+ taxt = tb.ceil - floor
+ else:
+ taxt = income - floor
+ ttax += taxt * tb.perc / 100
+ if income <= tb.ceil:
+ break
+ floor = tb.ceil
+ return ttax
+
+import unittest
+
+class TestTa(unittest.TestCase):
+ def test(self):
+ for income, tax, otpt in (
+ (10, (TaxB(3, 50), TaxB(7, 10), TaxB(12,25)), 2.65),
+ (2, (TaxB(1, 0), TaxB(4, 25), TaxB(5,50)), 0.25),
+ (0, (TaxB(2, 50),), 0),
+ ):
+ self.assertEqual(ta(income,tax), otpt)
+
+unittest.main()