diff options
| author | Pok <pok@goyangi> | 2025-05-27 01:03:42 +1000 |
|---|---|---|
| committer | Pok <pok@goyangi> | 2025-05-27 01:03:42 +1000 |
| commit | 855884a2ec6d853ec4ae345afbdc358c7801899f (patch) | |
| tree | 020d005ddf57f3790a6eac733b9f9b00e8a72744 | |
| parent | 71d41e3840ba847a0efdde2a79c7e8a064bd2230 (diff) | |
| download | perlweeklychallenge-club-855884a2ec6d853ec4ae345afbdc358c7801899f.tar.gz perlweeklychallenge-club-855884a2ec6d853ec4ae345afbdc358c7801899f.tar.bz2 perlweeklychallenge-club-855884a2ec6d853ec4ae345afbdc358c7801899f.zip | |
pwc323 solution in go
| -rw-r--r-- | challenge-323/pokgopun/go/ch-1.go | 86 | ||||
| -rw-r--r-- | challenge-323/pokgopun/go/ch-2.go | 97 |
2 files changed, 183 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 + } +} |
