diff options
| author | BarrOff <58253563+BarrOff@users.noreply.github.com> | 2024-05-05 21:56:16 +0200 |
|---|---|---|
| committer | BarrOff <58253563+BarrOff@users.noreply.github.com> | 2024-05-05 21:56:16 +0200 |
| commit | 0278564217adba916238fec1b953397542b008ec (patch) | |
| tree | 95b1461145018cbf95993fdb8a2576af06c5ec1d /challenge-267/barroff/nim | |
| parent | 7573054b72d6484f0b3b8083fa2fbe0232439e35 (diff) | |
| download | perlweeklychallenge-club-0278564217adba916238fec1b953397542b008ec.tar.gz perlweeklychallenge-club-0278564217adba916238fec1b953397542b008ec.tar.bz2 perlweeklychallenge-club-0278564217adba916238fec1b953397542b008ec.zip | |
feat: add solutions for challenge 267 from BarrOff
Diffstat (limited to 'challenge-267/barroff/nim')
| -rw-r--r-- | challenge-267/barroff/nim/ch_1.nim | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/challenge-267/barroff/nim/ch_1.nim b/challenge-267/barroff/nim/ch_1.nim new file mode 100644 index 0000000000..fd1af785b1 --- /dev/null +++ b/challenge-267/barroff/nim/ch_1.nim @@ -0,0 +1,25 @@ +import std/unittest + +from std/sequtils import countIt + +# run tests with following command: +# nim c -r ch_1.nim + +func product_sign[T: SomeInteger](ints: openArray[T]): int = + if 0 in ints: + return 0 + let + negativeCount = countIt(ints, it < 0) + if negativeCount mod 2 == 0: + return 1 + return -1 + +suite "product sign": + test "[-1, -2, -3, -4, 3, 2, 1]": + check(product_sign([-1, -2, -3, -4, 3, 2, 1]) == 1) + + test "[1, 2, 0, -2, -1]": + check(product_sign([1, 2, 0, -2, -1]) == 0) + + test "[-1, -1, 1, -1, 2]": + check(product_sign([-1, -1, 1, -1, 2]) == -1) |
