Asix3

MIT Pset6 handSubsets v.2

Feb 2nd, 2012
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.76 KB | None | 0 0
  1. def getCombinations(sortSub, size):
  2.     """returns list with all ordered combinations of len(size)
  3.    see itertools in python documation for more info"""
  4.     # Note: itertools returns a tuple. Tuple not returned here
  5.     # b/c yield function is not used
  6.     combinations = []
  7.     # Create tuple holding all letters of sortSub
  8.     pool = tuple(sortSub)
  9.    
  10.     n = len(pool)
  11.  
  12.     # do nothing if len(pool) is less than the length of how long you
  13.     # want the return tuples
  14.     if size > n:
  15.         return
  16.  
  17.     indices = range(size)
  18. ##    # Put first combo into list
  19. ##    # This doesn't have to be outside the while loop...
  20. ##    newCombo = ""
  21. ##    for x in indices:
  22. ##        newCombo += pool[x]
  23. ##    combinations.append(newCombo)
  24. ##    newCombo = ""
  25.     while True:
  26.         # add string to combinations
  27.         # don't know why this was outside the while loop in the
  28.         # itertools.combinations code
  29.         newCombo = ""
  30.         for x in indices:
  31.             newCombo += pool[x]
  32.         combinations.append(newCombo)
  33.         for i in reversed(range(size)):
  34.             # loop does not iterate i until indices[i] == i + n - size
  35.             # I.e., the last index increases until it will index the last
  36.             # element of pool of length n. Then, the next to last index
  37.             # will increase in a similar fashion, until it is 1 less
  38.             # than the index that comes after it
  39.             # NOTE: if this loop hits break, the next time it runs it will
  40.             # start at the beginning
  41.             print 'i =',i,'. indices =',indices,'. Length =',n,'. Iterate i when indices[',i,'] ==,',i + n - size
  42.             if indices[i] != i + n - size:
  43.                 print "Breaking out of loop. i starts over at",size - 1,"after rest of code runs."
  44.                 break
  45.             print 'Iterating i!!!'
  46.            
  47.         # else: belongs to for loop, e.g., runs when for loop terminates
  48.         # but NOT after for loop hits break
  49.         else:
  50.             return combinations
  51.        
  52.         # although for loop hit break, i is still in play
  53.         # indices[i] += 1 iterates last indice in set until
  54.         # indices[i] == i + n - size
  55.         indices[i] += 1
  56.  
  57.         # For loop entered only when i < max(reversed(range(size)))
  58.         # e.g., after first for loop iterates i
  59.         # after first loop iterates, the next to last value in indices
  60.         # also iterates after indices[i] += 1 above
  61.         for j in range(i+1, size):
  62.             # range(2,3) == [2]; range(3,4) == [3]; range(3,5) == [3,4]
  63.             # last value in indices that increased as far as it could
  64.             # is reset to one more than the the value next to it
  65.             print 'i+1 =',i+1,"size =",size,"range(i+1, size)=",range(i+1, size),"indices =",indices
  66.             print 'changing',indices,'to...'
  67.             indices[j] = indices[j-1] + 1
  68.             print indices
  69.  
  70. def combinations(iterable, r):
  71.     # combinations('ABCD', 2) --> AB AC AD BC BD CD
  72.     # combinations(range(4), 3) --> 012 013 023 123
  73.     pool = tuple(iterable)
  74.     n = len(pool)
  75.     if r > n:
  76.         return
  77.     indices = range(r)
  78.     print "indices1 range:",range(r)
  79.     yield tuple(pool[i] for i in indices)
  80.     while True:
  81.         for i in reversed(range(r)):
  82.             print "Top: i =", i,". n =", n,". r =",r,"indices =",indices
  83.             print "If/break: indices[i] =",indices[i],". i + n - r =", i+n-r
  84.             if indices[i] != i + n - r:
  85.                 print "break out"
  86.                 break
  87.         else:
  88.             print "returning"
  89.             return
  90.         indices[i] += 1
  91.         print "Added: indices[i] += 1:",indices,"where i =",i
  92.         print "entering for loop. range(i+1, r) =",range(i+1,r),"where i+1=",i+1,"and r =",r
  93.         for j in range(i+1, r):
  94.             print "for loop:"
  95.             print "j =",j,". indices =",indices,". indices[j] =",indices[j],". indices[j-1] + 1 =",indices[j-1] +1
  96.             indices[j] = indices[j-1] + 1
  97.             print "new indices[j] =",indices[j],". indices =",indices
  98.         yield tuple(pool[i] for i in indices)
  99.  
  100.  
  101. if __name__ == '__main__':
  102.     test = "werlkj5"
  103.     print "I will perform the test on,",test,", with size 3!"
  104.     testSort = getCombinations(test, 3)
  105.     print "Result:",testSort
  106.     print "I will try with itertools.combinations:"
  107.     iterList = []
  108.     for i in itertools.combinations(test, 3):
  109.         newCombo = ""
  110.         for x in i:
  111.             newCombo += x
  112.         iterList.append(newCombo)
  113.     print iterList
  114. ##    print "I will try with combinations 'Equivalent to:' code:"
  115. ##    iterList = []
  116. ##    for i in combinations(test, 3):
  117. ##        newCombo = ""
  118. ##        for x in i:
  119. ##            newCombo += x
  120. ##        iterList.append(newCombo)
  121. ##    print iterList
Advertisement
Add Comment
Please, Sign In to add comment