aboutsummaryrefslogtreecommitdiff
path: root/challenge-252/barroff/nim/ch_2.nim
blob: a512208a2e15c047edc805cb601700e12de59129 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import std/unittest

from std/algorithm import sort
from std/sequtils import map

# run tests with following command:
# nim c -r ch_2.nim

proc unique_sum_zero(n: Natural): seq[int] =
  result = newSeqOfCap[int](n)
  for i in 1..n div 2:
    add(result, [i, -i])
  if n mod 2 == 1:
    add(result, 0)
  sort(result)

suite "unique sum zero":
  test "5":
    check(unique_sum_zero(5) == @[-2, -1, 0, 1, 2])

  test "3":
    check(unique_sum_zero(3) == @[-1, 0, 1])

  test "1":
    check(unique_sum_zero(1) == @[0])

  test "0":
    check(unique_sum_zero(0) == newSeqOfCap[int](0))