2 ms·
The ‘normal’ way to write something like that (I hope I got the range ends right) is for i in range(1, N): for j in range(1, j + 1): candidate_pair
by Someone 5y ago
The ‘normal’ way to write something like that (I hope I got the range ends right) is
for i in range(1, N):
for j in range(1, j + 1):
candidate_pairs.add((i, j))
Also, conceptually, you don’t have tuples, but multisets. If you were to use those, you wouldn’t need that if at all.
Multiset isn’t built-in to python, so you’d need to use an external package. Alternatively, write a class for storing pairs of ints i, j that enforces i ≤ j.
- drexlspivey 5y agoPython actually has built in support for multisets, you use a set of frozensets >>> a = set( (frozenset((1,2)), frozenset((3,4)) ) ) >>> frozenset((2,1)) in a True
- Someone 5y agoThat’s not a multiset, it’s a set containing tuples. A multiset (https://en.wikipedia.org/wiki/Multiset https://en.wikipedia.org/wiki/Multiset) allows you to add identical items multiple times. You would use that to, for example, store the knowledge that 6 can be written as 3+3, by using a multiset {3,3} instead of a tuple (3,3)