[Jprogramming] J-like approach for merging numbers in a list
Raul Miller
rauldmiller at gmail.com
Wed Jun 8 12:28:54 UTC 2016
That string would not happen in this game - the numbers are always
integer powers of 2. However, the result should be 6 6.
Marshall's mergerow =: ((*>:) #~ _1|.-.@]) [:>/\.&.|. 0,~ 2 =/\ ]
gets it right.
Thanks,
--
Raul
On Wed, Jun 8, 2016 at 12:55 AM, Don Guinn <donguinn at gmail.com> wrote:
> Given a string 6 3 3. Should the result be 6 6, or should it be 12?
> On Jun 7, 2016 5:02 PM, "Ric Sherlock" <tikkanz at gmail.com> wrote:
>
>> Thanks Marshall!
>> I really like this solution. Very clean and concise too.
>> Identify adjacent numbers, double them, compress out the even ones.
>>
>> My solution using rplc worked fine as a single verb. It was only when I
>> included it as part of the application that things didn't work properly. I
>> agree I should look into this further.
>>
>>
>> On Wed, Jun 8, 2016 at 10:15 AM, Marshall Lochbaum <mwlochbaum at gmail.com>
>> wrote:
>>
>> > That solution doesn't work on empty input either. The problem is that
>> > (0,~ 2 =/\ $0) has length one when it should be zero; since we know in
>> > this case that the input is never negative, we can append _1 before
>> > finding equal groups rather than a 0 after.
>> >
>> > mergerow =. ((*>:) #~ _1|.-.@]) [:>/\.&.|. 2 =/\ ,&_1
>> > mergerow $0
>> >
>> > $mergerow $0
>> > 0
>> >
>> > Marshall
>> >
>> > On Tue, Jun 07, 2016 at 06:12:54PM -0400, Marshall Lochbaum wrote:
>> > > Unpredictable behavior? The second version of mergerow seems like it
>> > > should give exactly the same results as the first. Any problems it
>> > > causes seem like cases for further debugging.
>> > >
>> > > Here's a version that doesn't rely on rplc, if it helps.
>> > >
>> > > mergerow =: ((*>:) #~ _1|.-.@]) [:>/\.&.|. 0,~ 2 =/\ ]
>> > >
>> > > mergerow 3 4 4 4 4 4 5 8 8 7
>> > > 3 8 8 4 5 16 7
>> > >
>> > > The tricky part is suppressing extra matches. My initial solution used
>> > > a nice trick for counting within groups of ones:
>> > >
>> > > ]m =. 0,~ 2 =~/\ 3 4 4 4 4 4 5 8 8 7
>> > > 0 1 1 1 1 0 0 1 0 0
>> > > ([*+)/\.&.|. m
>> > > 0 1 2 3 4 0 0 1 0 0
>> > > 2|([*+)/\.&.|. m
>> > > 0 1 0 1 0 0 0 1 0 0
>> > >
>> > > Since we only need the answer mod 2, we can shift the 2&| inside, and
>> > > note that (2|+) is the same as (~:) on binary inputs. Then ([*~:) turns
>> > > out to be equal to (>) on binary inputs, a fact I found just by
>> > > computing the truth table.
>> > >
>> > > ([*~:)/\.&.|. m
>> > > 0 1 0 1 0 0 0 1 0 0
>> > > ([*~:)"0/~ 0 1
>> > > 0 0
>> > > 1 0
>> > > >/\.&.|. m
>> > > 0 1 0 1 0 0 0 1 0 0
>> > >
>> > > Marshall
>> > >
>> > > On Wed, Jun 08, 2016 at 09:15:14AM +1200, Ric Sherlock wrote:
>> > > > I'm looking for a J-like approach to a verb for merging adjacent
>> > numbers in
>> > > > a row where the numbers are the same. For example
>> > > >
>> > > > mergerow 4 4 4 4
>> > > >
>> > > > 8 8
>> > > >
>> > > > mergerow 8 8
>> > > >
>> > > > 16
>> > > >
>> > > > mergerow 16
>> > > >
>> > > > 16
>> > > >
>> > > > mergerow 4 4 4
>> > > >
>> > > > 8 4
>> > > >
>> > > > mergerow 8 4
>> > > >
>> > > > 8 4
>> > > >
>> > > > mergerow 4 8 8 4
>> > > >
>> > > > 4 16 4
>> > > >
>> > > >
>> > > > My version below which I've used as part of the answer to a
>> > RosettaCode task
>> > > > <http://rosettacode.org/wiki/2048#J> is very non J-like:
>> > > >
>> > > > mergerow=: verb define
>> > > > len=. <:@# y
>> > > > row=. i. i=. 0
>> > > > while. (i <: len) *. len >: 0 do.
>> > > > if. =/ (i , i+1) { y,_9 do.
>> > > > row=. row, +: i { y
>> > > > i=. i + 2
>> > > > else.
>> > > > row=. row, i { y
>> > > > i=. i + 1
>> > > > end.
>> > > > end.
>> > > > row
>> > > > )
>> > > >
>> > > > I thought I had a good solution with:
>> > > >
>> > > > mergerow=: rplc (2&# ; +:)&>@~.
>> > > >
>> > > > But this seemed to cause unpredictable behaviour when used as part of
>> > the
>> > > > whole solution.
>> > > >
>> ----------------------------------------------------------------------
>> > > > For information about J forums see
>> http://www.jsoftware.com/forums.htm
>> > ----------------------------------------------------------------------
>> > For information about J forums see http://www.jsoftware.com/forums.htm
>> >
>> ----------------------------------------------------------------------
>> For information about J forums see http://www.jsoftware.com/forums.htm
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
More information about the Programming
mailing list