Hi all !
Thanks!
Here with two versions of Henrys algorithm which handles the 1 line
case. Performance is the same for all three.
Henrys solution is elegant and more J:ish than my sort version I think.
Old APL style algorithms like my sort version still often perform better.
NB. Erling
ZigZagE=:([: /: ] (([: $ [) $ ]) [: (], [: |. [: }: }.) [: i. [) { ]
NB. Mike
start =: i.@(<. #)
nstep =: (0 >. (-~ #)) >.@% <:@[
diffs =: (,:~ |.)@:+:@start
tconvert =: (]{~(#@])([-.~~.@:<.) ,@|:@(+/\)@ (start, nstep $diffs))
NB. Henry Rich
zzc =: (#@] $ ((] , -) i.)@<:@[) ;@:(</.) ]
NB. Henry Rich Erling rewrite
zzcE1=:(([: # ]) $ [: ([: (], [: |. [: }: }.) i.) [) ;@:(</.) ]
zzcE2=:(#@:] $ ((], |.@:}:@:}.)@:i.)@:[) ;@:(</.) ]
NB. just an arbitrary long string
text=.'THISISJUSTSOMERANDOMSENTENCEFORTESTINGTHEZIGZAGPROBLEMSOLUTIONSTHISISJUSTSOMERANDOMSENTENCEFORTESTINGTHEZIGZAGPROBLEMSOLUTIONSTHISISJUSTSOMERANDOMSENTENCEFORTESTINGTHEZIGZAGPROBLEMSOLUTIONSTHISISJUSTSOMERANDOMSENTENCEFORTESTINGTHEZIGZAGPROBLEMSOLUTIONS'
NB. do 15 zigzags
timespacex '(2 + i.15) tconvert"0 _ text' NB. see Mike Day's solution
0.000174903 33664
timespacex '(2 + i.15) zzc"0 _ text'
0.000121448 24576
timespacex '(2 + i.15) ZigZagE"0 _ text'
6.2007e_5 17536
timespacex '(2 + i.15) zzcE1"0 _ text'
0.000114606 24576
timespacex '(2 + i.15) zzcE2"0 _ text'
0.00011204 24576
((2 + i.15) tconvert"0 _ text) -: (2 + i.15) zzc"0 _ text
1
((2 + i.15) tconvert"0 _ text) -: (2 + i.15) ZigZagE"0 _ text
1
((2 + i.15) tconvert"0 _ text) -: (2 + i.15) zzcE1"0 _ text
1
((2 + i.15) tconvert"0 _ text) -: (2 + i.15) zzcE2"0 _ text
1
1 ZigZagE 'PAYPALISHIRING'
PAYPALISHIRING
1 zzcE1 'PAYPALISHIRING'
PAYPALISHIRING
1 zzcE2 'PAYPALISHIRING'
PAYPALISHIRING
Cheers,
Erling
On 2016-09-28 14:14, 'Mike Day' via Programming wrote:
Whoosh!
It even works ok with larg = 1 !
Good stuff,
Mike
On 28/09/2016 11:58, Erling Hellenäs wrote:
ZigZagE=:([: /: ] (([: $ [) $ ]) [: (], [: |. [: }: }.) [: i. [) { ]
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm