Skip to content

Instantly share code, notes, and snippets.

@hotoo
Created July 30, 2010 10:31
Show Gist options
  • Select an option

  • Save hotoo/500289 to your computer and use it in GitHub Desktop.

Select an option

Save hotoo/500289 to your computer and use it in GitHub Desktop.
char a2 = { d }
char a3 = {e, f, g }
for ( i=0; i<3; i++ )
{
for ( j=0; j<1; j++ )
{
for ( k=0; k<3; k++ )
{
print ( a1[i], a2[j], a3[k] );
}
}
}
-module(rec).
-export([go/0]).
go() ->
list([[a,b,c],[d, h], [e,f,g]]).
list([[H|T], A2, A3]) ->
list([H, A2, A3]),
list([T, A2, A3]);
list([H, [H1|T1], A3]) ->
list([H, H1, A3]),
list([H, T1, A3]);
list([H, H1, [H2|T2]]) ->
list([H, H1, H2]),
list([H, H1, T2]);
list([H, H1, H2]) when is_atom(H), is_atom(H1), is_atom(H2) ->
io:format(">>> ~p, ~p, ~p~n", [H, H1, H2]);
list([_, _, _]) ->
ok.
function test() {
var result=buildRow(data[0],1);
for (var i=0; i<result.length; i++) {
alert(result[i]);
}
}
function buildRow(tempResult,currentRow) {
var result=[];
for (var i=0; i< tempResult.length ; i++ ) {
for (var j=0; j<data[currentRow].length; j++) {
result.push( tempResult[i] + data[currentRow][j] );
}
}
if (currentRow == data.length -1 ) {
return result;
} else {
return buildRow(result, currentRow+1);
}
}
test();
#!/usr/bin/env python3
# vim:fileencoding=utf-8
def main(L):
iters = list(map(iter, L))
curpos = [0] * len(L)
ret = [0] * len(L)
for i in range(len(L)):
ret[i] = iters[i]._next_()
yield ret
i = 0
while True:
try:
while True:
ret[i] = iters[i].__next__()
curpos[i] += 1
i = 0
yield ret
except StopIteration:
i = 0
try:
while True:
if curpos[i] == len(L[i])-1:
i += 1
continue
break
except IndexError:
raise StopIteration
for x in range(i):
curpos[x] = 0
iters[x] = iter(L[x])
ret[x] = iters[x].__next__()
if __name_ == '__main__':
for i in main(['a', 'abc', 'de', '123']):
print(i)
@hotoo
Copy link
Author

hotoo commented Jul 30, 2010

我们现在有一个不规则的二维数组,如何能穷举出所有可能的组合呢?
例如 [[a,b,c],[d],[e,f,g]]
总共有 9 种组合方式:
[
[a,d,e],
[a,d,f],
[a,d,g],
[b,d,e],
[b,d,f],
[b,d,g],
[c,d,e],
[c,d,f],
[c,d,g]
]
请问如何通过算法把他们穷举出来?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment