Skip to content

Commit 40457b2

Browse files
committed
add unit tests to permutation method
1 parent 5993210 commit 40457b2

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/array/init.lua

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -577,17 +577,20 @@ array = {
577577
return obj[math.random(#obj)]
578578
end,
579579

580+
-- Creates a new table returning all permutations of length of the elements of the given table
581+
-- @param obj {table}
582+
-- @return {table}
580583
permutation = function(obj)
581584
if #obj == 1 then
582585
return { obj }
583586
end
584587

585588
local output = {}
586-
local partialList = array.permutation(array.slice(obj, 2))
589+
local partial_list = array.permutation(array.slice(obj, 2))
587590
local first = { obj[1] }
588591

589-
for i=1, #partialList do
590-
local partial = partialList[i]
592+
for i=1, #partial_list do
593+
local partial = partial_list[i]
591594

592595
for j=1, #partial+1 do
593596
local merged = array.concat(

test.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,3 +367,13 @@ test('random', function(a)
367367

368368
a.equal(array.random({ 'a' }), 'a')
369369
end)
370+
371+
test('permutation', function(a)
372+
a.deep_equal(
373+
array.permutation({ 'lua', 'javascript' }),
374+
{
375+
{ 'lua', 'javascript' },
376+
{ 'javascript', 'lua' },
377+
}
378+
)
379+
end)

0 commit comments

Comments
 (0)