aboutsummaryrefslogtreecommitdiff
path: root/challenge-012
diff options
context:
space:
mode:
authorArchargelod <archargelod@gmail.com>2024-02-21 01:33:32 +0800
committerArchargelod <archargelod@gmail.com>2024-02-21 01:33:32 +0800
commit77f7f91c73773aa6cfd2713dea32c705a1971b58 (patch)
tree368b987ea819a1083930390e2cef19151827c4d6 /challenge-012
parentddd42e0db3017a5ec3108d09efba477f19e7f04b (diff)
downloadperlweeklychallenge-club-77f7f91c73773aa6cfd2713dea32c705a1971b58.tar.gz
perlweeklychallenge-club-77f7f91c73773aa6cfd2713dea32c705a1971b58.tar.bz2
perlweeklychallenge-club-77f7f91c73773aa6cfd2713dea32c705a1971b58.zip
Weeks 5-13, 257 in Nim
Diffstat (limited to 'challenge-012')
-rw-r--r--challenge-012/archargelod/README1
-rwxr-xr-xchallenge-012/archargelod/nim/ch_1.nim33
-rwxr-xr-xchallenge-012/archargelod/nim/ch_2.nim43
3 files changed, 77 insertions, 0 deletions
diff --git a/challenge-012/archargelod/README b/challenge-012/archargelod/README
new file mode 100644
index 0000000000..6cd57e1074
--- /dev/null
+++ b/challenge-012/archargelod/README
@@ -0,0 +1 @@
+Solution by archargelod
diff --git a/challenge-012/archargelod/nim/ch_1.nim b/challenge-012/archargelod/nim/ch_1.nim
new file mode 100755
index 0000000000..c64a70a11e
--- /dev/null
+++ b/challenge-012/archargelod/nim/ch_1.nim
@@ -0,0 +1,33 @@
+#!/usr/bin/env -S nim r -d:release --verbosity:0 --hints:off
+import std/math
+
+const Primes = [
+ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79,
+ 83, 89, 97
+]
+
+proc isPrime(n: int): bool =
+ if n == 2 or n == 3:
+ return true
+ elif n mod 3 == 0 or n mod 2 == 0:
+ return false
+
+ for i in 5 .. sqrt(n.float).int:
+ if n mod i == 0:
+ return false
+ true
+
+proc smallestNonPrimeEuclidNumber(): int =
+ var product = 1
+ for i, prime in Primes:
+ product = product * prime
+ let euclidNumber = product + 1
+ if not euclidNumber.isPrime:
+ return euclidNumber
+
+when isMainModule:
+ import std/unittest
+
+ suite "Smallest non-prime Euclid number":
+ test "smalles nonprime euclid number is 30031":
+ check smallestNonPrimeEuclidNumber() == 30031
diff --git a/challenge-012/archargelod/nim/ch_2.nim b/challenge-012/archargelod/nim/ch_2.nim
new file mode 100755
index 0000000000..ed6b057a67
--- /dev/null
+++ b/challenge-012/archargelod/nim/ch_2.nim
@@ -0,0 +1,43 @@
+#!/usr/bin/env -S nim r -d:release --verbosity:0 --hints:off
+import std/[parseutils, strformat, strutils]
+
+proc commonPath(paths: openarray[string], sep = '/'): string =
+ var stack: seq[string]
+ for pathInd, path in paths:
+ if path[0] != sep:
+ raise newException(ValueError, &"paths should start with '{sep}'")
+
+ var depth = 0
+ var index = 1
+ while index <= path.high:
+ let dirLength = path.skipUntil({sep}, index)
+
+ if dirLength < 1: # treat multiple separators as one (how it works on linux)
+ inc index
+ continue
+
+ let dirName = path[index ..< index + dirLength]
+
+ if pathInd == 0:
+ stack.add dirName
+ elif depth > stack.high or stack[depth] != dirName:
+ stack.setLen(depth)
+ break
+ elif depth == stack.high:
+ break
+
+ inc depth
+ index += dirLength + 1
+
+ sep & stack.join($sep)
+
+when isMainModule:
+ import std/unittest
+
+ const
+ TestPaths = ["/a/b/c/d", "/a/b/cd", "/a/b/cc", "/a/b/c/d/e"]
+ Expected = "/a/b"
+
+ suite "Common directory":
+ test "Example 1":
+ check commonPath(TestPaths) == Expected