Sorry, I gave the correct verb in my second mail.

Thanks for your solution. I'm not really sure I understand what cv is...
Is it a monad (i.e. 3 : 0)? It has a reference to the x argument, so it seems 
to be a dyad.
Also, what is the purpose of the colon on the first line of cv?

Thanks,
Jon
--------------------------------------------
On Tue, 9/27/16, 'Mike Day' via Programming <[email protected]> wrote:

 Subject: Re: [Jprogramming] Zig Zag problem
 To: [email protected]
 Date: Tuesday, September 27, 2016, 10:12 PM
 
 rmo's definition?
 
 Anyway, here's an approach
 which works on the indices.
 Please note that
 I haven't tested it thoroughly for boundary problems.
 
 I just consider the rising
 diagonals as extra columns.  Top & bottom
 indices will be reduplicated,  but can be
 removed with nub.
 
 eg for
 LHA = 5, we get this index array:
 0  8 
 (8)
 1  7   9
 2 
 6  10
 3  5  11  ..
 4 (4)
 12 (12)
 
 It actually works
 on the transpose first.
 
 cv
 =: 3 : 0
 :
 n =. x
 difs =. (,:~|.) +:@i. n <.l =. #y
 start=. i.n<.l
 nstep=. (0
 >. l-n) >.@% n-1
 y{~ l-.~
 ~.l<.,|:+/\start,nstep$difs
 )
 
     3 4 5 cv each/
 '';'paypa';1 2 3
 4;'paypalishiring'
 ++-----+-------+--------------+
 ||paapy|1 2 4 3|pahnaplsiigyir|
 ++-----+-------+--------------+
 ||payap|1 2 3 4|pinalsigyahrpi|
 ++-----+-------+--------------+
 ||paypa|1 2 3 4|phasiyirpligan|
 ++-----+-------+--------------+
 
 Any use?
 Mike
 
 
 On 27/09/2016 13:02, 'Jon Hough' via
 Programming wrote:
 > This is a
 programming challenge from here: 
https://leetcode.com/problems/zigzag-conversion/
 > The challenge is to convert a string into
 a "zig zag" formation with the given number of
 rows.
 >
 > e.g. (from
 the link)
 > PAYPALISHIRING is transformed
 into PAHNAPLSIIGYIR (the link explains clearly this
 transformation)
 >
 >
 This is my solution, which works.
 >
 >
 > NB. Convert text into
 a zigzag text.
 > NB. x: number of row
 (positive integer)
 > NB. y: string
 text
 > convert =: 4 : 0
 > rows =. x
 > text =.
 y
 > len =. # y
 > if. 0
 = len do. ''
 > elseif. 1 = len
 do. text
 > elseif. 1 = rows do. text
 > elseif. 1 do.
 > k=.
 0
 > l=. 0
 > res =.
 ''
 > NB. loop through the rows
 > while. (k < rows) *. k < len do.
 > res =. res, k{text
 >
 NB. how much to increment. first and last rows are
 special
 > NB. cases, inner rows switch
 the increment.
 > nextMax =. (2 * rows) -
 2
 > rm1 =. rows - 1
 >
 l1 =.
 ((+:@:(rm1&-))`(nextMax&[)@.((rmo&=)+.(0&=)))
 k
 > l2 =.
 ((+:)`(nextMax&[)@.((rm1&=)+.(0&=)))k
 > NB. first incremented value
 > nextIndex =. k + l1
 >
 NB. flag indicates which increment to use.
 > flag =. 1
 > while.
 nextIndex < len do.
 > res =. res,
 nextIndex{text
 > flag =. (flag = 0)
 > if. flag do. nextIndex =. nextIndex +
 l1
 > else. nextIndex =. nextIndex + l2
 > end.
 > end.
 > k =. k + 1 NB. increment the row
 > end.
 > res
 > end.
 >
 > )
 >
 > 3 convert 'PAYPALISHIRING'
 >
 > It's an
 interesting challenge and I'm sure there is a much nicer
 way to do this in J...
 >
 > Regards,
 > Jon
 >
 ----------------------------------------------------------------------
 > 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

Reply via email to