aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Köhler <jean-luc@picard.franken.de>2025-05-29 14:55:56 +0200
committerThomas Köhler <jean-luc@picard.franken.de>2025-05-29 14:55:56 +0200
commit24a611f5aea40c559178c25f08971e330dbd4ecd (patch)
tree9037b21d34e3f70aa549a5d1d5741c91c32f4aab
parent1f000a6d1875beabc5938005e4445127d5a1bb3d (diff)
downloadperlweeklychallenge-club-24a611f5aea40c559178c25f08971e330dbd4ecd.tar.gz
perlweeklychallenge-club-24a611f5aea40c559178c25f08971e330dbd4ecd.tar.bz2
perlweeklychallenge-club-24a611f5aea40c559178c25f08971e330dbd4ecd.zip
Add solution 323.
Signed-off-by: Thomas Köhler <jean-luc@picard.franken.de>
-rw-r--r--challenge-323/jeanluc2020/blog-1.txt1
-rw-r--r--challenge-323/jeanluc2020/blog-2.txt1
-rwxr-xr-xchallenge-323/jeanluc2020/perl/ch-1.pl73
-rwxr-xr-xchallenge-323/jeanluc2020/perl/ch-2.pl81
4 files changed, 156 insertions, 0 deletions
diff --git a/challenge-323/jeanluc2020/blog-1.txt b/challenge-323/jeanluc2020/blog-1.txt
new file mode 100644
index 0000000000..314713955b
--- /dev/null
+++ b/challenge-323/jeanluc2020/blog-1.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-323-1.html
diff --git a/challenge-323/jeanluc2020/blog-2.txt b/challenge-323/jeanluc2020/blog-2.txt
new file mode 100644
index 0000000000..e07a529969
--- /dev/null
+++ b/challenge-323/jeanluc2020/blog-2.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-323-2.html
diff --git a/challenge-323/jeanluc2020/perl/ch-1.pl b/challenge-323/jeanluc2020/perl/ch-1.pl
new file mode 100755
index 0000000000..acb161fd24
--- /dev/null
+++ b/challenge-323/jeanluc2020/perl/ch-1.pl
@@ -0,0 +1,73 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-323/#TASK1
+#
+# Task 1: Increment Decrement
+# ===========================
+#
+# 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
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Each possible input maps to either +1 or -1, so we just
+# fill a hash with those possible inputs as keys and their
+# corresponding mappings as values. Then we just need to
+# add up all the values for all given operations.
+
+use v5.36;
+
+increment_decrement("--x", "x++", "x++");
+increment_decrement("x++", "++x", "x++");
+increment_decrement("x++", "++x", "--x", "x--");
+
+sub increment_decrement( @operations ) {
+ say "Input: (\"" . join("\", \"", @operations) . "\")";
+ my $value = 0;
+ my $add = {
+ "++x" => 1,
+ "x++" => 1,
+ "--x" => -1,
+ "x--" => -1,
+ };
+ foreach my $op (@operations) {
+ $value += $add->{$op};
+ }
+ say "Output: $value";
+}
diff --git a/challenge-323/jeanluc2020/perl/ch-2.pl b/challenge-323/jeanluc2020/perl/ch-2.pl
new file mode 100755
index 0000000000..2a5d9fee87
--- /dev/null
+++ b/challenge-323/jeanluc2020/perl/ch-2.pl
@@ -0,0 +1,81 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-323/#TASK2
+#
+# Task 2: Tax Amount
+# ==================
+#
+# 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
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# First of all, there is no indication what should happen once
+# all tax brackets have been used up. I'm going to assume the tax
+# is zero in that case.
+# Then we just check each bracket, always keeping track of the previous
+# bracket to know how big the part will be for the current bracket,
+# and calculate the tax.
+
+use v5.36;
+
+tax_amount(10, [3, 50], [7, 10], [12,25]);
+tax_amount(2, [1, 0], [4, 25], [5,50]);
+tax_amount(0, [2, 50]);
+tax_amount(5, [2, 50]);
+
+sub tax_amount($income, @tax) {
+ say "Income: $income";
+ print "Tax brackets: (";
+ foreach my $t (@tax) {
+ print "[$t->[0], $t->[1]], ";
+ }
+ say ")";
+ my $tax = 0;
+ my $previous = 0;
+ foreach my $t (@tax) {
+ my $end = $t->[0];
+ my $percent = $t->[1];
+ if($income <= $end) {
+ $tax += ($income - $previous) * $percent / 100;
+ last;
+ } else {
+ $tax += ($end - $previous) * $percent / 100;
+ $previous = $end;
+ }
+ }
+ say "Output: $tax";
+}