[Jprogramming] Selecting trains with m@.v

2019-12-06 Thread ethiejiesa via Programming
Am I just doing something silly? Or does @. really not support building trains when the right operand is a verb? Here is an overly minimal example of what I want: (1:)`+`(1:)@.(0 1 2) 0 2 (1:)`+`(1:)@.(0 1 2"_) 0 |rank error | (1:)`+`(1:)@.(0 1 2"_)0 This is an obv

Re: [Jprogramming] Removing annihilating pairs

2019-12-06 Thread 'Pascal Jasmin' via Programming
 X =: (&{::)(@:[) delitem =: ;@:((1 X {. ]) ; (0 X + 1 X) }. ]) NB. len idx :X str :Y delitemG =: delitem^:(#@] >: +/@[)  NB. guarded: do nothing if out of bounds (delitemG~ 2, 0 i.~ 2&(+/\))(^:_) 1 3 _3 3 5 1 3 5 On Friday, December 6, 2019, 07:09:57 p.m. EST, Henry Rich wrote:  

Re: [Jprogramming] Removing annihilating pairs

2019-12-06 Thread Henry Rich
   join =: 4 : 0 neq =. (- |. x) i.&0@:=&((x <.&# y)&{.) y  NB. len of max prefix of y that matches suffix of x ((-neq)}.x) , neq}.y )    ddup =.   (({. join&$: }.)~ <.@-:@#)^:(1<#)NB. split & recur    ddup 2 1 1 _1 2 _2 _1 2 Henry Rich On 12/6/2019 6:26 PM, Louis de Forcrand wrote: Not parti

Re: [Jprogramming] Removing annihilating pairs

2019-12-06 Thread Louis de Forcrand
Not particularly J-ish, but (array-shuffling aside) linear solution: s=: ,`(1}.])@.(= -@{.)/ s 1 _1 2 _2{~100?.@#4 2 1 2 1 _2 _1 2 1 1 2 2 2 _1 2 _1 2 1 2 2 2 1 1 _2 _1 2 1 _2 _1 _2 _2 _2 _2 _1 _2 _1 _1 _2 _2 1 1 1 2 1 _2 1 _2 _1 _1 _1 2 Since the reduced form of the input list is unique,

Re: [Jprogramming] Removing annihilating pairs

2019-12-06 Thread Louis de Forcrand
If the answer to Jimmy's question is no, then the uniqueness of the resulting array has to do (surprisingly) with free groups (https://en.wikipedia.org/wiki/Free_group). Indeed we can view a vector of numbers as a word over the alphabet [0,∞) of positive real numbers (where negative numbers / ze

Re: [Jprogramming] Removing annihilating pairs

2019-12-06 Thread Raul Miller
The result from 1 3 _3 3 5 should be 1 3 5. Thanks, -- Raul On Fri, Dec 6, 2019 at 5:36 PM Jimmy Gauvin wrote: > > Hi, > > is the expected output of transforming 1 3 _3 3 5 to be 1 3 5 or 1 5 ? > > On Fri, Dec 6, 2019 at 7:15 AM R.E. Boss wrote: > > > Given an array with zero or more annihila

Re: [Jprogramming] Removing annihilating pairs

2019-12-06 Thread Jimmy Gauvin
Hi, is the expected output of transforming 1 3 _3 3 5 to be 1 3 5 or 1 5 ? On Fri, Dec 6, 2019 at 7:15 AM R.E. Boss wrote: > Given an array with zero or more annihilating pairs, i.e., two subsequent > numbers which add up to zero, the question is to clean up the array by > deleting all annihila

Re: [Jprogramming] Removing annihilating pairs

2019-12-06 Thread Raul Miller
Actually, my approach did not properly handle cases which should give an empty result or length 1 result. Fixed version: an0=: 2#0~:_2 +/\ ] fix=: ^:(1<#) an1=: ({.,(#~ an0)@}.@}:,{:)fix@(#~ an0)fix an2=: an1^:_ #an2 1 _1 1 _1 0 an2 3 3 Thanks, -- Raul On Fri, Dec 6, 2019 a

Re: [Jprogramming] Removing annihilating pairs

2019-12-06 Thread Henry Rich
   (#~  (+: |.!.0)@([: >/\. 0 ,~ 0 = 2&(+/\)))^:_ ] 1 _1 2 _2{~100?.@#4 2 1 2 1 _2 _1 2 1 1 2 2 2 _1 2 _1 2 1 2 2 2 1 1 _2 _1 2 1 _2 _1 _2 _2 _2 _2 _1 _2 _1 _1 _2 _2 1 1 1 2 1 _2 1 _2 _1 _1 _1 2 Henry Rich On 12/6/2019 2:24 PM, Raul Miller wrote: Here's one approach: an0=: 2#0~:_2 +/\ ]

Re: [Jprogramming] Removing annihilating pairs

2019-12-06 Thread Raul Miller
Here's one approach: an0=: 2#0~:_2 +/\ ] an1=: ({.,(#~ an0)@}.@}:,{:)@(#~ an0) an2=: an1^:_ an2 1 _1 2 _2{~100?.@#4 2 1 2 1 _2 _1 2 1 1 2 2 2 _1 2 _1 2 1 2 2 2 1 1 _2 _1 2 1 _2 _1 _2 _2 _2 _2 _1 _2 _1 _1 _2 _2 1 1 1 2 1 _2 1 _2 _1 _1 _1 2 #an2 1 _1 2 _2{~100?.@#4 50 I have not conv

[Jprogramming] Removing annihilating pairs

2019-12-06 Thread R.E. Boss
Given an array with zero or more annihilating pairs, i.e., two subsequent numbers which add up to zero, the question is to clean up the array by deleting all annihilating pairs such that no such pairs are left. I do have a solution that is both elegant and efficient (I believe), but I am curious