From cb89d632f5fdb4209af12bf8f65566abc2f6072e Mon Sep 17 00:00:00 2001 From: Yitzchak Scott-Thoennes Date: Wed, 4 Jun 2025 23:08:49 -0400 Subject: challenge 324 idiomatic perl solutions by ysth --- challenge-323/ysth/perl/ch-1.pl | 3 +++ challenge-323/ysth/perl/ch-2.pl | 23 +++++++++++++++++++++++ challenge-324/ysth/blog.txt | 1 + challenge-324/ysth/perl/ch-1.pl | 12 ++++++++++++ challenge-324/ysth/perl/ch-2.pl | 7 +++++++ 5 files changed, 46 insertions(+) create mode 100644 challenge-323/ysth/perl/ch-1.pl create mode 100644 challenge-323/ysth/perl/ch-2.pl create mode 100644 challenge-324/ysth/blog.txt create mode 100644 challenge-324/ysth/perl/ch-1.pl create mode 100644 challenge-324/ysth/perl/ch-2.pl 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; -- cgit