aboutsummaryrefslogtreecommitdiff
path: root/challenge-258
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-258')
-rwxr-xr-xchallenge-258/archargelod/nim/ch_1.nim22
-rwxr-xr-xchallenge-258/archargelod/nim/ch_2.nim22
2 files changed, 44 insertions, 0 deletions
diff --git a/challenge-258/archargelod/nim/ch_1.nim b/challenge-258/archargelod/nim/ch_1.nim
new file mode 100755
index 0000000000..175af303f2
--- /dev/null
+++ b/challenge-258/archargelod/nim/ch_1.nim
@@ -0,0 +1,22 @@
+#!/usr/bin/env -S nim r -d:release --verbosity:0 --hints:off
+
+proc countEvenDigitsNumbers(numbers: openarray[Natural]): int =
+ for num in numbers:
+ if num < 0: raise newException(ValueError, "Only positive values allowed")
+ if len($num) mod 2 == 0:
+ inc result
+
+when isMainModule:
+ import std/unittest
+
+ const
+ Test = [@[10.Natural, 1, 111, 24, 1000], @[111, 1, 11111], @[2, 8, 1024, 256]]
+ Expected = [3, 0, 1]
+
+ suite "Count Even Digits Number":
+ test "Example 1":
+ check countEvenDigitsNumbers(Test[0]) == Expected[0]
+ test "Example 2":
+ check countEvenDigitsNumbers(Test[1]) == Expected[1]
+ test "Example 3":
+ check countEvenDigitsNumbers(Test[2]) == Expected[2]
diff --git a/challenge-258/archargelod/nim/ch_2.nim b/challenge-258/archargelod/nim/ch_2.nim
new file mode 100755
index 0000000000..d7bbad4430
--- /dev/null
+++ b/challenge-258/archargelod/nim/ch_2.nim
@@ -0,0 +1,22 @@
+#!/usr/bin/env -S nim r -d:release --verbosity:0 --hints:off
+import std/strutils
+
+proc indexSetBitsNumberSum(numbers: openarray[int], setBits: int): int =
+ for ind, num in numbers:
+ if ind.toBin(64).count('1') == setBits:
+ result += num
+
+when isMainModule:
+ import std/unittest
+
+ const
+ Test = [(@[2, 5, 9, 11, 3], 1), (@[2, 5, 9, 11, 3], 2), (@[2, 5, 9, 11, 3], 0)]
+ Expected = [17, 11, 2]
+
+ suite "Sum of Values":
+ test "Example 1":
+ check indexSetBitsNumberSum(Test[0][0], Test[0][1]) == Expected[0]
+ test "Example 2":
+ check indexSetBitsNumberSum(Test[1][0], Test[1][1]) == Expected[1]
+ test "Example 3":
+ check indexSetBitsNumberSum(Test[2][0], Test[2][1]) == Expected[2]