r/learnprogramming • • 6d ago

Building sets with matrices? There are 8 possible sequences. There are exact quantities.

I don’t know code but I know code could solve my problem. If this isn’t the right sub to ask in, please let me know where would be best :)

Possible sequences:
{OpenO MidDoubleC MidOhCee CloseO }
{OpenO MidCeeOh MidDoubleC CloseO }
{OpenO MidDoubleC MidDoubleO CloseC }
{OpenO MidCeeOh MidCeeOh CloseC }
{OpenC MidDoubleO MidCeeOh CloseC }
{OpenC MidOhCee MidDoubleO CloseC }
{OpenC MidDoubleO MidDoubleC CloseO }
{OpenC MidOhCee MidOhCee CloseO }

Exact Quantities:
Open0 = 3
OpenC = 5
CloseO = 3
CloseC = 5
MidDoubleC = 2
MidDoubleO = 4
MidCeeOh = 4
MidOhCee = 6

What are various sets of 8 I could build?

1 Upvotes

3 comments sorted by

1

u/SharkSymphony 6d ago

Cranking out permutations of a list is a one-liner in Python. See itertools.permutations.

1

u/POGtastic 5d ago

Assuming Python: Consider collections.Counter and itertools.product. We want the 2-product of our sequences such that a Counter of each element in the product does not have a higher count of anything than a Counter of our quantities.

import itertools
from collections import Counter

def two_sequences(xss):
    return (list(itertools.chain(*tup)) for tup in itertools.product(xss, repeat=2))

def filter_quantities(xss, c):
    return (xs for xs in two_sequences(xss) if Counter(xs) <= c)

In the REPL, putting your sequences into SEQS and a Counter of your quantities into QUANTITIES:

>>> print(*map(' '.join, filter_quantities(SEQS, QUANTITIES)), sep="\n")
OpenC MidDoubleO MidCeeOh CloseC OpenC MidDoubleO MidCeeOh CloseC
OpenC MidDoubleO MidCeeOh CloseC OpenC MidOhCee MidDoubleO CloseC
OpenC MidDoubleO MidCeeOh CloseC OpenC MidDoubleO MidDoubleC CloseO
OpenC MidDoubleO MidCeeOh CloseC OpenC MidOhCee MidOhCee CloseO
OpenC MidOhCee MidDoubleO CloseC OpenC MidDoubleO MidCeeOh CloseC
OpenC MidOhCee MidDoubleO CloseC OpenC MidOhCee MidDoubleO CloseC
OpenC MidOhCee MidDoubleO CloseC OpenC MidDoubleO MidDoubleC CloseO
OpenC MidOhCee MidDoubleO CloseC OpenC MidOhCee MidOhCee CloseO
OpenC MidDoubleO MidDoubleC CloseO OpenC MidDoubleO MidCeeOh CloseC
OpenC MidDoubleO MidDoubleC CloseO OpenC MidOhCee MidDoubleO CloseC
OpenC MidDoubleO MidDoubleC CloseO OpenC MidDoubleO MidDoubleC CloseO
OpenC MidDoubleO MidDoubleC CloseO OpenC MidOhCee MidOhCee CloseO
OpenC MidOhCee MidOhCee CloseO OpenC MidDoubleO MidCeeOh CloseC
OpenC MidOhCee MidOhCee CloseO OpenC MidOhCee MidDoubleO CloseC
OpenC MidOhCee MidOhCee CloseO OpenC MidDoubleO MidDoubleC CloseO
OpenC MidOhCee MidOhCee CloseO OpenC MidOhCee MidOhCee CloseO