Skip to content

Instantly share code, notes, and snippets.

@huklee
Last active March 6, 2017 12:02
Show Gist options
  • Save huklee/4ff6f25bd5984600dbb654f798284f70 to your computer and use it in GitHub Desktop.
Save huklee/4ff6f25bd5984600dbb654f798284f70 to your computer and use it in GitHub Desktop.
print all combinations of 2D string array
# author : huklee
# source : JTD 2017-03-05
# print all combinations of string 2D array
def printComb(a, sl):
s = ""
for i in range(len(a)):
s += sl[i][a[i]]
return s
def increment(a, sl):
for i in range(len(a))[::-1]:
a[i] += 1
if a[i] == len(sl[i]):
a[i] = 0
continue
break
return a
def printAll(sl):
a = [0 for x in sl]
endCond = [len(x) - 1 for x in sl]
while True:
# print each combination
print(printComb(a, sl))
if a == endCond:
break
# increment the combination
a = increment(a, sl)
d = [["A", "B", "C"], ["X", "Y", "Z"], ["1", "2", "3"]]
printAll(d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment