On Fri, Sep 26, 2008 at 7:04 AM, Paul Moore <[EMAIL PROTECTED]> wrote: > 2 4 5 6 12 --> 3 > 1 2 3 4 5 6 --> 6 > > Other constraints ensure that there will only ever be 1 such run (for > example 2 3 4 6 7 8, with 2 runs of 3, is not possible). However, > duplicates are: 1 12 13 13 14 - the longest run here is length 3. For > extra credit, I need to know about the duplicates (so that I can > calculate how many combinations make a run of 3).
Working on your question rather than your problem (I do not play cribbage and do not have time to learn its rules), but noting that your arguments tend to be short: rlen=: [:>./[:,/((#,[EMAIL PROTECTED]) * >:@}: *./@:>: }.)\.\ rlen 2 4 5 6 12 3 3 rlen 1 2 3 4 5 6 6 6 rlen 1 12 13 13 14 4 3 The first number in the result is the length of the result with duplicates included and the second is the number of unique elements in the result. Note, however, that this does not make any attempt to identify the duplicate values -- you did not say what you wanted there in a way that makes sense to a non-cribbage player, like me, so I just made something up. So I need to document how to get what you would have wanted out of this code. So, ok, first off, the phrase verb\.\ uses verb on all suffixes of all prefixes of its argument. Consider: <\.\ 2 4 5 6 12 <@(<\.)\ 2 4 5 6 12 In my case, the verb I used was ((#,[EMAIL PROTECTED]) * >:@}: *./@:>: }.) The right hand part of this verb (>:@}: *./@:>: }.) returns 1 when no part of the argument increases by more than 1 per item. The left hand part of this verb (#,[EMAIL PROTECTED]) gives me the length of the argument section under consideration and its number of unique elements. Hypothetically, you could replace (#,[EMAIL PROTECTED]) with whatever verb you wanted that would extract the information you needed. However, if your new result is not a fixed width numeric list you probably will need to replace my selection mechanism -- instead of multiplying by 1, for example, maybe you want to put everything in a box and then use #~ to determine if this was a potential valid result. Note, also, that if you reorganize the results significantly you will also need to change my "pick the winner" algorithm. I used [:>./\ [:,/ Verb If you went with a boxed result, would need to replace >./\ with something that picks the best box from the list of potentially valid boxes that this routine generates. (Note also that >./\ picks the "longest raw" and "longest unique" columns in an independent fashion. I can get away with this because for valid possibilities one can not be longest without the other also being longest.) FYI, -- Raul ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
