I am merely wanting to output the contents of a set
(because I have to use this cursed and inflexible type), but every time I go to do it or loop through and print each element, I get the last one instead of the rest. Here's my entire code:
def factorial(n):
r = 1
for i in range(1, n + 1):
r *= i
return r
def nCr(n, r):
return factorial(n) / (factorial(r) * factorial(n - r))
def combinations(s, tokens):
maxcombos = int(nCr(len(s), tokens))
for index in range(maxcombos):
token_copy = tokens
combo = []
for val in range(1, len(s) + 1):
if not token_copy:
break
thresh = nCr(len(s) - val, token_copy - 1)
if index < thresh:
combo.append(s.pop())
token_copy -= 1
else:
index -= thresh
yield tuple(combo)
def powerset(s):
pset = set()
for num_tokens in range(1, len(s)):
for combo in combinations(s, num_tokens):
pset.add(combo)
pset.update(s)
#pset.update(tuple())
return pset
if __name__ == '__main__':
s = set([1,2,23])
p = powerset(s)
msg = "Initial set: set(["
for (i, item) in enumerate(s):
msg += str(item) + ", "
msg += "])"
print(msg)
#print(powerset())
I merely want each one to appear in between those ([])
like a normal set does. Is there a way to simply output the whole variable with its contents? Thanks!
Change the top of your combinations definition to the following:
This creates a new set which will not be altered in the combinations function. The reason it was being altered before is because the local variable in combinations was a reference to the same object outside the function. If two names reference the same object, mutating one will affect the other.
In main you want the following:
Your
powerset
function actually alters its input. Incombinations
, you uses.pop()
. That's almost certainly wrong, and would explain why its contents have changed when you try to print it.The simplest fix is to replace your
powerset
function with the recipe from theitertools
documentation: http://docs.python.org/2/library/itertools.html#recipes