Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.
Become a Premium Member and unlock a new, free course in leading technologies each month.
Add your voice to the tech community where 5M+ people just like you are talking about what matters.
import itertools
row = ['bbContag', 'bbED', 'bbENN_MN', 'bbLPI', 'bbLSI']
# If you want to do also permutations from using not all
# values in the list, then you can add the second argument
# of the itertools.permutations(row, n) and loop for n
# from 1 to n inclusively.
for n in range(1, len(row)+1):
print '\nPermutations of the length', n
print '-' * 50
for t in itertools.permutations(row, n):
print '+'.join(t)
result.txt
Sub Combinatrix()
'Forms all the combinations of one fixed value with varying numbers of other values
'Uses binary counting method
Dim i As Long, j As Long, k As Long, N As Long, NN As Long
Dim v As Variant, vInputs As Variant, vResults As Variant
Dim s As String
N = Application.CountA(Selection.Value)
ReDim vInputs(1 To N)
For i = 1 To N
If Selection.Cells(i) <> "" Then
j = j + 1
vInputs(j) = Selection.Cells(i)
End If
Next
ReDim Preserve vInputs(1 To j)
NN = 2 ^ (N - 1) - 1
ReDim vResults(1 To NN, 1 To 1)
For i = 1 To NN
vResults(i, 1) = vInputs(1)
For j = 2 To N
k = IIf((i Mod 2 ^ (j - 1)) >= 2 ^ (j - 2), 1, 0)
If k = 1 Then vResults(i, 1) = vResults(i, 1) & "+" & vInputs(j)
Next
Next
Selection.Offset(1, 0).Resize(NN, 1).Value = vResults
End Sub
Sub Combinatrix()
'Forms all the combinations of one fixed value with varying numbers of other values
'Uses binary counting method
Dim i As Long, iCol As Long, j As Long, k As Long, N As Long, NN As Long
Dim v As Variant, vInputs As Variant, vResults As Variant
Dim rw As Range, rg As Range
Dim s As String
Set rg = Selection
For Each rw In Selection.Rows
N = Application.CountA(rw)
ReDim vInputs(1 To N)
j = 0
For i = 1 To N
If rw.Cells(i) <> "" Then
j = j + 1
vInputs(j) = rw.Cells(i)
End If
Next
ReDim Preserve vInputs(1 To j)
NN = 2 ^ (N - 1) - 1
ReDim vResults(1 To NN, 1 To 1)
For i = 1 To NN
vResults(i, 1) = vInputs(1)
For j = 2 To N
k = IIf((i Mod 2 ^ (j - 1)) >= 2 ^ (j - 2), 1, 0)
If k = 1 Then vResults(i, 1) = vResults(i, 1) & "+" & vInputs(j)
Next
Next
iCol = iCol + 1 'Put the results on Sheet2 in adjacent columns
Worksheets("Sheet2").Cells(1, iCol).Resize(NN, 1).Value = vResults
Next
End Sub
If you are experiencing a similar issue, please ask a related question
Join the community of 500,000 technology professionals and ask your questions.