diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-05-27 11:23:10 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-05-27 11:23:10 +0100 |
| commit | 137124cd00a1d0b64d24302294c0feb95670972f (patch) | |
| tree | 5e39300cdaa8052a7b0100806fb66e4af2805952 | |
| parent | 4c68cbc2f53bd66e21086f8ae91457be14d9bc0d (diff) | |
| parent | 855884a2ec6d853ec4ae345afbdc358c7801899f (diff) | |
| download | perlweeklychallenge-club-137124cd00a1d0b64d24302294c0feb95670972f.tar.gz perlweeklychallenge-club-137124cd00a1d0b64d24302294c0feb95670972f.tar.bz2 perlweeklychallenge-club-137124cd00a1d0b64d24302294c0feb95670972f.zip | |
Merge pull request #12090 from pokgopun/pwc323
Pwc323
| -rw-r--r-- | challenge-323/pokgopun/go/ch-1.go | 86 | ||||
| -rw-r--r-- | challenge-323/pokgopun/go/ch-2.go | 97 | ||||
| -rw-r--r-- | challenge-323/pokgopun/python/ch-1.py | 71 | ||||
| -rw-r--r-- | challenge-323/pokgopun/python/ch-2.py | 81 |
4 files changed, 335 insertions, 0 deletions
diff --git a/challenge-323/pokgopun/go/ch-1.go b/challenge-323/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..fd9c265f95 --- /dev/null +++ b/challenge-323/pokgopun/go/ch-1.go @@ -0,0 +1,86 @@ +//# 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 + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +type operations []string + +func (ops operations) process() int { + r := 0 + sigm := byte('-') + sigp := byte('+') + for _, op := range ops { + for _, sig := range []byte{op[0], op[2]} { + if sig == sigm { + r-- + } else if sig == sigp { + r++ + } else { + continue + } + break + } + } + return r +} + +func main() { + for _, data := range []struct { + input operations + output int + }{ + {operations{"--x", "x++", "x++"}, 1}, + {operations{"x++", "++x", "x++"}, 3}, + {operations{"x++", "++x", "--x", "x--"}, 0}, + } { + io.WriteString(os.Stdout, cmp.Diff(data.input.process(), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-323/pokgopun/go/ch-2.go b/challenge-323/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..a1662f5a27 --- /dev/null +++ b/challenge-323/pokgopun/go/ch-2.go @@ -0,0 +1,97 @@ +//# 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 + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +type taxb struct { + ceil, perc float64 +} + +type taxbs []taxb + +type input struct { + income float64 + tax taxbs +} + +func (in input) process() float64 { + var floor float64 + var ttax, taxt float64 + for _, tb := range in.tax { + if in.income >= tb.ceil { + taxt = tb.ceil - floor + } else { + taxt = in.income - floor + } + ttax += taxt * tb.perc / 100 + if in.income <= tb.ceil { + break + } + floor = tb.ceil + } + return ttax +} + +func main() { + for _, data := range []struct { + input input + output float64 + }{ + {input{10, taxbs{taxb{3, 50}, taxb{7, 10}, taxb{12, 25}}}, 2.65}, + {input{2, taxbs{taxb{1, 0}, taxb{4, 25}, taxb{5, 50}}}, 0.25}, + {input{0, taxbs{taxb{2, 50}}}, 0}, + } { + io.WriteString(os.Stdout, cmp.Diff(data.input.process(), data.output)) // blank if ok, otherwise show the difference + } +} 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() |
