User:RE Boss/Recurringproblems/Nested2Boxed

From J Wiki
Jump to: navigation, search

Nested to Boxed

Problem stated by Bron 2009 August :

I needed to represent a "nested list" like this:

	   NEST =.  ;: '(345 * _2 + +/@:(+/%#) * ]) - ''hello''"_ , <.'

as analogously-nested set of boxes, like this:

	   ] BOX  =.  (< (;:'345*_2+ +/@:') , (<;:'+/%#') , ;:'*]') ,;: '-''hello''"_,<.'
	+---------------------------------+-+-------+-+-+-+--+
	|+---+-+--+-+-+-+--+---------+-+-+|-|'hello'|"|_|,|<.|
	||345|*|_2|+|+|/|@:|+-+-+-+-+|*|]|| |       | | | |  |
	||   | |  | | | |  ||+|/|%|#|| | || |       | | | |  |
	||   | |  | | | |  |+-+-+-+-+| | || |       | | | |  |
	|+---+-+--+-+-+-+--+---------+-+-+| |       | | | |  |
	+---------------------------------+-+-------+-+-+-+--+

Similar to a problem stated by Boss 2009 June :

Looking for a dyadic foo with

   4 = (3!:0) x         NB. x is integer
   (# x) = 2 = #$ x     NB. x has 2 items and has rank 2
   (# y) = +/{: x
   0 < <./{. x          NB. {.x is positive

such that

   ({.x) -: L."0 x foo y     NB. {.x is level of leaves
   ({:x) -: # S:0 x foo y    NB. {:x is tally of leaves

Example:

   (1 2 3 2 3 1,:2 1 3 2 1 3) foo i.12
+---+---+---------+-----+-----+-------+
|0 1|+-+|+-------+|+---+|+---+|9 10 11|
|   ||2|||+-----+|||6 7|||+-+||       |
|   |+-+|||3 4 5|||+---+|||8|||       |
|   |   ||+-----+||     ||+-+||       |
|   |   |+-------+|     |+---+|       |
+---+---+---------+-----+-----+-------+

Solution in Boss 2009 August, although the output is slightly different from what was required.

N2B=: 4 : 0		NB. Nested to Boxed
z=. 1 _1 0 {~ x i. y
;<@]^:[&.>/"1 (z <;._1 y) ,.~ (z=. 1 (0)} |z) <@{.;.1 +/\ z
)

   (;&,/'()') N2B NEST
+-------------------+-----------+-----+-+-------+-+-+-+--+
|+---+-+--+-+-+-+--+|+---------+|+-+-+|-|'hello'|"|_|,|<.|
||345|*|_2|+|+|/|@:|||+-+-+-+-+|||*|]|| |       | | | |  |
|+---+-+--+-+-+-+--+|||+|/|%|#|||+-+-+| |       | | | |  |
|                   ||+-+-+-+-+||     | |       | | | |  |
|                   |+---------+|     | |       | | | |  |
+-------------------+-----------+-----+-+-------+-+-+-+--+

To test my tacit fluency, also this version:

N2Btc=: [:; [: <@]^:[&.>/"1 ] (<@{.;.1~/@] ,. (<;._1~ {:)) [:(+/\ ,: 1 (0)} |) 1 _1 0 {~ i.

   (;&,/'()') N2Btc NEST
+-------------------+-----------+-----+-+-------+-+-+-+--+
|+---+-+--+-+-+-+--+|+---------+|+-+-+|-|'hello'|"|_|,|<.|
||345|*|_2|+|+|/|@:|||+-+-+-+-+|||*|]|| |       | | | |  |
|+---+-+--+-+-+-+--+|||+|/|%|#|||+-+-+| |       | | | |  |
|                   ||+-+-+-+-+||     | |       | | | |  |
|                   |+---------+|     | |       | | | |  |
+-------------------+-----------+-----+-+-------+-+-+-+--+

A solution which meets the requirements in Boss 2009 August

Using a technique Hui applied in http://www.jsoftware.com/pipermail/general/2008-July/032128.html

N2Ba=: 4 : 0
t=.((= * *@]) >./) 1 _1 0 (+/\)@:{~ x i. y
z=. 1 4 5 e.~ 3 #.\ 1 0 , t
; <@}.@}:@]^:[ &.>/"1 (z <@{.;.1 t) ,. z <;.1 y
)

N2Batc=: [:; [: <@}.@}:@]^:[ &.>/"1 ] (<@{.;.1&>~/@] ,.(<;.1~ {:)) [: (;1 4 5 e.~ 3 #.\ 1 0 , ]) [: ((= * *@]) >./) 1 _1 0 (+/\)@:{~ i.

   (;&,/'()') N2Ba^:_ NEST
+---------------------------------+-+-------+-+-+-+--+
|+---+-+--+-+-+-+--+---------+-+-+|-|'hello'|"|_|,|<.|
||345|*|_2|+|+|/|@:|+-+-+-+-+|*|]|| |       | | | |  |
||   | |  | | | |  ||+|/|%|#|| | || |       | | | |  |
||   | |  | | | |  |+-+-+-+-+| | || |       | | | |  |
|+---+-+--+-+-+-+--+---------+-+-+| |       | | | |  |
+---------------------------------+-+-------+-+-+-+--+
   BOX -: (;&,/'()') N2Ba^:_ NEST
1
   BOX -: (;&,/'()') N2Batc^:_ NEST
1

More solutions were given by Miller 2009 August where the 3 should be _ and by Ambrus 2009 August, although this one does not avoid recursion.