diff options
| -rwxr-xr-x | challenge-237/peter-meszaros/perl/ch-1.pl | 61 | ||||
| -rwxr-xr-x | challenge-237/peter-meszaros/perl/ch-2.pl | 66 |
2 files changed, 127 insertions, 0 deletions
diff --git a/challenge-237/peter-meszaros/perl/ch-1.pl b/challenge-237/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..4cbda65aa0 --- /dev/null +++ b/challenge-237/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,61 @@ +#!/usr/bin/env perl +# +# Given a year, a month, a weekday of month, and a day of week (1 (Mon) .. 7 +# (Sun)), print the day. +# +# Example 1 +# +# Input: Year = 2024, Month = 4, Weekday of month = 3, day of week = 2 +# Output: 16 +# +# The 3rd Tue of Apr 2024 is the 16th +# +# Example 2 +# +# Input: Year = 2025, Month = 10, Weekday of month = 2, day of week = 4 +# Output: 9 +# +# The 2nd Thu of Oct 2025 is the 9th +# +# Example 3 +# +# Input: Year = 2026, Month = 8, Weekday of month = 5, day of week = 3 +# Output: 0 +# +# There isn't a 5th Wed in Aug 2026 +# + +use strict; +use warnings; +use Test::More; +use Data::Dumper; +use Date::Calc qw/Nth_Weekday_of_Month_Year/; + +my $cases = [ + # Year, Month, Weekday of month, day of week + [ 2024, 4, 3, 2], + [ 2025, 10, 2, 4], + [ 2026, 8, 5, 3], +]; + +sub seize_the_day +{ + my $l = shift; + + my (undef, undef, $day) = + Nth_Weekday_of_Month_Year($l->[0], + $l->[1], + $l->[3], + $l->[2]); + + return $day // 0; +} + +is(seize_the_day($cases->[0]), 16, '[ 2024, 4, 3, 2]'); +is(seize_the_day($cases->[1]), 9, '[ 2025, 10, 2, 4]'); +is(seize_the_day($cases->[2]), 0, '[ 2026, 8, 5, 3]'); +done_testing(); + +exit 0; + + diff --git a/challenge-237/peter-meszaros/perl/ch-2.pl b/challenge-237/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..573e51bbe9 --- /dev/null +++ b/challenge-237/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,66 @@ +#!/usr/bin/env perl +# +# You are given an array of integers. +# +# Write a script to permute the give array such that you get the maximum +# possible greatness. +# +# To determine greatness, nums[i] < perm[i] where 0 <= i < nums.length +# +# +# Example 1 +# +# Input: @nums = (1, 3, 5, 2, 1, 3, 1) +# Output: 4 +# +# One possible permutation: (2, 5, 1, 3, 3, 1, 1) which returns 4 greatness as +# below: +# nums[0] < perm[0] +# nums[1] < perm[1] +# nums[3] < perm[3] +# nums[4] < perm[4] +# +# Example 2 +# +# Input: @ints = (1, 2, 3, 4) +# Output: 3 +# +# One possible permutation: (2, 3, 4, 1) which returns 3 greatness as below: +# nums[0] < perm[0] +# nums[1] < perm[1] +# nums[2] < perm[2] +# + +use strict; +use warnings; +use Test::More; +use Data::Dumper; +use Math::Combinatorics qw/permute/; + +my $cases = [ + [1, 3, 5, 2, 1, 3, 1], + [1, 2, 3, 4], +]; + +sub maximise_greatness +{ + my $l = shift; + + my $maxperm = 0; + + for my $p (permute(@$l)) { + my $perm = 0; + for (my $i=0; $i < @$p; ++$i) { + ++$perm if $p->[$i] > $l->[$i]; + } + $maxperm = $perm if $perm > $maxperm; + } + + return $maxperm; +} + +is(maximise_greatness($cases->[0]), 4, '[1, 3, 5, 2, 1, 3, 1]'); +is(maximise_greatness($cases->[1]), 3, '[1, 2, 3, 4]'); +done_testing(); + +exit 0; |
