File tree Expand file tree Collapse file tree 2 files changed +16
-3
lines changed
Expand file tree Collapse file tree 2 files changed +16
-3
lines changed Original file line number Diff line number Diff 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 (
Original file line number Diff line number Diff line change @@ -367,3 +367,13 @@ test('random', function(a)
367367
368368 a .equal (array .random ({ ' a' }), ' a' )
369369end )
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 )
You can’t perform that action at this time.
0 commit comments