NB. count number of repetitions
reps=: 3 : ';<@i."0 #/.~ +/\1,2~:/\y'
NB. identify last number or next number is 2 or
NB. (number >1 and next number =0)
bnds=: 1(_1)} (0,~2=}.)+.(1<]) *.0,~0=}.
NB. box string, add spaces after each token in a box,
NB. except for the last token
bx=: 3 : '('' ''}:@,@,.~])&.> y <;.2~ bnds reps y'
NB. stitch the tags and ravel, if t is odd delete last box,
NB. raze and if t is even delete last space
stch=: 3 : '}:^:(2|>:#y) ;}:^:(2|#y) , y,. (#y) $'' <span>'';''</span> '''
stch bx 'abccdeeeeefefefaaafff'
a b c c d e e <span>e e e</span> f e f e f a a <span>a</span> f f
<span>f</span>
stch bx 'abccdeeeeefefefaaaffg'
a b c c d e e <span>e e e</span> f e f e f a a <span>a</span> f f g
ts 'stch bx 100#t'
0.00022563727 112512
ts 'bookend space group 100#t'
0.00016670782 109568
ts 'bookend space group1 100#t'
0.0024253555 121600
R.E. Boss
> -----Oorspronkelijk bericht-----
> Van: [EMAIL PROTECTED] [mailto:programming-
> [EMAIL PROTECTED] Namens Sherlock, Ric
> Verzonden: vrijdag 11 juli 2008 4:28
> Aan: Programming forum
> Onderwerp: RE: [Jprogramming] Programming brain-teaser
>
> ---Raul Miller wrote:
> > On Thu, Jul 10, 2008 at 6:47 PM, Sherlock, Ric wrote:
> > > a b c c d e e <span>e e e</span> f e f e f a a
> > <span>a</span> f f <span>f</span>
> > >
> > > In English:
> > > Group together all duplicate items that occur anytime
> > beyond twice by wrapping them with a tag, naturally "bookending" them.
> > >
> > > Spoiler Alert.
> > > ==============
> > >
> > >
> > > Finding the mask of the atoms that need enclosing was Ok.
> > But I find my solution for enclosing them long winded and
> > overly complex. Is there a better way?
> >
> > t=: <"0 'abccdeeeeefefefaaafff'
> >
> > span=: (]`2:`[} '<span>' <@, 2&{::)@(]`_1:`[} '</span>' <@,~
> > _1&{::)^:(2<#)
> > teas=: [:; ,&.>&' '@;@(<@span/.~ 1 +/\@, 2 ~:/\ ])
> >
> > teas t
> > a b c c d e e <span>e e e</span> f e f e f a a <span>a</span> f f
> > <span>f</span>
>
> Using ideas from Raul's and Oleg's posts simplifies my solution:
> msk1=: 0 0 , 3 (1 = [EMAIL PROTECTED])\ ]
> group1=: ] </.~ [: +/\ 1 , 2 ~:/\ msk1
> space=: }.@,@(' '&,.)&.>
> bookend=: ;@,@(] ,. (' <span>';'</span> ') $~ #)
>
> bookend space group1 t
> a b c c d e e <span>e e e</span> f e f e f a a <span>a</span> f f
> <span>f</span>
>
> However my original code was faster and leaner:
> ts=: 6!:2 , 7!:2 @]
>
> (100) ts 'bookend space group t'
> 2.51652e_5 2816
> (100) ts 'bookend space group1 t'
> 0.000119682 3008
> (100) ts 'teas <"0 t'
> 0.0002852 5440
>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm