aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpme <hauptadler@gmail.com>2025-05-26 19:55:06 +0200
committerpme <hauptadler@gmail.com>2025-05-26 19:55:06 +0200
commit63d984337435edb0c7dfa81d9e80ad10a5c461a4 (patch)
treea7e316fadfc1d1a304c6dd3d4d65c9044df97378
parent0729d1308bfe2e7d4fc1ea6f41b40356645d4f72 (diff)
downloadperlweeklychallenge-club-63d984337435edb0c7dfa81d9e80ad10a5c461a4.tar.gz
perlweeklychallenge-club-63d984337435edb0c7dfa81d9e80ad10a5c461a4.tar.bz2
perlweeklychallenge-club-63d984337435edb0c7dfa81d9e80ad10a5c461a4.zip
challenge-323
-rwxr-xr-xchallenge-323/peter-meszaros/perl/ch-1.pl76
-rwxr-xr-xchallenge-323/peter-meszaros/perl/ch-2.pl81
-rwxr-xr-xchallenge-323/peter-meszaros/tcl/ch-1.tcl73
-rwxr-xr-xchallenge-323/peter-meszaros/tcl/ch-2.tcl83
4 files changed, 313 insertions, 0 deletions
diff --git a/challenge-323/peter-meszaros/perl/ch-1.pl b/challenge-323/peter-meszaros/perl/ch-1.pl
new file mode 100755
index 0000000000..b17de6075c
--- /dev/null
+++ b/challenge-323/peter-meszaros/perl/ch-1.pl
@@ -0,0 +1,76 @@
+#!/usr/bin/env perl
+#
+=head1 Task 1: Increment Decrement
+
+Submitted by: 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
+
+=head2 Example 1
+
+ Input: @operations = ("--x", "x++", "x++")
+ Output: 1
+
+ Operation "--x" => 0 - 1 => -1
+ Operation "x++" => -1 + 1 => 0
+ Operation "x++" => 0 + 1 => 1
+
+=head2 Example 2
+
+ Input: @operations = ("x++", "++x", "x++")
+ Output: 3
+
+=head2 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
+
+=cut
+
+use strict;
+use warnings;
+use Test2::V0 -no_srand => 1;
+use Data::Dumper;
+
+my $cases = [
+ [["--x", "x++", "x++"], 1, "Example 1"],
+ [["x++", "++x", "x++"], 3, "Example 2"],
+ [["x++", "++x", "--x", "x--"], 0, "Example 3"],
+ [["x++", , "--y", "x--"], undef, "Example 4"],
+];
+
+sub increment_decrement
+{
+ my $operations = shift;
+ my $value = 0;
+ for my $op (@$operations) {
+ if ($op eq '++x' || $op eq 'x++') {
+ $value++;
+ } elsif ($op eq '--x' || $op eq 'x--') {
+ $value--;
+ } else {
+ return undef;
+ }
+ }
+ return $value;
+}
+
+for (@$cases) {
+ is(increment_decrement($_->[0]), $_->[1], $_->[2]);
+}
+done_testing();
+
+exit 0;
diff --git a/challenge-323/peter-meszaros/perl/ch-2.pl b/challenge-323/peter-meszaros/perl/ch-2.pl
new file mode 100755
index 0000000000..fc71356f90
--- /dev/null
+++ b/challenge-323/peter-meszaros/perl/ch-2.pl
@@ -0,0 +1,81 @@
+#!/usr/bin/env perl
+#
+=head1 Task 2: Tax Amount
+
+Submitted by: Mohammad Sajid Anwar
+
+You are given an income amount and tax brackets.
+
+Write a script to calculate the total tax amount.
+
+=head2 Example 1
+
+ Input: $income = 10, @tax = ([3, 50], [7, 10], [12,25])
+ Output: 1.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
+
+=head2 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
+
+=head2 Example 3
+
+ Input: $income = 0, @tax = ([2, 50])
+ Output: 0
+
+=cut
+
+use strict;
+use warnings;
+use Test2::V0 -no_srand => 1;
+use Data::Dumper;
+
+my $cases = [
+ [[10, [[3, 50], [7, 10], [12,25]]], 2.65, "Example 1"],
+ [[ 2, [[1, 0], [4, 25], [5,50]]], 0.25, "Example 2"],
+ [[ 0, [[2, 50]]], 0, "Example 3"],
+];
+
+sub tax_amount
+{
+ my $income = $_[0]->[0];
+ my $tax = $_[0]->[1];
+
+ my $total_tax = 0;
+ my $last_limit = 0;
+ for my $t (@$tax) {
+ my $limit = $t->[0];
+ my $rate = $t->[1] / 100;
+
+ last if $income <= $last_limit;
+
+ if ($income > $limit) {
+ $total_tax += ($limit - $last_limit) * $rate;
+ } else {
+ $total_tax += ($income - $last_limit) * $rate;
+ last;
+ }
+ $last_limit = $limit;
+ }
+
+ return $total_tax;
+}
+
+for (@$cases) {
+ is(tax_amount($_->[0]), $_->[1], $_->[2]);
+}
+done_testing();
+
+exit 0;
diff --git a/challenge-323/peter-meszaros/tcl/ch-1.tcl b/challenge-323/peter-meszaros/tcl/ch-1.tcl
new file mode 100755
index 0000000000..72787a5374
--- /dev/null
+++ b/challenge-323/peter-meszaros/tcl/ch-1.tcl
@@ -0,0 +1,73 @@
+#!/usr/bin/env tclsh
+#
+# Task 1: Increment Decrement
+#
+# Submitted by: 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
+#
+
+package require tcltest
+
+set cases {
+ {{"--x" "x++" "x++"} 1 "Example 1"}
+ {{"x++" "++x" "x++"} 3 "Example 2"}
+ {{"x++" "++x" "--x" "x--"} 0 "Example 3"}
+ {{"x++" "--y" "x--"} null "Example 4"}
+}
+
+proc increment_decrement {p} {
+ set value 0
+ foreach op $p {
+ if {$op eq "++x" || $op eq "x++"} {
+ incr value
+ } elseif {$op eq "--x" || $op eq "x--"} {
+ incr value -1
+ } else {
+ return null
+ }
+ }
+ return $value
+}
+
+tcltest::configure -verbose {pass}
+foreach case $cases {
+ tcltest::test [lindex $case 2] {} {
+ increment_decrement [lindex $case 0]
+ } [lindex $case 1]
+}
+
+exit 0
+
diff --git a/challenge-323/peter-meszaros/tcl/ch-2.tcl b/challenge-323/peter-meszaros/tcl/ch-2.tcl
new file mode 100755
index 0000000000..8c4999775c
--- /dev/null
+++ b/challenge-323/peter-meszaros/tcl/ch-2.tcl
@@ -0,0 +1,83 @@
+#!/usr/bin/env tclsh
+#
+# Task 2: Tax Amount
+#
+# Submitted by: 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: 1.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
+#
+
+package require tcltest
+
+set cases {
+ {{10 {{3 50} {7 10} {12 25}}} 2.65 "Example 1"}
+ {{ 2 {{1 0} {4 25} {5 50}}} 0.25 "Example 2"}
+ {{ 0 {{2 50}}} 0.0 "Example 3"}
+}
+
+proc tax_amount {p} {
+ set income [lindex $p 0]
+ set tax [lindex $p 1]
+
+ set total_tax 0.0
+ set last_limit 0
+
+ foreach t $tax {
+ set limit [lindex $t 0]
+ set rate [expr [lindex $t 1] / 100.0]
+
+ if {$income <= $last_limit} {
+ break
+ }
+
+ if {$income > $limit} {
+ set total_tax [expr $total_tax + ($limit - $last_limit) * $rate]
+ } else {
+ set total_tax [expr $total_tax + ($income - $last_limit) * $rate]
+ break
+ }
+ set last_limit $limit
+ }
+
+ return $total_tax
+}
+
+tcltest::configure -verbose {pass}
+foreach case $cases {
+ tcltest::test [lindex $case 2] {} {
+ tax_amount [lindex $case 0]
+ } [lindex $case 1]
+}
+
+exit 0
+
+