diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-10-08 23:27:36 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-10-08 23:27:36 +0100 |
| commit | a8acfea1d79ee2ef789fa3b2d0a2e900105012fe (patch) | |
| tree | dc188a7b013e0a3aaeb8c6a0933073f8d0d44a29 | |
| parent | 61216ee5b59f80bdbddca238fe7c59224feff65b (diff) | |
| parent | 2a17a7597896d3c83ab298550fbfacaca90f4ac1 (diff) | |
| download | perlweeklychallenge-club-a8acfea1d79ee2ef789fa3b2d0a2e900105012fe.tar.gz perlweeklychallenge-club-a8acfea1d79ee2ef789fa3b2d0a2e900105012fe.tar.bz2 perlweeklychallenge-club-a8acfea1d79ee2ef789fa3b2d0a2e900105012fe.zip | |
Merge pull request #8833 from BarrOff/barroff-237
feat: add solutions for challenge 237 from BarrOff
| -rw-r--r-- | challenge-237/barroff/raku/ch-1.p6 | 33 | ||||
| -rw-r--r-- | challenge-237/barroff/raku/ch-2.p6 | 26 | ||||
| -rw-r--r-- | challenge-237/barroff/raku/ch_1.nim | 42 | ||||
| -rw-r--r-- | challenge-237/barroff/raku/ch_2.nim | 29 |
4 files changed, 130 insertions, 0 deletions
diff --git a/challenge-237/barroff/raku/ch-1.p6 b/challenge-237/barroff/raku/ch-1.p6 new file mode 100644 index 0000000000..ca5ebfcd79 --- /dev/null +++ b/challenge-237/barroff/raku/ch-1.p6 @@ -0,0 +1,33 @@ +#!/usr/bin/env raku + +use v6.d; + +my subset Month of UInt where {not .defined or ($_ > 0 and $_ < 13)}; +my subset WD of UInt where {not .defined or ($_ > 0 and $_ < 6)}; +my subset Dow of UInt where {not .defined or ($_ > 0 and $_ < 8)}; + +sub size-the-day(UInt:D $year, Month:D $month , WD:D $wd, Dow:D $dow --> Int:D) { + my Date $day = Date.new($year, $month, 1); + my UInt $week-shift = $day.day-of-week > $dow ?? 1 !! 2; + if $wd == 1 { + return $week-shift == 1 ?? 0 !! 1 + $dow - $day.date-of-week; + } + my UInt $days = 7 - $day.day-of-week + ($wd - $week-shift) * 7 + $dow; + my Date $final-day = $day.later(days => $days); + return $final-day.month == $day.month ?? $final-day.day !! 0; +} + +#| Run test cases +multi sub MAIN('test') { + use Test; + plan 3; + + is size-the-day(2024, 4, 3, 2), 16, 'works for size-the-day(2024, 4, 3, 2)'; + is size-the-day(2025, 10, 2, 4), 9, 'works for size-the-day(2025, 10, 2, 4)'; + is size-the-day(2026, 8, 5, 3), 0, 'works for size-the-day(2026, 8, 5, 3)'; +} + +#| Take user provided year like 2023 4 3 2 +multi sub MAIN(UInt:D $year, Month:D $month , WD:D $wd, Dow:D $dow) { + say size-the-day($year, $month , $wd, $dow); +} diff --git a/challenge-237/barroff/raku/ch-2.p6 b/challenge-237/barroff/raku/ch-2.p6 new file mode 100644 index 0000000000..7249ae51f8 --- /dev/null +++ b/challenge-237/barroff/raku/ch-2.p6 @@ -0,0 +1,26 @@ +#!/usr/bin/env raku + +use v6.d; + +sub greatness(@ints, @permutation --> Int:D) { + sum(map({ $_[0] < $_[1] ?? 1 !! 0 }, zip(@ints, @permutation))); +} + +sub maximise-greatness(@ints --> Int:D) { + max(map({ greatness(@ints, $_) }, permutations(@ints).race)); +} + +#| Run test cases +multi sub MAIN('test') { + use Test; + plan 2; + + is maximise-greatness([1, 3, 5, 2, 1, 3, 1]), 4, + 'works for (1, 3, 5, 2, 1, 3, 1)'; + is maximise-greatness([1, 2, 3, 4]), 3, 'works for (1, 2, 3, 4)'; +} + +#| Take user provided list like 1 1 2 2 2 3 +multi sub MAIN(*@ints where @ints.elems ≥ 1) { + say maximise-greatness(@ints); +} diff --git a/challenge-237/barroff/raku/ch_1.nim b/challenge-237/barroff/raku/ch_1.nim new file mode 100644 index 0000000000..834a3b8fc4 --- /dev/null +++ b/challenge-237/barroff/raku/ch_1.nim @@ -0,0 +1,42 @@ +import std/[algorithm, sequtils, times, unittest] + +# run tests with following command: +# nim c -r ch_1.nim + +proc seize_the_day(year, month, wd, dow: int): int = + let + start_date = dateTime(year, Month(month), wd) + week_shift = if weekday(start_date) > WeekDay(dow): 1 else: 2 + + if wd == 1: + if week_shift == 1: + return 0 + else: + return 1 + dow - int(weekday(start_date)) + + let + day_shift = days(int(high(WeekDay)) - int(weekday(start_date)) + (wd - + week_shift) * 7 + dow) + final_day = start_date + day_shift + + return if month(start_date) == month(final_day): int(monthday( + final_day)) else: 0 + +suite "seize the day": + test "(2024, 4, 3, 2)": + let + std = seize_the_day(2024, 4, 3, 2) + + check(std == 16) + + test "(2025, 10, 2, 4)": + let + std = seize_the_day(2025, 10, 2, 4) + + check(std == 9) + + test "(2026, 8, 5, 3)": + let + std = seize_the_day(2026, 8, 5, 3) + + check(std == 0) diff --git a/challenge-237/barroff/raku/ch_2.nim b/challenge-237/barroff/raku/ch_2.nim new file mode 100644 index 0000000000..e6b3dccd89 --- /dev/null +++ b/challenge-237/barroff/raku/ch_2.nim @@ -0,0 +1,29 @@ +import std/[algorithm, sequtils, unittest] + +# run tests with following command: +# nim c -r ch_2.nim + +proc greatness(list1, list2: openArray[int]): int = + result = len(zip(list1, list2).filterIt(it[0] < it[1])) + +proc maximise_greatness(a: openArray[int]): int = + var + sa: seq[int] = sorted(a) + result = greatness(a, sa) + while nextPermutation(sa): + result = max(result, greatness(a, sa)) + +suite "maximise_greatness": + test "(1, 3, 5, 2, 1, 3, 1)": + let + ints = [1, 3, 5, 2, 1, 3, 1] + mg = maximise_greatness(ints) + + check(mg == 4) + + test "(1, 2, 3, 4)": + let + ints = [1, 2, 3, 4] + mg = maximise_greatness(ints) + + check(mg == 3) |
