diff options
| author | Util <bruce.gray@acm.org> | 2023-10-08 16:11:17 -0500 |
|---|---|---|
| committer | Util <bruce.gray@acm.org> | 2023-10-08 16:11:17 -0500 |
| commit | d81a385563ace903ff8c75241a6d9981242ea3b5 (patch) | |
| tree | e6def65cf77a85a13d91a12f2d4a26a3b54457eb /challenge-237 | |
| parent | c1bfbdc2269919b85579f941d22b5c6c9682fa99 (diff) | |
| download | perlweeklychallenge-club-d81a385563ace903ff8c75241a6d9981242ea3b5.tar.gz perlweeklychallenge-club-d81a385563ace903ff8c75241a6d9981242ea3b5.tar.bz2 perlweeklychallenge-club-d81a385563ace903ff8c75241a6d9981242ea3b5.zip | |
Add TWC 237 solutions by Bruce Gray (Raku only).
Diffstat (limited to 'challenge-237')
| -rw-r--r-- | challenge-237/bruce-gray/raku/ch-1.raku | 35 | ||||
| -rw-r--r-- | challenge-237/bruce-gray/raku/ch-2.raku | 60 |
2 files changed, 95 insertions, 0 deletions
diff --git a/challenge-237/bruce-gray/raku/ch-1.raku b/challenge-237/bruce-gray/raku/ch-1.raku new file mode 100644 index 0000000000..1f5174d0ed --- /dev/null +++ b/challenge-237/bruce-gray/raku/ch-1.raku @@ -0,0 +1,35 @@ +sub task1 ( $year, $month, $weekday_of_month, $day_of_week --> UInt ) { + + my Date $day_1 .= new: :$year, :$month; + + my Date $first_dow = $day_1 + + ($day_of_week - $day_1.day-of-week) mod 7; + + my Date $nth_dow = $first_dow + + ($weekday_of_month - 1) * 7; + + return $nth_dow.month == $month + ?? $nth_dow.day + !! 0; +} + + +my @tests = + ( 16, (2024, 4, 3, 2) ), + ( 9, (2025, 10, 2, 4) ), + ( 0, (2026, 8, 5, 3) ), + + ( 2, (2023, 10, 1, 1) ), # To spot `abs` vs `mod` issue. + ( 31, (2023, 10, 5, 2) ), +; +use Test; plan @tests + 1; +for @tests -> ( $expected, @in ) { + is task1(|@in), $expected, @in; +} +{ + my @tests_big = map { .day, (.year, .month, .weekday-of-month, .day-of-week) }, + Date.new(:2023year) ..^ Date.new(:2033year); + + my $good = +grep { task1(|.[1]) == .[0] }, @tests_big; + is $good, +@tests_big, "All {@tests_big.elems} are correct"; +} diff --git a/challenge-237/bruce-gray/raku/ch-2.raku b/challenge-237/bruce-gray/raku/ch-2.raku new file mode 100644 index 0000000000..e94fe8a69d --- /dev/null +++ b/challenge-237/bruce-gray/raku/ch-2.raku @@ -0,0 +1,60 @@ +sub greatness (@a, @b) { [+] @a Z< @b } + +sub task2_permute ( @ns --> UInt ) { + my &great_vs_nums = &greatness.assuming(@ns); + + return [max] map &great_vs_nums, permutations @ns; +} + +sub construct_best_perm ( @descending ) { + die unless [>=] @descending; + + my @to_use_up = @descending; + + return gather for @descending -> $num { + take @to_use_up.head > $num + ?? @to_use_up.shift + !! @to_use_up.pop; + } +} +sub task2_construct ( @ns --> UInt ) { + my @ordered = @ns.sort: -*; + + return greatness( @ordered, construct_best_perm(@ordered) ); +} + +sub mk_order ( @a --> List ) { @a.antipairs.sort».value } +sub task2_construct_and_show ( @ns --> UInt ) { + my @order = mk_order(@ns ).reverse; + my @unorder = mk_order(@order); + + my @perm = construct_best_perm(@ns[@order])[@unorder]; + + # .say for @perm, @ns, +«(@perm Z> @ns); # For debugging + return greatness( @ns, @perm ); +} + + + +my @tests = + ( 4, (1, 3, 5, 2, 1, 3, 1) ), + ( 3, (1, 2, 3, 4) ), + + + ( 3, (1, 2, 3, 14) ), # to spot non-numeric sorting. + + ( 6, ( 8,9,5,5,9,4,4,2 ) ), + # ( 6, ( 8,9,5,5,9,4,4,2,5 ) ), + # ( 6, ( 18,19,5,5,19,4,4,2,5 ) ), +; +my @subs = + permute__ => &task2_permute, + construct => &task2_construct, + con_show_ => &task2_construct_and_show +; +use Test; plan @tests * @subs; +for @tests.kv -> $n, ( $expected, @in ) { + for @subs -> ( :key($sub_name), :value(&task2) ) { + is task2(@in), $expected, "$sub_name {$n+1} : @in[]"; + } +} |
