diff options
| author | Archargelod <archargelod@gmail.com> | 2024-03-31 21:26:06 +0800 |
|---|---|---|
| committer | Archargelod <archargelod@gmail.com> | 2024-03-31 21:26:06 +0800 |
| commit | 7e07268564c8b39d4908e8123a662cd13355bb7f (patch) | |
| tree | 8ae6473e7e3c7cf0e0c17d48af123b66eced9d3f | |
| parent | fdeee3349654c610d6ed2f4aca69ef8985e3c094 (diff) | |
| download | perlweeklychallenge-club-7e07268564c8b39d4908e8123a662cd13355bb7f.tar.gz perlweeklychallenge-club-7e07268564c8b39d4908e8123a662cd13355bb7f.tar.bz2 perlweeklychallenge-club-7e07268564c8b39d4908e8123a662cd13355bb7f.zip | |
weeks 48, 262 in Nim
| -rw-r--r-- | challenge-048/archargelod/README | 1 | ||||
| -rwxr-xr-x | challenge-048/archargelod/nim/ch_1.nim | 19 | ||||
| -rwxr-xr-x | challenge-048/archargelod/nim/ch_2.nim | 33 | ||||
| -rwxr-xr-x | challenge-262/archargelod/nim/ch_1.nim | 46 | ||||
| -rwxr-xr-x | challenge-262/archargelod/nim/ch_2.nim | 25 |
5 files changed, 124 insertions, 0 deletions
diff --git a/challenge-048/archargelod/README b/challenge-048/archargelod/README new file mode 100644 index 0000000000..6cd57e1074 --- /dev/null +++ b/challenge-048/archargelod/README @@ -0,0 +1 @@ +Solution by archargelod diff --git a/challenge-048/archargelod/nim/ch_1.nim b/challenge-048/archargelod/nim/ch_1.nim new file mode 100755 index 0000000000..f04fe841ab --- /dev/null +++ b/challenge-048/archargelod/nim/ch_1.nim @@ -0,0 +1,19 @@ +#!/usr/bin/env -S nim r -d:release --verbosity:0 --hints:off +import std/bitops + +proc highestOneBit(n: int): int = + if n == 0: return 0 + 1 shl (sizeof(n) * 8 - countLeadingZeroBits(n) - 1) + +proc josephusSurvivor(n: int): int = + not(highestOneBit(n*2)) and ((n shl 1) or 1) +when isMainModule: + import std/unittest + + const + Test = 50 + Expected = 37 + + suite "Survivor": + test "50 people, k = 2": + check josephusSurvivor(Test) == Expected diff --git a/challenge-048/archargelod/nim/ch_2.nim b/challenge-048/archargelod/nim/ch_2.nim new file mode 100755 index 0000000000..f7eddd4baf --- /dev/null +++ b/challenge-048/archargelod/nim/ch_2.nim @@ -0,0 +1,33 @@ +#!/usr/bin/env -S nim r -d:release --verbosity:0 --hints:off +import std/[times, strformat] + +proc asInt(c: char): int = + assert c in '0'..'9' + c.ord - '0'.ord + +proc palindromeDates(yearRange: Slice[int]) = + assert yearRange.a <= yearRange.b + assert yearRange.b < 10000 + + for year in yearRange: + let yearString = &"{year:04}" + let day = yearString[^3].asInt * 10 + yearString[^4].asInt + if day notin 1..31: continue + + let month = yearString[^1].asInt * 10 + yearString[^2].asInt + if month notin 1..12: continue + + let formatted = $month & $day & yearString + + try: + let date = formatted.parse("MMddyyyy") + echo "There is a palindrome date in year ", $year, ": ", formatted, + " or ", date.format("MMM-dd-yyyy") + except TimeParseError: + discard + +proc main = + palindromeDates(2000 .. 2999) + +when isMainModule: + main() diff --git a/challenge-262/archargelod/nim/ch_1.nim b/challenge-262/archargelod/nim/ch_1.nim new file mode 100755 index 0000000000..e12e72c333 --- /dev/null +++ b/challenge-262/archargelod/nim/ch_1.nim @@ -0,0 +1,46 @@ +#!/usr/bin/env -S nim r -d:release --verbosity:0 --hints:off + +type + NumberSign = enum + Positive + Negative + Zero + +proc maxSign(numbers: openarray[int]): (int, NumberSign) = + var + positiveCnt, negativeCnt: int + + for num in numbers: + if num > 0: + inc positiveCnt + elif num < 0: + inc negativeCnt + + if positiveCnt > negativeCnt: + (positiveCnt, Positive) + else: + (negativeCnt, Negative) + + +when isMainModule: + import std/unittest + + const + Test = [ + @[-3, 1, 2, -1, 3, -2, 4], + @[-1, -2, -3, 1], + @[1,2], + ] + Expected = [ + (4, Positive), + (3, Negative), + (2, Positive), + ] + + suite "Max Positive Negative": + test "Example 1": + check maxSign(Test[0]) == Expected[0] + test "Example 2": + check maxSign(Test[1]) == Expected[1] + test "Example 3": + check maxSign(Test[2]) == Expected[2] diff --git a/challenge-262/archargelod/nim/ch_2.nim b/challenge-262/archargelod/nim/ch_2.nim new file mode 100755 index 0000000000..de411fc17f --- /dev/null +++ b/challenge-262/archargelod/nim/ch_2.nim @@ -0,0 +1,25 @@ +#!/usr/bin/env -S nim r -d:release --verbosity:0 --hints:off + +proc equalIndexesProductDivisibleByKCount(numbers: openarray[int], k: int): int = + doAssert numbers.len > 1 + for i, val1 in numbers: + for j, val2 in numbers[i+1..^1]: + let j = i + 1 + j + if (val1 == val2) and (i*j mod k == 0): + inc result + +when isMainModule: + import std/unittest + + const + Test = [ + (@[3,1,2,2,2,1,3], 2), + (@[1,2,3], 1), + ] + Expected = [4, 0] + + suite "Count Equal Divisible": + test "Example 1": + check equalIndexesProductDivisibleByKCount(Test[0][0], Test[0][1]) == Expected[0] + test "Example 2": + check equalIndexesProductDivisibleByKCount(Test[1][0], Test[1][1]) == Expected[1] |
