diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-02-16 22:39:38 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-02-16 22:39:38 +0000 |
| commit | fd74e66af1b93c344ce8bc4076760538bc1d0651 (patch) | |
| tree | 01414624de0c09aea923ef0ebb88b43083e3c829 | |
| parent | 4ed3c9e9a45b004a60c406367b89e0602f9fa6f1 (diff) | |
| parent | a8e2e8f4169a1887dd35bd9516e1d931ba57528a (diff) | |
| download | perlweeklychallenge-club-fd74e66af1b93c344ce8bc4076760538bc1d0651.tar.gz perlweeklychallenge-club-fd74e66af1b93c344ce8bc4076760538bc1d0651.tar.bz2 perlweeklychallenge-club-fd74e66af1b93c344ce8bc4076760538bc1d0651.zip | |
Merge pull request #3549 from polettix/polettix/pwc100
Add polettix's solutions to PWC100
| -rw-r--r-- | challenge-100/polettix/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-100/polettix/blog1.txt | 1 | ||||
| -rw-r--r-- | challenge-100/polettix/perl/ch-1.pl | 19 | ||||
| -rw-r--r-- | challenge-100/polettix/perl/ch-2.pl | 35 |
4 files changed, 56 insertions, 0 deletions
diff --git a/challenge-100/polettix/blog.txt b/challenge-100/polettix/blog.txt new file mode 100644 index 0000000000..0c04e798b4 --- /dev/null +++ b/challenge-100/polettix/blog.txt @@ -0,0 +1 @@ +https://github.polettix.it/ETOOBUSY/2021/02/17/pwc100-fun-time/ diff --git a/challenge-100/polettix/blog1.txt b/challenge-100/polettix/blog1.txt new file mode 100644 index 0000000000..b7508d4664 --- /dev/null +++ b/challenge-100/polettix/blog1.txt @@ -0,0 +1 @@ +https://github.polettix.it/ETOOBUSY/2021/02/18/pwc100-triangle-sum/ diff --git a/challenge-100/polettix/perl/ch-1.pl b/challenge-100/polettix/perl/ch-1.pl new file mode 100644 index 0000000000..5a8ed5b2bb --- /dev/null +++ b/challenge-100/polettix/perl/ch-1.pl @@ -0,0 +1,19 @@ +#!/usr/bin/env perl +use 5.024; +use warnings; +use experimental 'signatures'; +no warnings 'experimental::signatures'; + +sub fun_time ($t) { + my ($h, $m, $ampm) = $t =~ m{\A(\d\d):(\d\d)(?:\s*(am|pm))?\z}mxs; + ($h, $ampm) = + ($ampm//='') eq 'pm' ? ($h < 12 ? $h + 12 : 12, '' ) + : $ampm eq 'am' ? ($h < 12 ? $h : 0 , '' ) + : $h == 0 ? (12 , ' am') + : $h == 12 ? (12 , ' pm') + : $h > 12 ? ($h - 12 , ' pm') + : ($h , ' am'); + return sprintf "%02d:%02d%s", $h % 24, $m, $ampm; +} + +say fun_time(shift || '05:15'); diff --git a/challenge-100/polettix/perl/ch-2.pl b/challenge-100/polettix/perl/ch-2.pl new file mode 100644 index 0000000000..e0d0c07b9a --- /dev/null +++ b/challenge-100/polettix/perl/ch-2.pl @@ -0,0 +1,35 @@ +#!/usr/bin/env perl +use 5.024; +use warnings; +use experimental qw< postderef signatures >; +no warnings qw< experimental::postderef experimental::signatures >; +use List::Util 'min'; + +sub triangle_sum ($tri) { + my @s = $tri->[0][0]; + my $i = 1; + while ($i <= $tri->$#*) { + my $l = $tri->[$i]; + my @ns = $s[0] + $l->[0]; + push @ns, $l->[$_] + ($s[$_ - 1] < $s[$_] ? $s[$_ - 1] : $s[$_]) + for 1 .. $l->$#* - 1; + push @ns, $s[-1] + $l->[-1]; + @s = @ns; + ++$i; + } + return min(@s); +} + +sub triangularize (@list) { + my @retval; + my $n = 1; + while (@list) { + die "invalid number of elements\n" unless @list >= $n; + push @retval, [splice @list, 0, $n]; + ++$n; + } + return \@retval; +} + +my @list = @ARGV ? @ARGV : qw< 1 2 4 6 4 9 5 1 7 2 >; +say triangle_sum(triangularize(@list)); |
