Hello Snappers!
Brooks (25M) here. I'm pretty good at math, but have always been a bit intimidated by coding so I've only just started using Snap last month I've been coming up with Snap coding projects to do for fun... I've gotten stuck on my most recent experiment.
The block gives the cross product of the lists, and it works as I understand it just fine, giving the result:
I want a slightly different block, that looks like this:
and that gives the following:
Essentially, I'm taking combinations of the first n numbers, no repeats, and only up to reordering of the selected numbers. Here's a slightly bigger example to get the point across:
The block should give the output:
At least, that's what I'm trying to do. Unordered combinations. Once I have this block I can use its array to map over other lists and get their unordered combinations.
There are probably many ways to do this easily, but the way I made up mimicked what you might do with paper and pencil. For unordered combinations first 6 numbers, choosing 3 at a time, I write as reference 1, 2, 3, 4, 5, 6, and then start taking combinations in this order:
1, 2, 3, 4, 5, 6
1, 2, 3, 4, 5, 6
1, 2, 3, 4, 5, 6
1, 2, 3, 4, 5, 6
1, 2, 3, 4, 5, 6
1, 2, 3, 4, 5, 6
1, 2, 3, 4, 5, 6
1, 2, 3, 4, 5, 6
1, 2, 3, 4, 5, 6
.... and so on.
Essentially, I call the bolded numbers Anchors. List the Anchors, then if there's room, move the rightmost anchor right one, and if there isn't room, move the next anchor right one and slide the others back.
Anyways, I'm having trouble implementing this (probably due to my lack of Snap familiarity, or coding familiarity in general.) Here's something really close:
So when running the unordered combinations block, (with inputs 6 and 3) it reports just an empty list. Here, I'm nesting the script "Anchor Runner" a number of times equal to the __-tuples I'm taking. Before entering the next nesting level, I append a list containing the anchor (the bold number) to the Updator List. If I'm at the deepest level, I also append the Updator List to the end of the Running List. It's really close to working... in the visible stepping the innermost nested layer works as intended, but when we go back out a layer to move the second to last anchor, the Running List was input earlier as blank, so the running list reverts to blank.
I have found a way around this, which is to scrap the input "Running List" and just make a Global Variable which gets appended by the Updator list. (This is how I got the list photos above). But I don't like this solution because it feel like a work-around, and I want this block to work without any global variables.
Again, I'm sure there are many ways to accomplish my goal (all of which I'm happy to see), but I'm particularly interested in fixing the code with the least amount of overhaul as possible, so as to expand my learning in this particular area.
Any suggestions?
Brooks
P.S. From just one list or set of objects, I want to be able to take all possible n-tuples with or without replacement, with or without order.
P.S. I've tried looking at maybe making the Running List an UpVar, that didn't seem to work, and also at making it an Unevaluated Input, that didn't seem to work either, but maybe I just don't fully comprehend how these work.