Skip to content

Instantly share code, notes, and snippets.

@sushain97
Last active October 11, 2015 18:01
Show Gist options
  • Save sushain97/be0a5e19a90e6d9e3062 to your computer and use it in GitHub Desktop.
Save sushain97/be0a5e19a90e6d9e3062 to your computer and use it in GitHub Desktop.
Given 2n points on a plane. One wants to draw n segments which join pairs of these points such that no two segments share a common vertex. In how many ways this can be done?
>>> l = range(1, 7)
>>> pairs = list(itertools.product(l, repeat=2))
>>> pairs = list(filter(lambda x: x[0] != x[1], pairs))
>>> l = list(itertools.combinations(pairs, 3))
>>> c = set(map(lambda x: frozenset(map(lambda y: frozenset(y), x)), l))
>>> len(set(filter(lambda x: len(set(list(itertools.chain(*list(map(lambda y: list(y), x)))))) is 6, c)))
15
>>> l[:5]
[((1, 2), (1, 3), (1, 4)), ((1, 2), (1, 3), (1, 5)), ((1, 2), (1, 3), (1, 6)), ((1, 2), (1, 3), (2, 1)), ((1, 2), (1, 3), (2, 3))]
>>> list(c)[:5]
[frozenset({frozenset({2, 4}), frozenset({1, 2}), frozenset({4, 6})}), frozenset({frozenset({2, 4}), frozenset({1, 2}), frozenset({3, 6})}), frozenset({frozenset({2, 3}), frozenset({4, 6}), frozenset({3, 6})}), frozenset({frozenset({1, 2}), frozenset({1, 4}), frozenset({4, 5})}), frozenset({frozenset({2, 6}), frozenset({1, 4}), frozenset({4, 5})})]
>>> l = range(1, 9)
>>> pairs = list(itertools.product(l, repeat=2))
>>> pairs = list(filter(lambda x: x[0] != x[1], pairs))
>>> l = list(itertools.combinations(pairs, 4))
>>> c = set(map(lambda x: frozenset(map(lambda y: frozenset(y), x)), l))
>>> len(set(filter(lambda x: len(set(list(itertools.chain(*list(map(lambda y: list(y), x)))))) is 8, c)))
105
>>> def ways(n):
... l = range(1, 2 * n + 1)
... pairs = list(itertools.product(l, repeat=2))
... pairs = list(filter(lambda x: x[0] != x[1], pairs))
... l = list(itertools.combinations(pairs, n))
... c = set(map(lambda x: frozenset(map(frozenset, x)), l))
... return len(set(filter(lambda x: len(set(list(itertools.chain(*list(map(list, x)))))) is 2 * n, c)))
...
>>> ways(1)
1
>>> ways(2)
3
>>> ways(3)
15
>>> ways(4)
105
>>> ways(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment