Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def getCombinations(sortSub, size):
- """returns list with all ordered combinations of len(size)
- see itertools in python documation for more info"""
- # Note: itertools returns a tuple. Tuple not returned here
- # b/c yield function is not used
- combinations = []
- # Create tuple holding all letters of sortSub
- pool = tuple(sortSub)
- n = len(pool)
- # do nothing if len(pool) is less than the length of how long you
- # want the return tuples
- if size > n:
- return
- indices = range(size)
- ## # Put first combo into list
- ## # This doesn't have to be outside the while loop...
- ## newCombo = ""
- ## for x in indices:
- ## newCombo += pool[x]
- ## combinations.append(newCombo)
- ## newCombo = ""
- while True:
- # add string to combinations
- # don't know why this was outside the while loop in the
- # itertools.combinations code
- newCombo = ""
- for x in indices:
- newCombo += pool[x]
- combinations.append(newCombo)
- for i in reversed(range(size)):
- # loop does not iterate i until indices[i] == i + n - size
- # I.e., the last index increases until it will index the last
- # element of pool of length n. Then, the next to last index
- # will increase in a similar fashion, until it is 1 less
- # than the index that comes after it
- # NOTE: if this loop hits break, the next time it runs it will
- # start at the beginning
- print 'i =',i,'. indices =',indices,'. Length =',n,'. Iterate i when indices[',i,'] ==,',i + n - size
- if indices[i] != i + n - size:
- print "Breaking out of loop. i starts over at",size - 1,"after rest of code runs."
- break
- print 'Iterating i!!!'
- # else: belongs to for loop, e.g., runs when for loop terminates
- # but NOT after for loop hits break
- else:
- return combinations
- # although for loop hit break, i is still in play
- # indices[i] += 1 iterates last indice in set until
- # indices[i] == i + n - size
- indices[i] += 1
- # For loop entered only when i < max(reversed(range(size)))
- # e.g., after first for loop iterates i
- # after first loop iterates, the next to last value in indices
- # also iterates after indices[i] += 1 above
- for j in range(i+1, size):
- # range(2,3) == [2]; range(3,4) == [3]; range(3,5) == [3,4]
- # last value in indices that increased as far as it could
- # is reset to one more than the the value next to it
- print 'i+1 =',i+1,"size =",size,"range(i+1, size)=",range(i+1, size),"indices =",indices
- print 'changing',indices,'to...'
- indices[j] = indices[j-1] + 1
- print indices
- def combinations(iterable, r):
- # combinations('ABCD', 2) --> AB AC AD BC BD CD
- # combinations(range(4), 3) --> 012 013 023 123
- pool = tuple(iterable)
- n = len(pool)
- if r > n:
- return
- indices = range(r)
- print "indices1 range:",range(r)
- yield tuple(pool[i] for i in indices)
- while True:
- for i in reversed(range(r)):
- print "Top: i =", i,". n =", n,". r =",r,"indices =",indices
- print "If/break: indices[i] =",indices[i],". i + n - r =", i+n-r
- if indices[i] != i + n - r:
- print "break out"
- break
- else:
- print "returning"
- return
- indices[i] += 1
- print "Added: indices[i] += 1:",indices,"where i =",i
- print "entering for loop. range(i+1, r) =",range(i+1,r),"where i+1=",i+1,"and r =",r
- for j in range(i+1, r):
- print "for loop:"
- print "j =",j,". indices =",indices,". indices[j] =",indices[j],". indices[j-1] + 1 =",indices[j-1] +1
- indices[j] = indices[j-1] + 1
- print "new indices[j] =",indices[j],". indices =",indices
- yield tuple(pool[i] for i in indices)
- if __name__ == '__main__':
- test = "werlkj5"
- print "I will perform the test on,",test,", with size 3!"
- testSort = getCombinations(test, 3)
- print "Result:",testSort
- print "I will try with itertools.combinations:"
- iterList = []
- for i in itertools.combinations(test, 3):
- newCombo = ""
- for x in i:
- newCombo += x
- iterList.append(newCombo)
- print iterList
- ## print "I will try with combinations 'Equivalent to:' code:"
- ## iterList = []
- ## for i in combinations(test, 3):
- ## newCombo = ""
- ## for x in i:
- ## newCombo += x
- ## iterList.append(newCombo)
- ## print iterList
Advertisement
Add Comment
Please, Sign In to add comment