product more robust against empty input
This commit is contained in:
parent
f070edb8b9
commit
e01fb17d02
2 changed files with 6 additions and 0 deletions
|
|
@ -148,6 +148,7 @@ proc product*[T](x: openarray[seq[T]]): seq[seq[T]] =
|
||||||
var next: seq[T] = @[]
|
var next: seq[T] = @[]
|
||||||
next.setLen(x.len)
|
next.setLen(x.len)
|
||||||
for i in 0..(x.len-1):
|
for i in 0..(x.len-1):
|
||||||
|
if len(x[i]) == 0: return
|
||||||
initial[i] = len(x[i])-1
|
initial[i] = len(x[i])-1
|
||||||
indexes = initial
|
indexes = initial
|
||||||
while true:
|
while true:
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,11 @@
|
||||||
import unittest
|
import unittest
|
||||||
|
import algorithm
|
||||||
|
|
||||||
suite "product":
|
suite "product":
|
||||||
|
test "empty input":
|
||||||
|
check product[int](newSeq[seq[int]]()) == newSeq[seq[int]]()
|
||||||
|
test "bit more empty input":
|
||||||
|
check product[int](@[newSeq[int](), @[], @[]]) == newSeq[seq[int]]()
|
||||||
test "a simple case of one element":
|
test "a simple case of one element":
|
||||||
check product(@[@[1,2]]) == @[@[1,2]]
|
check product(@[@[1,2]]) == @[@[1,2]]
|
||||||
test "two elements":
|
test "two elements":
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue