Skip to content

Instantly share code, notes, and snippets.

@nejni-marji
Last active August 29, 2015 14:16
Show Gist options
  • Save nejni-marji/751dd4f2577e1fae75e1 to your computer and use it in GitHub Desktop.
Save nejni-marji/751dd4f2577e1fae75e1 to your computer and use it in GitHub Desktop.
Ticket Problem
#!/usr/bin/python
import itertools
tick = range(1,25)
totals = [14, 16, 17, 18, 21, 22, 23, 24, 26, 29, 43, 47]
n = 2
# {{{ Making every permutation, skipping duplicates
perms = []
start = list(itertools.permutations(tick, r=n))
for i in start:
j = sorted(i)
if not j in perms:
perms.append(j)
# }}}
# {{{ Getting rid of lists that have sums we don't want
reduce = []
for i in range(len(totals)):
reduce.append([])
for j in perms:
if sum(j) == totals[i]:
reduce[i].append(j)
# }}}
# {{{ Making every possible path for the totals
tree = itertools.product(*reduce)
# }}}
# {{{ Printing out what I have so far
print("I have a variable `tree' with every possible path to product the totals.")
# }}}
# {{{ Returning every successful tree
selection = raw_input("Print `tree'? [yes/no] Hit CONTROL-C to cancel output.")
if selection == yes:
for i in tree:
print i
# }}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment