diff options
author | Yonaba <roland.yonaba@gmail.com> | 2019-01-29 03:46:24 +0300 |
---|---|---|
committer | Yonaba <roland.yonaba@gmail.com> | 2019-01-29 03:46:24 +0300 |
commit | e1fbe125870585f8bdccfddf8df9f5edb4734904 (patch) | |
tree | baf45d22f176ed9d08418f81b89360c8a25af6ec /moses.lua | |
parent | c59d157f102a8b8c0a387cb236dd6906a33db448 (diff) |
Fixed powerset (Fixes #64)
Updated powerset spec
Fixed spec
Diffstat (limited to 'moses.lua')
-rw-r--r-- | moses.lua | 16 |
1 files changed, 9 insertions, 7 deletions
@@ -1578,20 +1578,22 @@ function M.rep(value, n) end --- Returns the powerset of array values. For instance, when given the set {1,2,3}, --- returns `{{1},{2},{3},{1,2},{2,3},{1,2,3}}`. +-- returns `{{},{1},{2},{3},{1,2},{2,3},{1,3},{1,2,3}}`. -- @name powerset -- @param array an array -- @return an array function M.powerset(array) local n = #array - if n == 0 then return {{}} end - local t = {} - for l = 1, n do - for s = 1, n-l+1 do - t[#t+1] = M.slice(array,s,s+l-1) + local powerset = {} + for i, v in ipairs(array) do + for j = 1, #powerset do + local set = powerset[j] + t_insert(powerset, M.push(M.slice(set), v)) end + t_insert(powerset, {v}) end - return t + t_insert(powerset, {}) + return powerset end --- Iterator returning partitions of an array. It returns arrays of length `n` |