aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-06-06 23:26:14 +0100
committerGitHub <noreply@github.com>2025-06-06 23:26:14 +0100
commitab838c841ffdff146d5a0c7d25d375bb68fccc21 (patch)
tree6eee8996b77411e2a4c70f303aa6290601cbea76
parente400c589e0601c7d1c643e7c83f3a13eb39336c7 (diff)
parentcb89d632f5fdb4209af12bf8f65566abc2f6072e (diff)
downloadperlweeklychallenge-club-ab838c841ffdff146d5a0c7d25d375bb68fccc21.tar.gz
perlweeklychallenge-club-ab838c841ffdff146d5a0c7d25d375bb68fccc21.tar.bz2
perlweeklychallenge-club-ab838c841ffdff146d5a0c7d25d375bb68fccc21.zip
Merge pull request #12134 from ysth/challenge-324-ysth
challenge 324 idiomatic perl solutions by ysth
-rw-r--r--challenge-323/ysth/perl/ch-1.pl3
-rw-r--r--challenge-323/ysth/perl/ch-2.pl23
-rw-r--r--challenge-324/ysth/blog.txt1
-rw-r--r--challenge-324/ysth/perl/ch-1.pl12
-rw-r--r--challenge-324/ysth/perl/ch-2.pl7
5 files changed, 46 insertions, 0 deletions
diff --git a/challenge-323/ysth/perl/ch-1.pl b/challenge-323/ysth/perl/ch-1.pl
new file mode 100644
index 0000000000..dec71955a8
--- /dev/null
+++ b/challenge-323/ysth/perl/ch-1.pl
@@ -0,0 +1,3 @@
+use 5.036;
+
+say y/+///2-y/-///2 for "@ARGV";
diff --git a/challenge-323/ysth/perl/ch-2.pl b/challenge-323/ysth/perl/ch-2.pl
new file mode 100644
index 0000000000..503278ec45
--- /dev/null
+++ b/challenge-323/ysth/perl/ch-2.pl
@@ -0,0 +1,23 @@
+use 5.036;
+
+use JSON::XS 'decode_json';
+use List::Util 'min';
+use Math::BigRat ();
+
+my $income = Math::BigRat->new(shift // 10);
+my $brackets = decode_json(shift // '[[3, 50], [7, 10], [12,25]]');
+push @$brackets, [Math::BigRat->binf, 0];
+
+my $tax = 0;
+$income = 0 if $income < 0;
+my $bracket_start = 0;
+for (;;) {
+ my $bracket = shift @$brackets;
+ my $final_bracket = $income <= $bracket->[0];
+ my $bracket_amount = ($final_bracket ? $income : Math::BigRat->new($bracket->[0])) - $bracket_start;
+ $tax += $bracket_amount * $bracket->[1] / 100;
+ last if $final_bracket;
+ $bracket_start = $bracket->[0];
+}
+
+say $tax->numify;
diff --git a/challenge-324/ysth/blog.txt b/challenge-324/ysth/blog.txt
new file mode 100644
index 0000000000..231462af66
--- /dev/null
+++ b/challenge-324/ysth/blog.txt
@@ -0,0 +1 @@
+https://blog.ysth.info/idiomatic-perl-solutions-to-the-weekly-challenge-324/
diff --git a/challenge-324/ysth/perl/ch-1.pl b/challenge-324/ysth/perl/ch-1.pl
new file mode 100644
index 0000000000..c651d332c0
--- /dev/null
+++ b/challenge-324/ysth/perl/ch-1.pl
@@ -0,0 +1,12 @@
+use 5.036;
+
+@ARGV = qw(2 2 1 2 3 4) unless @ARGV;
+my ($r, $c, @ints) = map int, @ARGV;
+unless ($r >= 0 && $c >= 0 && @ints == $r * $c) {
+ die "expected count of rows, count of columns, and list of that many integers\n";
+}
+
+my @out = map [splice @ints, 0, $c], 1..$r;
+
+use Data::Dumper;
+say Data::Dumper->new([$_])->Terse(1)->Indent(0)->Dump for @out;
diff --git a/challenge-324/ysth/perl/ch-2.pl b/challenge-324/ysth/perl/ch-2.pl
new file mode 100644
index 0000000000..3e5b873305
--- /dev/null
+++ b/challenge-324/ysth/perl/ch-2.pl
@@ -0,0 +1,7 @@
+use 5.036;
+use List::Util 'sum';
+
+my @ints = grep !/\D/a, @ARGV;
+die "expected list of integers\n" if @ints != @ARGV;
+
+say sum map eval, glob 0 . join '', map "{^$_,}", @ints;