From 959b69908ef1a78d05e5ddfa3fbcbb31fd62d3ed Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Sun, 8 Oct 2023 23:44:35 +0100 Subject: - Added solutions by Matthew Neleigh. - Added solutions by Robbie Hatley. - Added solutions by BarrOff. - Added solutions by Jan Krnavek. - Added solutions by Bruce Gray. --- challenge-237/barroff/nim/ch_1.nim | 42 +++++++++++++++++++++++++++++++++++++ challenge-237/barroff/nim/ch_2.nim | 29 +++++++++++++++++++++++++ challenge-237/barroff/raku/ch_1.nim | 42 ------------------------------------- challenge-237/barroff/raku/ch_2.nim | 29 ------------------------- 4 files changed, 71 insertions(+), 71 deletions(-) create mode 100644 challenge-237/barroff/nim/ch_1.nim create mode 100644 challenge-237/barroff/nim/ch_2.nim delete mode 100644 challenge-237/barroff/raku/ch_1.nim delete mode 100644 challenge-237/barroff/raku/ch_2.nim (limited to 'challenge-237') diff --git a/challenge-237/barroff/nim/ch_1.nim b/challenge-237/barroff/nim/ch_1.nim new file mode 100644 index 0000000000..834a3b8fc4 --- /dev/null +++ b/challenge-237/barroff/nim/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/nim/ch_2.nim b/challenge-237/barroff/nim/ch_2.nim new file mode 100644 index 0000000000..e6b3dccd89 --- /dev/null +++ b/challenge-237/barroff/nim/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) diff --git a/challenge-237/barroff/raku/ch_1.nim b/challenge-237/barroff/raku/ch_1.nim deleted file mode 100644 index 834a3b8fc4..0000000000 --- a/challenge-237/barroff/raku/ch_1.nim +++ /dev/null @@ -1,42 +0,0 @@ -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 deleted file mode 100644 index e6b3dccd89..0000000000 --- a/challenge-237/barroff/raku/ch_2.nim +++ /dev/null @@ -1,29 +0,0 @@ -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) -- cgit