On a problem like this, like most problems in real life, execution speed
is less important than
* ease of coding
* ease of documentation
* confidence in the solution
* scalability
Henry Rich
On 9/27/2016 9:06 PM, 'Jon Hough' via Programming wrote:
That is a great solution. Thanks.
Just for fun I compared it with Mike Day's:
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.000179 33664
timespacex '(2 + i.15) zzc"0 _ text'
0.000114 24576
Leetcode give the users the number of milliseconds it took to run through their
unit test suite, but they don't make the unit tests public so it is not
possible to compare J solutions with other Leetcode solutions (they only allow
Java, C#, C++ etc)
--------------------------------------------
On Wed, 9/28/16, Henry Rich <[email protected]> wrote:
Subject: Re: [Jprogramming] Zig Zag problem
To: [email protected]
Date: Wednesday, September 28, 2016, 4:27 AM
zzc =: (#@] $ ((] ,
-) i.)@<:@[) ;@:(</.) ]
3 zzc
'PAYPALISHIRING'
PAHNAPLSIIGYIR
Henry Rich
On 9/27/2016 11:21 AM, 'Mike Day' via
Programming wrote:
> You might enjoy this
tacit version:
>
>
start =: i.@(<. #)
> nstep =:
(0 >. (-~ #)) >.@% <:@[
>
diffs =: (,:~ |.)@:+:@start
>
tconvert =: (]{~(#@])([-.~~.@:<.) ,@|:@(+/\)@ (start,
nstep $diffs))
>
>
M
>
>
> On 27/09/2016 15:27, 'Mike Day'
via Programming wrote:
>> Thanks.
It's nice to find an array-based solution to something
that
>> looks loopy!
>>
>> These
comments might help. (I talk about rows and columns as in
the
>> original problem,
>> though in my code the intermediate
array is transposed. Even columns
>>
are diagonals
>> in the original!):
>>
>>
"difs" is the expected differences between
row-wise adjacent pairs of
>>
indices.
>>
>>
"start" is merely the indices in the first
column.
>>
>>
"nstep" hopes to be an over-estimate of the
remaining number of
>> colums after
"start"
>>
>> +/\start, nstep$difs calculates the
indices, in general with some >:
>> l, the length
>>
of the input string.
>>
>> l<. ... limits the indices to <:
l
>>
>> l -. ~
~. .... removes l from the nub of indices
>>
>> y{~ ...
reorders the input array as required.
>>
>> Mike
>>
>>
>>
>> On 27/09/2016
14:49, 'Jon Hough' via Programming wrote:
>>>> NB. colon separates monad and
dyad sections
>>> I did not know
that, thanks.
>>>
>>> Your solution is great. I'll
have to spend a little time figuring
>>> out what's
>>> going on, but I can't find any
flaws yet. And much more J-like than
>>> mine.
>>>
>>>
Regards,
>>> 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:38 PM
>>>
Yes, our messages
>>> crossed.
>>> I'm lazy
with
>>> verb
definitions, nearly always using 3 : 0 even
>>> for a dyad. I could
have written
>>> cv
=: 3 : 0
>>> 3
>>> cv y NB.
monad section: supply a default larg
>>> for monad use
>>> :
NB. colon
>>> separates monad and
dyad sections
>>> [dyad
>>> section]
>>> )
>>> then 3 would be
the default left arg if used as
>>> a monad.
>>> As it is,
attempting to use it as
>>> a monad will result
in a
>>> domain
error.
>>> In my
example tableau for
>>> depth 5, I should
have pointed out
>>> that the
>>> differences between
columns are alternately 8 6 4 2 0 and 0
>>> 2 4 6 8
>>> Any clearer?
>>> Mike
>>> On
>>> 27/09/2016 14:22,
'Jon Hough' via Programming
>>> wrote:
>>> > 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
>>>
----------------------------------------------------------------------
>>> 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
----------------------------------------------------------------------
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