last day (15 days later) » 

8:09 PM
2
A: Partition a set into subsets of size $k$

RojoTry with partitions[list_, l_] := With[{sub = Subsets[list, {l}, Binomial[Length[list] - 1, l - 1]]}, Flatten[ Table[{#, Sequence @@ i}, {i, partitions[Complement[list, #], l]}] & /@ sub, 1] ] partitions[list_, l_] /; Length[list] === l := {{list}} The list must have ...

 
This threw errors when I tried partitions[Range@9, 3]
 
@Mr.Wizard It doesn't throw errors to me, and I restarted the kernel to check
 
okay, either I screwed up the copy&paste or you made a ninja-edit, as it's working now.
 
I just edited because there was a useless With. @Mr.Wizard please if you check and still doesn't work let me know
Haha, I think you made a ninja-copy-paste and I made a ninja-edit, cause I first posted it with a missing line and edited 2 second later
 
It appears you beat mine on speed. I'm sorry I cannot +1 but I am out of votes.
Join me in Chat, please?
 
8:09 PM
Alo
 
Hello Rojo.
Would you please explain the Binomial logic in your function?
 
Evening Spartacus
Sure
If I have a list of say 5 elements
Binomial[5, 2] gives you the number of subsets of 2 elements you can take from those 5
So, let's assume I have a list {a, b, c, d, e, f}
and I want to know how many subsets of 3 elements that have "a" in it I can make
That's the same as asking how many subsets of 3-1 I can make in {b, c, d, e, f}
because a will be in all of them
 
Ah, I see it now. Very good!
 
Fortunately, when you use the 3 argument version of Subsets, the order in which it finds them allows me to find "all the subsets that have the first element of the list" in that notation
compact, nice
so I find those, and then recursively do the same on the complement
I'll ask you something
Say I built something like
{a, {{b, c}, {3, {56, 7}}}
How would you do to get "the list of all the paths of the tree"? I mean, to get
{{a,b,3, 56}, {a, b, 3, 7}, {a, c, 3, 56} ,{a, c, 3, 7}}
 
Interesting question. Let me think about that.
You're missing a bracket in the first line.
 
8:16 PM
Seems so
 
This should be easy but now I am under pressure and I cannot think. :-)
 
Haha
I thought it should be easy, but
I'm asking because I can only think of non-easy ways
No pressure, let me know
wait
Now that I have you here
You linked in a comment to a ProjectEuler problem
and I went and tried to do it, fun
but my result was wroong
Oh, forget it, I must have lost the code anyway
I'm letting you go free
 
Free is good. :^)
@Rojo I'm not sure I understand the tree problem. Could you provide a slightly larger example remove to ambiguity?
 
I explained it very unclearly
Ok
{a, {b,{c,d}}}
(bad enter)
 
(You know you can edit your comments for about a minute after you post them, right? Hover over the left edge just to the right of your avatar, click the down arrow, and choose edit.)
 
8:35 PM
I'm also clearing up the problem in my head as I explain... The previous example wasn't valid, nor the tree description, sorry... {a, {{b, {d, e}, {c, {e, f}}}}} for example, should give you all the 3 letter combinations like {a, b, d}, {a, b, e}, {a, c, e}, {a, c, f}
Humm... Don't bother, I still don't even have it clear in my head, but in the partitions answer I would have liked to avoid the "Sequence@@" and just nest the partitions of the complements, and only restructure in the end... But it seemed hard. And I know that more than a couple of times I've wanted to do something very similar
(not only the Sequence@@, it would have also avoided the Table)
 
Your original question, even if it is not what you intended, is still interesting. I may try to solve my interpretation of it just to learn. When you have a clear specification in mind please let me know.
 
Hehe, ok
 
I'll be going now. Thanks for the chat.
 
Buhbye
 
oh
partitions :)
 
8:43 PM
It's on top
The rest is worthless
Hello buhbye
Came back to poke you in advance @MrWizard, as you asked, in case the 24hs pass and I forget
 
@Szabolcs and Rojo, before I leave (always hard to put down a problem for me):
"Cases traverses the parts of expr in a depth-first order, with leaves visited before roots."
I am almost positive someone showed how do return results similar to Cases but in a breadth-first order. I cannot find it. Help?
 
I don't know but
 
okay, really gone this time. bye
 
9:10 PM
@Rojo (yes, I'm back again) this is my interpretation of your original tree question:
expr = {a, {{b, c}, {3, {56, 7}}}};

Join @@
Table[
   Cases[expr, {Longest[x__?AtomQ], ___} :> {x}, {i}],
   {i, 0, Depth@expr}
] // Tuples
I still think there was I cleaner way to do breadth first but I cannot recall it.
 

last day (15 days later) »