Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Sets #181

Closed
Neon22 opened this issue May 17, 2023 · 3 comments
Closed

[Feature Request] Sets #181

Neon22 opened this issue May 17, 2023 · 3 comments
Labels
enhancement New feature or request mojo-repo Tag all issues with this label

Comments

@Neon22
Copy link

Neon22 commented May 17, 2023

Request

I'd like to use Python Sets.
E.g. Pattern = [ {1,3}, {0,2,3,5}, {2,3,4,5}, {1,3,4,5}, {1,2,4,5}, {1,2,3} ]

Motivation

Its art of the language and I'm using it in my existing code which I just tried to convert to Mojo.
If there is an equiv structure - very happy to use it instead.

Description and Requirements

Here is an example Class using Sets (last function using 'in' - rest included for completeness)

from PythonInterface import Python
let iter = Python.import_module("itertools")

Pattern = [ {1,3},
            {0,2,3,5},
            {2,3,4,5},
            {1,3,4,5},
            {1,2,4,5},
            {1,2,3}
    ]
print(Pattern)

### Target class used for solving.
class Target(object):
    """ The target is the series of shafts associated with one treadle.
        The bricks are smaller chunks of shafts that, when combined, match the target.
    """
    def __init__(self, pattern, bricks=[]):
        self.pattern = pattern
        self.bricks = bricks
        self.ordered_bricks = [] # indices into the bricks for fast search
        self.solution = []       # holds simple solution only if easily found
        
    def __repr__(self):
        return "<Target %s: %d brick seqs>"%(list(self.pattern), len(self.bricks))
    
    def show(self):
        print(self)
        print("  ordered indices =", [ list(b) for b in self.ordered_bricks])
        for bseq in self.bricks:
            print(" ", [list(b) for b in bseq])
            
    def create_ordered_bricks(self, ordered_bricks):
        """ one value per brick as a set
        """
        for bseq in self.bricks:
            bricks = []
            for b in bseq:
                index = ordered_bricks.index(b)
                bricks.append(index)
            self.ordered_bricks.append(frozenset(bricks))

def allIntersections(frozenSets):
    """ recursive way to do intersections efficently
        - used by build_bricks()
    """
    if len(frozenSets) == 0:
          return []
    else:
        head = frozenSets[0]
        tail = frozenSets[1:]
        tailIntersections = allIntersections(tail)
        newIntersections = [head]
        newIntersections.extend(tailIntersections)
        newIntersections.extend(head & s for s in tailIntersections)
        return list(set(newIntersections))
    

def build_bricks(pattern):
    """ Find all intersections between pattern columns.
        These are the building blocks for finding a 
         minimal set of bricks to get the same result for less treadles.
    """
    bricks = []
    # initialise Bricks with pattern
    for s in pattern:
        bricks.append(frozenset(s))
    # find bricks
    bricks = allIntersections(bricks)
    bricks.remove(set({})) # remove the terminator artifact
    return(bricks)

def show_bricks(bricks):
    print("Bricks:", len(bricks))
    for b in bricks:
        print(" ",list(b))


bricks = build_bricks(Pattern)
show_bricks(bricks)
@Neon22 Neon22 added the enhancement New feature or request label May 17, 2023
@Mogball
Copy link
Collaborator

Mogball commented May 18, 2023

This is a duplicate of another issue requesting sets.

@Mogball Mogball closed this as completed May 18, 2023
@Neon22
Copy link
Author

Neon22 commented May 18, 2023

I couldn't find it by searching for sets
I hope its not this:
#43
(although existing python sets would probably enable this).

@Mogball
Copy link
Collaborator

Mogball commented May 18, 2023

#14

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request mojo-repo Tag all issues with this label
Projects
None yet
Development

No branches or pull requests

3 participants